历史上的今天
今天是:2024年12月31日(星期二)
2021年12月31日 | 4412 RS485
2021-12-31 来源:eefocus
一、485硬件原理
差分对传输数据的原理
IO数据的传输→差分对
rs232传输的距离在15米以下,RS485传输距离是几十米到1000米以上
为什么485可以传输这么远
差分对的机制可以降低电磁场的干扰
衰减
485传输距离和传输线有关系
注意:双绞线和屏蔽线
二、485原理图
嵌入式上一般使用串口转485
分析芯片datasheet

串口的信号转化为485则:
D→(A,B),DE高电平,RE高电平
485信号转化为串口信号则:
(A,B),DE低电平,RD低电平
三、驱动
485驱动=串口驱动+GPIO的字符驱动
BUF_XURTS1高电平发送,低电平接收
串口驱动是drivers/char/max485_ctl.c
485驱动=串口驱动+GPIO的字符驱动
BUF_XURTS1高电平发送,低电平接收
(GPIO是GPA0_7,串口设备节点是ttySAC1)
驱动中只需要操作GPIO
设备节点/dev/max485_ctl
应用中,控制GPIO和串口=类似led+串口的操作
ioctl是参数是1,则输出高电平,发送
ioctl是参数是0,则输出低电平,接收
串口的节点/dev/ttySAC1
运行程序
发送./test_485 /dev/ttySAC1 1
接收./test_485 /dev/ttySAC1 0
测试程序发送的信息:iTOP-4412: max485 test app(times:%d)
测试程序:
//#include #include //#include "uart.h" #include #include #include #include #include #include #include #include #include #define MAX485_CONTROL //#include "uart.c" int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop) { struct termios newtio,oldtio; if ( tcgetattr( fd,&oldtio) != 0) { perror("SetupSerial 1"); return -1; } bzero( &newtio, sizeof( newtio ) ); newtio.c_cflag |= CLOCAL | CREAD; newtio.c_cflag &= ~CSIZE; switch( nBits ) { case 7: newtio.c_cflag |= CS7; break; case 8: newtio.c_cflag |= CS8; break; } switch( nEvent ) { case 'O': newtio.c_cflag |= PARENB; newtio.c_cflag |= PARODD; newtio.c_iflag |= (INPCK | ISTRIP); break; case 'E': newtio.c_iflag |= (INPCK | ISTRIP); newtio.c_cflag |= PARENB; newtio.c_cflag &= ~PARODD; break; case 'N': newtio.c_cflag &= ~PARENB; break; } printf("Baund Rate: %dn", nSpeed); switch( nSpeed ) { case 2400: cfsetispeed(&newtio, B2400); cfsetospeed(&newtio, B2400); break; case 4800: cfsetispeed(&newtio, B4800); cfsetospeed(&newtio, B4800); break; case 9600: cfsetispeed(&newtio, B9600); cfsetospeed(&newtio, B9600); break; case 115200: cfsetispeed(&newtio, B115200); cfsetospeed(&newtio, B115200); break; case 460800: cfsetispeed(&newtio, B460800); cfsetospeed(&newtio, B460800); break; case 921600: printf("Rate:921600n"); cfsetispeed(&newtio, B921600); cfsetospeed(&newtio, B921600); break; default: cfsetispeed(&newtio, B9600); cfsetospeed(&newtio, B9600); break; } if( nStop == 1 ) newtio.c_cflag &= ~CSTOPB; else if ( nStop == 2 ) newtio.c_cflag |= CSTOPB; newtio.c_cc[VTIME] = 0; newtio.c_cc[VMIN] = 0; tcflush(fd,TCIFLUSH); if((tcsetattr(fd,TCSANOW,&newtio))!=0) { perror("com set error"); return -1; } // printf("set done!nr"); return 0; } int prepare_to_send(int fd) { int ret; ret = ioctl(fd, 1, 0); if(ret<0) { printf("max485 set ctl to high failed!rn"); return -1; } else { return 0; } } int prepare_to_recv(int fd) { int ret; ret = ioctl(fd, 0, 0); if(ret<0) { printf("max485 set ctl to low failed!rn"); return -1; } else { return 0; } } void main(int argc, char* argv[]) { unsigned char ucTmp; int fd1,fd2,nset1,nset2,nread; char buf[100]; //char buf1[1]; //char *buff = "Hellonr"; int i = 0; char *max485_ctl = "/dev/max485_ctl_pin"; if(3 != argc) { printf("Usage: test_485 [uart port] [type]rn"); printf(" type: 0--recv, 1--sendrn"); return; } fd1 = open(argv[1], O_RDWR); if (fd1 == -1) { printf("Open %s faildn", argv[1]); exit(1); } nset1 = set_opt(fd1, 9600, 8, 'N', 1); if (nset2 == -1) { printf("Set uart faildn"); exit(1); } #ifdef MAX485_CONTROL if((fd2=open(max485_ctl, O_RDWR|O_NOCTTY|O_NDELAY))<0) { printf("Open %s faildn", max485_ctl); close(fd1); exit(1); } #endif if(0 == atoi(argv[2])) //recv { #ifdef MAX485_CONTROL prepare_to_recv(fd2); #endif while(1) { nread = read(fd1, buf, 100); if (nread > 0) { for(i=0; i printf("%c", buf[i]); if(buf[i] == 'q') //break; goto exit; } } //if(nread) //{ // printf("rn"); //} sleep(1); } } else //send { #ifdef MAX485_CONTROL prepare_to_send(fd2); #endif while(1) { printf("Send data, time:%drn", i); sprintf(buf, "iTOP-4412: max485 test app(times:%d)rn", i++); //nread = write(fd1, "iTOP-4412: max485 test apprn", strlen("iTOP-4412: max485 test apprn")); nread = write(fd1, buf, strlen(buf)); sleep(1); #if 0 nread = read(fd1, buf, 100); if (nread > 0) { for(i=0; i printf("%c", buf[i]); if(buf[i] == 'q') //break; goto exit; }
史海拾趣
|
大家好,我是一名大三的学生,学的是嵌入式方向的,但我们现在才学嵌入式操作系统,都TMD还是理论,学了等于没学,就上学期还学了个汇编,现在一学期都快过完了,感觉什么关于嵌入式方面的都没学到,如果靠下学期再来学点东西,以后出去就别想混了 ...… 查看全部问答> |
|
先详细解释一下标题的意思。 我的播放器运行在wince5.0中,我编写了各种各样的播放器,有使用WMP的,有使用TCPMP的,有使用用DSHOW的,都会有这么个情况: 我要跳转到视频的某个位置,比如说10s,不能准确的设置到10s,而 ...… 查看全部问答> |
|
用核心板做一款机器,最后做电源管理才发现PWREN给悬空了,且VDDi,VDDiarm和VDDalive一起供电,休眠省电基本不用想了,现在我想仅靠降低CLOCK时钟降低功耗,比如进入SLOW模式,能将2440电流降低到多少?哪位试验过? 另外在wince系统运行时候转换n ...… 查看全部问答> |
|
请教各位帮忙单步跟踪一下下面这个程序 #include \"stdafx.h\" #include #include using namespace &n ...… 查看全部问答> |
|
本公司(知名国企)正在寻求IPCamera(网络摄像机)的技术合作,包括嵌入式开发+硬件设计、中心 管理平台开发。有特色的产品,可提供整机、主板,或者能提供优秀的硬件方案的亦可。 合作方式多种,可技术转让、兼职或者全职。 &nb ...… 查看全部问答> |
|
程序先前一直没问题,后来改程序又加了几条语句,在编译过程中就有一个错误:Code size limit exceeded(4096 code bytes)for this version of the compiler 4212bytes of CODE memory 10bytes of CONST memory(+4bytes shared) 544bytes of DATA me ...… 查看全部问答> |




