|
我现在在做一个CC2430通信实验。两个CC2430分别连上PC机,使用串口调试助手完全正常。
但是若是我用一个CC2430连上PC机,串口助手自动写入数据发送,
然后另外一个CC2430连上TQ2440的串口扩展模块的COM3口,如此做通信实验时,出现一个现象:
TQ2440写串口数据,PC机端串口助手显示另外一个CC2430接收正常,但是TQ2440反复读取数据时,只能读取几次数据而已(read函数此后返回的字节数都是0)。下面是代码,望各位大侠帮忙解决,急!!!!!- serial_test.h
- #define FALSE 0
- #define TRUE 1
- #define WORDLEN 32 //其他名称长度32字节(16个汉字)
- struct serial_config
- {
- unsigned char serial_dev[WORDLEN];
- unsigned int serial_speed;
- unsigned char databits;
- unsigned char stopbits;
- unsigned char parity;
- };
复制代码- serial_test.c
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <errno.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <unistd.h>
- #include"pthread.h"
- #include "serial_test.h"
- struct serial_config serialread;
- static int serial_fd,beep_fd;
- int speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300,
- B38400, B19200, B9600, B4800, B2400, B1200, B300};
- int name_arr[] = {230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300,
- 38400, 19200, 9600, 4800, 2400, 1200, 300};
- //-----------------------------------------------
- //打印配置文件tq2440_serial.cfg的内容
- //-----------------------------------------------
- void print_serialread()
- {
- printf("serialread.dev is %s\n",serialread.serial_dev);
- printf("serialread.speed is %d\n",serialread.serial_speed);
- printf("serialread.databits is %d\n",serialread.databits);
- printf("serialread.stopbits is %d\n",serialread.stopbits);
- printf("serialread.parity is %c\n",serialread.parity);
- }
- //-----------------------------------------------
- //读取tq2440_serial.cfg文件并进行配置
- //-----------------------------------------------
- void readserialcfg()
- {
- FILE *serial_fp;
- char j[10];
-
- printf("readserailcfg\n");
- serial_fp = fopen("/etc/tq2440_serial.cfg","r");
- if(NULL == serial_fp)
- {
- printf("can't open /etc/tq2440_serial.cfg");
- }
- else
- {
- fscanf(serial_fp, "DEV=%s\n", serialread.serial_dev);
- fscanf(serial_fp, "SPEED=%s\n", j);
- serialread.serial_speed = atoi(j);
- fscanf(serial_fp, "DATABITS=%s\n", j);
- serialread.databits = atoi(j);
- fscanf(serial_fp, "STOPBITS=%s\n", j);
- serialread.stopbits = atoi(j);
- fscanf(serial_fp, "PARITY=%s\n", j);
- serialread.parity = j[0];
- }
- fclose(serial_fp);
- }
- //-----------------------------------------------
- //设置波特率
- //-----------------------------------------------
- void set_speed(int fd)
- {
- int i;
- int status;
- struct termios Opt;
- struct termios oldOpt;
- tcgetattr(fd, &oldOpt);
- // printf("serialread.speed is %d\n",serialread.serial_speed);
- for( i = 0; i < sizeof(speed_arr)/sizeof(int); i++)
- {
- if(serialread.serial_speed == name_arr[i])
- {
- 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;
- }
- tcflush(fd, TCIOFLUSH);
- }
- }
- }
- //-----------------------------------------------
- //设置其他参数
- //-----------------------------------------------
- int set_Parity(int fd)
- {
- struct termios options;
- struct termios oldoptions;
- if(tcgetattr(fd, &oldoptions) != 0)
- {
- perror("SetupSerial 1");
- return(FALSE);
- }
- options.c_cflag |= (CLOCAL|CREAD);
- options.c_cflag &=~CSIZE;
- // printf("serialread.databits is %d\n",serialread.databits);
- switch(serialread.databits)
- {
- case 7:
- options.c_cflag |= CS7;
- break;
- case 8:
- options.c_cflag |= CS8;
- break;
- default:
- options.c_cflag |= CS8;
- fprintf(stderr, "Unsupported data size\n");
- return(FALSE);
- }
- // printf("serialread.parity is %c\n",serialread.parity);
- switch(serialread.parity)
- {
- case 'n':
- case 'N':
- options.c_cflag &= ~PARENB;
- options.c_iflag &= ~INPCK;
- break;
- case 'o':
- case 'O':
- options.c_cflag |= (PARODD | PARENB);
- options.c_iflag |= INPCK;
- break;
- case 'e':
- case 'E':
- options.c_cflag |= PARENB;
- options.c_cflag &= ~PARODD;
- options.c_iflag |= INPCK;
- break;
- default:
- options.c_cflag &= ~PARENB;
- options.c_iflag &= ~INPCK;
- fprintf(stderr, "Unsupported parity\n");
- return(FALSE);
- }
- // printf("serialread.stopbits is %d\n",serialread.stopbits);
- switch(serialread.stopbits)
- {
- case 1:
- options.c_cflag &= ~CSTOPB;
- break;
- case 2:
- options.c_cflag |= CSTOPB;
- break;
- default:
- options.c_cflag &= ~CSTOPB;
- fprintf(stderr, "Unsupported stop bits\n");
- return(FALSE);
- }
- if(serialread.parity != 'n')
- options.c_iflag |= INPCK;
- options.c_cc[VTIME] = 150; //150; //15 seconds
- options.c_cc[VMIN] = 0;
- #if 1
- options.c_iflag |= IGNPAR|ICRNL;
- options.c_oflag |= OPOST;
- //options.c_iflag &= ~(IXON|IXOFF|IXANY);
- options.c_iflag |= IXOFF|IXON;
- #endif
- tcflush(fd, TCIFLUSH);
- 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("Can't Open Serial Port");
- return -1;
- }
- else
- return fd;
- }
- //--------------------------------------------------
- //串口初始化
- //--------------------------------------------------
- void serial_init(void)
- {
- char *Dev;
- int i;
- readserialcfg();
- print_serialread();
- Dev = serialread.serial_dev;
- //打开串口设备
- serial_fd = OpenDev(Dev);
- if(serial_fd > 0)
- set_speed(serial_fd); //设置波特率
- else
- {
- printf("Can't Open Serial Port!\n");
- exit(0);
- }
- //恢复串口未阻塞状态
- /*if(fcntl(serial_fd,F_SETFL,0)<0) */
- if (fcntl(serial_fd, F_SETFL, fcntl(serial_fd,F_GETFL,0) | O_NONBLOCK) < 0)
- {
- printf("fcntl failed!\n");
- exit(0);
- }
- //检查是否是终端设备
- #if 1 //如果屏蔽下面这段代码,在串口输入时不会有回显的情况,调用下面这段代码时会出现回显现象。
- if(isatty(STDIN_FILENO)==0)
- {
- printf("standard input is not a terminal device\n");
- }
- else
- printf("isatty success!\n");
- #endif
- //设置串口参数
- if(set_Parity(serial_fd) == FALSE)
- {
- printf("Set parity Error\n");
- exit(1);
- }
- }
- int read_data(int fd, void *buf, int len)
- {
- int count;
- int ret;
- ret = 0;
- count = 0;
- //while (len > 0) {
- ret = read(fd, (char*)buf + count, len);
- if (ret < 0) {
- fprintf(stderr, "Read error %s\n", strerror(errno));
- //break;
- }
- count += ret;
- len = len - ret;
- //}
- *((char*)buf + count) = 0;
- return count;
- }
- int write_data(int fd, void *buf, int len)
- {
- int count;
- int ret;
- ret = 0;
- count = 0;
- while (len > 0) {
- ret = write(fd, (char*)buf + count, len);
- if (ret < 1) {
- fprintf(stderr, "Write error %s\n", strerror(errno));
- break;
- }
- count += ret;
- len = len - ret;
- }
- return count;
- }
- void serial_rw()
- {
- int i=0;
- char buff[512];
- char buff2[]="hello,TQ2440!\n";
- int nread,nwrite;
- struct timeval tv;
- fd_set rfds;
-
- tv.tv_sec=0;
- tv.tv_usec=30;
-
- nwrite=write_data(serial_fd,buff2,sizeof(buff2));
- printf("nwrite=%d\n",nwrite);
- while(1){
- printf("waiting........!\n");
- /*if (fcntl(serial_fd, F_SETFL, fcntl(serial_fd,F_GETFL,0) | O_NONBLOCK) < 0)
- {
- printf("fcntl failed!\n");
- exit(0);
- }
- nread=read_data(serial_fd,buff,50);
- printf("readlength=%d\n",nread);
- buff[nread]='\0';
- printf("%s\n",buff);
- sleep(2);*/
-
- FD_ZERO(&rfds);
- FD_SET(serial_fd,&rfds);
- if(select(serial_fd+1,&rfds,NULL,NULL,&tv)>0)
- {
- printf("enter select()!\n");
- if(FD_ISSET(serial_fd,&rfds))
- {
- printf("reading!\n");
- nread=read_data(serial_fd,buff,10);
- printf("readlength=%d\n",nread);
-
- printf("i=%d\n",++i);
- buff[nread]='\0';
- printf("%s\n",buff);
- memset(buff,'\0',sizeof(buff));
- //tcflush(serial_fd, TCIFLUSH);
- }
-
- }
- sleep(2);
-
- }
- close(serial_fd);
- }
- int main(int argc, char **argv)
- {
- serial_init();
- serial_rw();
- return 0;
- // close(serialread.serial_dev);
- // serial_test(telnum, telmoney);
- }
复制代码 如下是运行效果:
readserailcfg
serialread.dev is /dev/tq2440_serial2
serialread.speed is 38400
serialread.databits is 8
serialread.stopbits is 1
serialread.parity is N
isatty success!
nwrite=15
waiting........!
waiting........!
enter select()!
reading!
readlength=128
i=1
98745698745698745698745698745698745698745698745698745698745698745698745698745698745698745698745698745698745698745698745698745698
waiting........!
waiting........!
waiting........!
waiting........! |
|