本帖最后由 简单侶途 于 2015-11-9 21:16 编辑
1.最近很多人在CC-A80平台上遇到很多奇怪的串口问题,今天亲生经历了一遍,调试代码,终于成功出现效果。
下面是我找到的参考资料,要注意的是官方资料里面代码有误,正确代码我会给出,请大家对比参考。
A80T uart使用说明书.pdf
(894.55 KB, 下载次数: 61)
2.修改后代码如下:- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <termios.h>//必须包含此头文件
- #include <errno.h>
- #define FALSE 0
- #define TRUE 1
- int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
- B38400, B19200, B9600, B4800, B2400, B1200, B300, };//波特率
- int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,
- 38400, 19200, 9600, 4800, 2400, 1200, 300, };
- void set_speed(int fd, int speed)
- {
- int i;
- int status;
- struct termios opt;
- tcgetattr(fd,&opt);
- for (i= 0;i<sizeof(speed_arr)/sizeof(int);i++){
- if(speed == name_arr[i]){
- printf("set speed!\n");
- tcflush(fd, TCIOFLUSH);
- cfsetispeed(&opt, speed_arr[i]);
- cfsetospeed(&opt, speed_arr[i]);
- status = tcsetattr(fd, TCSANOW, &opt);
- if(status != 0)
- perror("tcsetattr fd1");
- return;
- }
- printf("no set speed!\n");
- tcflush(fd,TCIOFLUSH);
- }
- }
- int set_Parity(int fd,int databits,int stopbits,int parity)
- {
- struct termios options;
- if( tcgetattr( fd,&options)!= 0){
- perror("SetupSerial 1");
- return(FALSE);
- }
- options.c_cflag &= ~CSIZE;
- switch(databits){
- case 7:
- options.c_cflag |= CS7;
- break;
- case 8:
- options.c_cflag |= CS8;
- break;
- default:
- fprintf(stderr,"Unsupported data size\n");
- return (FALSE);
- }
- switch(parity){
- case 'n':
- case 'N':
- options.c_cflag &= ~PARENB; /* Clear parity enable */
- options.c_iflag &= ~INPCK; /* Enable parity checking */
- options.c_iflag &= ~(ICRNL|IGNCR);
- options.c_lflag &= ~(ICANON );
- break;
- case 'o':
- case 'O':
- options.c_cflag |= (PARODD | PARENB);
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'e':
- case 'E':
- options.c_cflag |= PARENB; /* Enable parity */
- options.c_cflag &= ~PARODD;
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'S':
- case 's': /*as no parity*/
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported parity\n");
- return (FALSE);
- }
- //设置停止位
- switch(stopbits){
- case 1:
- options.c_cflag &= ~CSTOPB;
- break;
- case 2:
- options.c_cflag |= CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported stop bits\n");
- return (FALSE);
- }
- /* Set input parity option */
- if(parity != 'n')
- options.c_iflag |= INPCK;
- options.c_cc[VTIME] = 150; // 15 seconds
- options.c_cc[VMIN] = 0;
- tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
- if(tcsetattr(fd,TCSANOW,&options) != 0){
- perror("SetupSerial 3");
- return (FALSE);
- }
- return (TRUE);
- }
- //打开串口
- int openDev(char *Dev)
- {
- int fd = open(Dev,O_RDWR);
- if(-1 == fd){
- perror("cat't open serial port");
- return -1;
- } else
- return fd;
- }
- static void changestr(char *str, char *out_str)
- {
- int i = 0, j = 0;
- while((str[i++]) && (i <= 1024)){
- if(str[i-1] == ',')
- out_str[j] = ' ';
- else
- out_str[j] =str[i-1];
- j++;
- }
- }
- int main(int argc,char **argv)
- {
- int fd;
- int nread;
- char buff[512];
- char *dev = "/dev/ttyS4";
- char write_info[1024];
- char write_ch[256];
-
- fd = openDev(dev);//根据设备节点,打开相应的uart口
- if(fd > 0){
- set_speed(fd,115200);
- } else {
- printf("can't open serial port!\n");
- exit(0);
- }
- if(set_Parity(fd,8,1,'N') == FALSE) {
- printf("set parity error\n");
- exit(1);
- }
- while(1){
- while((nread = read(fd,&buff,512))>0){ //读测试
- printf("\nLen %d\n",nread);
- buff[nread + 1] = '\0';
- printf("%s\n",buff);
- }
- }
- close(fd);
- exit(0);
- }
复制代码 3.测试方法如下:
a.首先记得要插好串口线,在板子插UART4-RX,UART-TX,GND三根线后USB端连Win7电脑,Win7电脑安装使用串口工具SSCOM3.3版本,输入数据,鼠标点击发送,空白框会出现发送成功的数据,而不是你自以为的板子发回来的数据。并且出现最底下会有S:18 | R:18 反正是S-send R-receiver
root@cubieboard4:~/uart# gcc -o uart_a80 uart_a80.c
root@cubieboard4:~/uart# ./uart_a80
set speed! //初始化打印
Len 18 //显示发送数据长度
this is a uart_a80 //打印发送的数据
b.
CC-A80板子终端显示
sscom输入数据
以上显示效果操作如下:
注意设置程序定的波特率115200
(1)scan: abc
点发送
(2)scan: qwer
点发送
(3)scan: 1223jkh32
点发送
|