差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
template:temp_uart [2023/02/07 17:51] wjj |
template:temp_uart [2023/02/21 18:31] (当前版本) wjj |
||
---|---|---|---|
行 1: | 行 1: | ||
- | 系统下操作 UART 的测试串口,以 | + | 系统下操作 UART 的测试串口,以 |
- | COM2 设备节点为: | + | J_RS232 TX3\RX3 |
- | | + | /dev/ttysWK2 |
C参考UART高低电平输入代码如下: | C参考UART高低电平输入代码如下: | ||
<code C> | <code C> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | int set_opt(int, | ||
+ | |||
+ | void main(){ | ||
+ | int fd,nByte; | ||
+ | char *uart3 = "/ | ||
+ | char bufferR[2]; | ||
+ | char bufferW[2]; | ||
+ | |||
+ | memset(bufferR, | ||
+ | memset(bufferW, | ||
+ | |||
+ | if((fd=open(uart3, | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | else{ | ||
+ | printf(" | ||
+ | set_opt(fd, | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | while(1){ | ||
+ | |||
+ | nByte = 0; | ||
+ | |||
+ | memset(bufferR, | ||
+ | memset(bufferW, | ||
+ | |||
+ | // printf(" | ||
+ | if((nByte = read(fd, bufferR, 1)) == 1){ // | ||
+ | |||
+ | |||
+ | printf(" | ||
+ | |||
+ | bufferW[0] = ' | ||
+ | |||
+ | write(fd, | ||
+ | |||
+ | |||
+ | memset(bufferR, | ||
+ | memset(bufferW, | ||
+ | |||
+ | nByte = 0; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | close(fd); | ||
+ | |||
+ | } | ||
+ | |||
+ | // | ||
+ | int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop) | ||
+ | { | ||
+ | struct termios newtio, | ||
+ | // | ||
+ | if ( tcgetattr( fd,& | ||
+ | perror(" | ||
+ | return -1; | ||
+ | } | ||
+ | // | ||
+ | bzero( & | ||
+ | newtio.c_cflag | ||
+ | newtio.c_cflag &= ~CSIZE; | ||
+ | // | ||
+ | switch( nBits ) | ||
+ | { | ||
+ | case 7: | ||
+ | newtio.c_cflag |= CS7; | ||
+ | break; | ||
+ | case 8: | ||
+ | newtio.c_cflag |= CS8; | ||
+ | break; | ||
+ | } | ||
+ | // | ||
+ | switch( nEvent ) | ||
+ | { | ||
+ | // | ||
+ | case ' | ||
+ | newtio.c_cflag |= PARENB;// | ||
+ | newtio.c_cflag |= PARODD;// | ||
+ | newtio.c_iflag |= (INPCK | ISTRIP);// | ||
+ | break; | ||
+ | case ' | ||
+ | newtio.c_iflag |= (INPCK | ISTRIP); | ||
+ | newtio.c_cflag |= PARENB; | ||
+ | newtio.c_cflag &= ~PARODD;// | ||
+ | break; | ||
+ | case ' | ||
+ | newtio.c_cflag &= ~PARENB;// | ||
+ | break; | ||
+ | } | ||
+ | // | ||
+ | switch( nSpeed ) | ||
+ | { | ||
+ | case 2400: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | case 4800: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | case 9600: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | case 115200: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | case 460800: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | default: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | } | ||
+ | // | ||
+ | if( nStop == 1 ) | ||
+ | newtio.c_cflag & | ||
+ | else if ( nStop == 2 ) | ||
+ | newtio.c_cflag |= CSTOPB;// | ||
+ | |||
+ | newtio.c_cc[VTIME] | ||
+ | newtio.c_cc[VMIN] = 0;// | ||
+ | |||
+ | tcflush(fd, | ||
+ | |||
+ | // | ||
+ | if((tcsetattr(fd, | ||
+ | { | ||
+ | perror(" | ||
+ | return -1; | ||
+ | } | ||
+ | // | ||
+ | return 0; | ||
+ | } | ||
</ | </ | ||
行 17: | 行 167: | ||
将编译好的程序使用 scp 拷贝到 3399 主板上,执行测试: | 将编译好的程序使用 scp 拷贝到 3399 主板上,执行测试: | ||
- | {{: | + | {{: |