串口不能发数据 收正常 达人帮忙看看程序
串口不能发数据 收正常 达人帮忙看看程序
#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 BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS0 "
#define _POSIX_SOURCE 1
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
main()
{
int fd,c, res;
struct termios oldtio,newtio;
char buf[255];
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }
tcgetattr(fd,&oldtio);
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 1;
newtio.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
while (STOP==FALSE) {
write(fd, "abc ",3);
res = read(fd,buf,255);
if(res == 0) continue;
buf[res]=0;
printf( ":%s:%d\n ", buf, res);
if (buf[0]== 'z ') STOP=TRUE;
}
tcsetattr(fd,TCSANOW,&oldtio);
}
------解决方案-------------------- newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio_new.c_cflag &= ~CSTOPB; //1 stop bits
newtio_new.c_cflag &= ~PARENB; //no parity check
------解决方案--------------------1,分别测试读和写.
2,检查你的串口线.
3,现不要设那么多项,仅仅用fsetispeed和fsetospeed设波特率看看.