我写一个串口通信的代码,通过串口发送数据,首先申明硬件已经通了,就是发送的时候发送不出去,接收端显示不出数据,接受端也是正确的,通过发送工具发送是可以的,代码如下,请指教一下为什么我发送不出去!哪里设置可能出问题了。
void set_speed(int fd, int speed)
{
struct termios Opt;
tcgetattr(fd, &Opt);
for(int i = 0; i < sizeof(speed_arr)/sizeof(int); i++)
{
if(speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
int status = tcsetattr(fd, TCSANOW, &Opt);
if(status != 0) perror("tcsetattr fd1");
return;
}
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;
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;
case 'S':
case 's':
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);
}
if(parity != 'n') options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150;
options.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
int port_in()
{
if(fd != 0){
char data[8];
int i = read(fd, data, sizeof(data));
printf("recv i=%d\n",i);
for(int j=0;j<i;j++){
printf("recv %d\n",data[i]);
}
printf("\n");
}
}
int port_in_w(){
while(fd != 0){
port_in();
}
}
void disable_terminal_return(int fd)
{
struct termios newt;
tcgetattr(fd, &newt);
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(fd, TCSANOW, &newt);
}
void check_port_open(unsigned baud)