差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
template:temp_uart [2023/02/07 17:15] 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高低电平输入代码如下: | ||
行 10: | 行 10: | ||
#include < | #include < | ||
#include < | #include < | ||
+ | #include < | ||
+ | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
- | #include <unistd.h> | + | #include <termios.h> |
- | #include <string.h> | + | #include <errno.h> |
- | + | int set_opt(int, | |
- | + | ||
- | static char gpio_path[100]; | + | void main(){ |
- | static | + | int fd,nByte; |
- | { | + | char *uart3 = "/ |
- | char file_path[100]; | + | char bufferR[2]; |
- | int len; | + | char bufferW[2]; |
- | int fd; | + | |
- | | + | memset(bufferR, ' |
- | if (0 > (fd = open(file_path, O_WRONLY))) { | + | memset(bufferW, ' |
- | | + | |
- | | + | if((fd=open(uart3,O_RDWR,0777))<0) |
- | } | + | { |
- | | + | printf("failed\n"); |
- | if (len != write(fd, val, len)) { | + | } |
- | | + | else{ |
- | close(fd); | + | printf(" |
- | | + | set_opt(fd, 115200, 8, ' |
- | } | + | |
- | close(fd); | + | } |
- | | + | |
+ | |||
+ | while(1){ | ||
+ | |||
+ | nByte | ||
+ | |||
+ | memset(bufferR, ' | ||
+ | memset(bufferW, | ||
+ | |||
+ | // printf(" | ||
+ | | ||
+ | |||
+ | |||
+ | printf("receive: | ||
+ | |||
+ | bufferW[0] = ' | ||
+ | |||
+ | write(fd,bufferW,1); // | ||
+ | |||
+ | |||
+ | memset(bufferR, | ||
+ | memset(bufferW, | ||
+ | |||
+ | nByte = 0; | ||
+ | | ||
+ | |||
+ | } | ||
+ | |||
+ | close(fd); | ||
+ | |||
} | } | ||
- | + | ||
- | int main(int argc, char *argv[]) | + | // |
+ | int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop) | ||
{ | { | ||
- | char file_path[100]; | + | struct termios newtio, |
- | char val; | + | //将原串口的数据取到oldtio |
- | int fd; | + | if ( tcgetattr( fd,& |
- | | + | perror("SetupSerial 1"); |
- | if (2 != argc) { | + | return |
- | | + | } |
- | exit(-1); | + | //将newio清零和设置c_cflag |
- | } | + | bzero( &newtio, sizeof( newtio ) ); |
- | /* 判断指定编号的 GPIO 是否导出 */ | + | newtio.c_cflag |
- | | + | newtio.c_cflag &= ~CSIZE; |
- | | + | //设置数据位 |
- | int len; | + | switch( nBits ) |
- | | + | { |
- | | + | case 7: |
- | exit(-1); | + | newtio.c_cflag |= CS7; |
- | } | + | break; |
- | len = strlen(argv[1]); | + | case 8: |
- | | + | newtio.c_cflag |= CS8; |
- | | + | break; |
- | close(fd); | + | } |
- | exit(-1); | + | // |
- | } | + | switch( nEvent |
- | close(fd); // | + | { |
- | } | + | //偶校验 |
- | + | case ' | |
- | /* 配置为输入模式 */ | + | newtio.c_cflag |= PARENB;// |
- | | + | newtio.c_cflag |= PARODD;// |
- | exit(-1); | + | newtio.c_iflag |= (INPCK | ISTRIP);// |
- | /* 极性设置 | + | break; |
- | if (gpio_config(" | + | case ' |
- | exit(-1); | + | newtio.c_iflag |= (INPCK | ISTRIP); |
- | | + | newtio.c_cflag |= PARENB; |
- | if (gpio_config(" | + | newtio.c_cflag &= ~PARODD;//取消偶校验(置零偶校验位),开启奇校验 |
- | | + | break; |
- | /* 读取 | + | case ' |
- | | + | newtio.c_cflag &= ~PARENB;//不进行奇偶校验 |
- | if (0 > (fd = open(file_path, O_RDONLY))) { | + | break; |
- | perror(" | + | } |
- | | + | // |
- | } | + | switch( nSpeed |
- | + | { | |
- | | + | case 2400: |
- | perror(" | + | cfsetispeed(& |
- | | + | cfsetospeed(& |
- | exit(-1); | + | break; |
- | } | + | case 4800: |
- | printf(" | + | cfsetispeed(& |
- | /* 退出程序 */ | + | cfsetospeed(& |
- | close(fd); | + | break; |
- | exit(0); | + | case 9600: |
+ | cfsetispeed(&newtio, B9600); | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | case 115200: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | case 460800: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | default: | ||
+ | cfsetispeed(&newtio, B9600); | ||
+ | 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,TCIFLUSH);// | ||
+ | |||
+ | // | ||
+ | if((tcsetattr(fd,TCSANOW,&newtio))!=0) | ||
+ | { | ||
+ | perror(" | ||
+ | return | ||
+ | } | ||
+ | // printf(" | ||
+ | return | ||
} | } | ||
+ | |||
</ | </ | ||
行 99: | 行 167: | ||
将编译好的程序使用 scp 拷贝到 3399 主板上,执行测试: | 将编译好的程序使用 scp 拷贝到 3399 主板上,执行测试: | ||
- | {{: | + | {{: |