int main(void)
{
int fd,nread;
struct termios Opt;
char buff[512];
/*以读写方式打开串口*/
fd = open( "/dev/ttySAC2", O_RDWR|O_NONBLOCK);
if(-1 == fd)
{
perror("提示错误!");/* 不能打开串口一*/
return 1;
}
//获取属性
tcgetattr(fd, &Opt);
tcflush(fd, TCIOFLUSH);
//设置为19200Bps
cfsetispeed(&Opt,B115200);
cfsetospeed(&Opt,B115200);
//8位数据位
Opt.c_cflag &= ~CSIZE;
Opt.c_cflag |= CS8;
//无校验
Opt.c_cflag &= ~PARENB; /* Clear parity enable */
Opt.c_iflag &= ~INPCK;
//1停止位
Opt.c_cflag &= ~CSTOPB;
//
Opt.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
Opt.c_cc[VMIN] = 0; /* Update the options and do it NOW */
//设置属性
tcsetattr(fd,TCSANOW,&Opt);
tcflush(fd, TCIOFLUSH);
//
while(!quit)
{
while((nread = read(fd, buff, 512))>0)
{
buff[nread+1] = '\0';
write(fd,buff,nread);
}
}
printf("quit\n");
close(fd);
exit (0);
}
int main(void)
{
int fd,nread;
struct termios Opt;
char buff[512];
/*以读写方式打开串口*/
fd = open( "/dev/ttySAC2", O_RDWR|O_NONBLOCK);
if(-1 == fd)
{
perror("提示错误!");/* 不能打开串口一*/
return 1;
}
//获取属性
tcgetattr(fd, &Opt);
tcflush(fd, TCIOFLUSH);
//设置为19200Bps
cfsetispeed(&Opt,B115200);
cfsetospeed(&Opt,B115200);
//8位数据位
Opt.c_cflag &= ~CSIZE;
Opt.c_cflag |= CS8;
//无校验
Opt.c_cflag &= ~PARENB; /* Clear parity enable */
Opt.c_iflag &= ~INPCK;
//1停止位
Opt.c_cflag &= ~CSTOPB;
//
Opt.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
Opt.c_cc[VMIN] = 0; /* Update the options and do it NOW */
//设置属性
tcsetattr(fd,TCSANOW,&Opt);
tcflush(fd, TCIOFLUSH);
//
while(!quit)
{
while((nread = read(fd, buff, 512))>0)
{
buff[nread+1] = '\0';
write(fd,buff,nread);
}
}
printf("quit\n");
close(fd);
exit (0);
}
To copy to clipboard, switch view to plain text mode
now i want to place it into a class ,inherited from QThread.
Bookmarks