差别
这里会显示出您选择的修订版和当前版本之间的差别。
后一修订版 | 前一修订版 | ||
template:temp_uart [2022/12/28 19:31] ljy 创建 |
template:temp_uart [2023/02/21 18:31] (当前版本) wjj |
||
---|---|---|---|
行 1: | 行 1: | ||
- | 系统下操作 UART 的方式测试串口,以 | + | 系统下操作 UART 的测试串口,以 |
- | COM2 设备节点为: | + | J_RS232 TX3\RX3 |
- | | + | /dev/ttysWK2 |
- | C参考代码如下: | + | C参考UART高低电平输入代码如下: |
<code C> | <code C> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
- | #include < | ||
#include < | #include < | ||
- | #include <stdio.h> | + | #include <errno.h> |
- | #include < | + | int set_opt(int, |
- | #include < | + | |
- | #define UART_DEVICE | + | void main(){ |
+ | int fd,nByte; | ||
+ | char *uart3 = "/dev/ttyS2"; | ||
+ | char bufferR[2]; | ||
+ | char bufferW[2]; | ||
- | int main(int argc, char *argv[]) | + | memset(bufferR, ' |
- | { | + | memset(bufferW, |
- | int fd, res; | + | if((fd=open(uart3,O_RDWR,0777))<0) |
- | struct termios | + | { |
- | char ch; | + | printf(" |
- | char buf[256] = {0}; | + | } |
- | + | else{ | |
- | // | + | printf(" |
- | | + | set_opt(fd, 115200, 8, ' |
- | if (fd < 0) { | + | |
- | | + | |
- | | + | |
- | | + | |
- | else | + | |
- | printf(" | + | |
- | + | ||
- | // | + | |
- | tcgetattr(fd, & | + | |
- | memset(& | + | |
- | + | ||
- | // | + | |
- | newtio.c_cflag = B115200|CS8|CLOCAL|CREAD; | + | |
- | newtio.c_iflag = IGNPAR; | + | |
- | tcflush(fd, TCIFLUSH);//清空输入缓冲区和输出缓冲区 | + | } |
- | | + | |
+ | |||
+ | 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); | ||
- | // | + | } |
- | res=write(fd, | + | |
- | while(1) { | + | |
- | // | + | |
- | while((ch=getchar()) != ' | + | |
- | buf[0]=ch; | + | |
- | res=write(fd, | + | |
- | | + | |
- | + | ||
- | buf[0]=ch; | + | |
- | buf[1]=' | + | |
- | res = write(fd, buf, 2); | + | |
- | break; | + | |
- | } | + | |
- | // | + | |
- | while(1) { | + | |
- | res = read(fd, buf, 255);// | + | |
- | if (res == 0) | + | |
- | continue; | + | |
- | + | ||
- | buf[res] = ' '; | + | |
- | printf(" | + | |
- | if (buf[0] == ' | + | |
- | break; | + | |
- | } | + | |
- | // | + | |
- | close(fd); | + | |
- | printf(" | + | |
- | tcsetattr(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; | ||
} | } | ||
+ | |||
</ | </ | ||
+ | |||
+ | 交叉编译源码: | ||
+ | |||
+ | aarch64-linux-gnu-gcc -o uart uart.c | ||
将编译好的程序使用 scp 拷贝到 3399 主板上,执行测试: | 将编译好的程序使用 scp 拷贝到 3399 主板上,执行测试: | ||
- | {{: | + | {{: |