当前位置: 技术问答>linux和unix
GCC下如何设置串口的参数,在本地传数据时(ttyS0和ttyS1)正常,和windows传数据出现乱码
来源: 互联网 发布时间:2014-12-10
本文导语: 这是读写的代码: //读数据 #include #include #include #include #include #include #define BAUDRATE B115200 open_port(void) { int fd,speed; struct termios newtio; fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY); if(fd==-1) { perror("open_port:Una...
这是读写的代码:
//读数据
#include
#include
#include
#include
#include
#include
#define BAUDRATE B115200
open_port(void)
{
int fd,speed;
struct termios newtio;
fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if(fd==-1)
{
perror("open_port:Unable to open /dev/ttyS0");
}
tcgetattr(fd,&newtio);
bzero(&newtio,sizeof(newtio));
//setting c_cflag
newtio.c_cflag |= (CLOCAL|CREAD);
newtio.c_cflag &=~PARENB;
newtio.c_cflag &=~PARODD;
newtio.c_cflag &=~CSTOPB;
newtio.c_cflag &=~CSIZE;
newtio.c_cflag |=CS8;
newtio.c_oflag|=OPOST;
//setting c_iflag
newtio.c_iflag &=~(IXON|IXOFF|IXANY);
cfsetispeed(&newtio,BAUDRATE);
cfsetospeed(&newtio,BAUDRATE);
printf("speed=%dn",BAUDRATE);
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
return(fd);
}
main()
{
int fd,n,i;
unsigned char buff[256];
fd = open_port();
while(1){
n = read(fd,buff,256);
if(n>0){
printf("n=%dn",n);
for(i=0;i
//读数据
#include
#include
#include
#include
#include
#include
#define BAUDRATE B115200
open_port(void)
{
int fd,speed;
struct termios newtio;
fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if(fd==-1)
{
perror("open_port:Unable to open /dev/ttyS0");
}
tcgetattr(fd,&newtio);
bzero(&newtio,sizeof(newtio));
//setting c_cflag
newtio.c_cflag |= (CLOCAL|CREAD);
newtio.c_cflag &=~PARENB;
newtio.c_cflag &=~PARODD;
newtio.c_cflag &=~CSTOPB;
newtio.c_cflag &=~CSIZE;
newtio.c_cflag |=CS8;
newtio.c_oflag|=OPOST;
//setting c_iflag
newtio.c_iflag &=~(IXON|IXOFF|IXANY);
cfsetispeed(&newtio,BAUDRATE);
cfsetospeed(&newtio,BAUDRATE);
printf("speed=%dn",BAUDRATE);
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
return(fd);
}
main()
{
int fd,n,i;
unsigned char buff[256];
fd = open_port();
while(1){
n = read(fd,buff,256);
if(n>0){
printf("n=%dn",n);
for(i=0;i