关于串口通信的问题 急啊!!!
我写一个串口通信的代码,通过串口发送数据,首先申明硬件已经通了,就是发送的时候发送不出去,接收端显示不出数据,接受端也是正确的,通过发送工具发送是可以的,代码如下,请指教一下为什么我发送不出去!哪里设置可能出问题了。
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)
{
if(fd == 0){
char * dev ="/dev/ttyAMA1";
fd = open(dev, O_RDWR);
if(-1 == fd)
{
fd = 0;
printf("Can't Open Serial Port!\n");
return;
}
else
{
printf("dev=%s\n",dev);
}
set_speed(fd, baud);
set_parity(fd, 8, 1, 'N');
disable_terminal_return(fd);
}
}
int port(unsigned baud, char *data, int data_length)
{
//printf("send len=%d\n", data_length);
for(int i = 0; i < data_length; ++i){
printf("sned %d\n",data[i]);
}
printf("\n");
check_port_open(baud);
if(fd != 0){
write(fd, data, data_length);
}
printf("send buffer\n");
}
void close_port()
{
if(fd == 0)return;
close(fd);
fd = 0;
}
int main()
{
char buffer[3];
buffer[0]=1;
buffer[1]=2;
buffer[2]=3;
printf("buffer0=%d,buffer1=%d,buffer2=%d\n",buffer[0],buffer[1],buffer[2]);
port(9600,buffer,3);
while(1){
port(9600,buffer,3);
sleep(2);
}
return 0;
}
串口
[解决办法]
linux的console编程不太懂,串口一般就是波特率、数据位、停止位、校验位以及流控方式。这几个参数两边match了就没太大问题。
还有你的buffer里还是填几个可显示字符比如‘a’,‘b’,‘c’之类的吧,也许你已经接收到了,只是1,2,3不能显示出来而已。