[英]Linux C write serial port (Arduino) and wait for answer
Dear Colleagues. 亲爱的同事们。 I'm trying write C program for Linux to write serial port (Arduino) and wait for answer.
我正在尝试为Linux编写C程序以编写串行端口(Arduino)并等待答案。 I know, there was many Q about it on forum, but all I tried have the same problem - successfully write - but not read answer.
我知道,论坛上对此有很多疑问,但是我尝试过的所有问题都存在相同的问题-成功编写-但未阅读答案。 For example, here and here .
例如, 在这里和这里 。 I have found two separate files for write and for read .
我找到了两个单独的文件用于写入和读取 。
I'm compile and run read file in one terminal window, and write file in other. 我正在编译并在一个终端窗口中运行读取文件,并在另一个窗口中运行文件。 Works great.
效果很好。 But I can't merge them in one file to write and then wait for answer.
但是我无法将它们合并到一个文件中以编写然后等待答案。 The same problem - write, but not read.
同样的问题-写,但不读。
Here is like I tried: 这就像我尝试过的:
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
void main(void)
{
int fd;
fd = open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_SYNC);
if(fd == -1)
printf("\n Error! in Opening ttyUSB0 ");
else
printf("\n ttyS0 Opened Successfully ");
struct termios SerialPortSettings;
tcgetattr(fd, &SerialPortSettings);
cfsetispeed(&SerialPortSettings,B9600);
cfsetospeed(&SerialPortSettings,B9600);
/* 8N1 Mode */
SerialPortSettings.c_cflag &= ~PARENB;
SerialPortSettings.c_cflag &= ~CSTOPB;
SerialPortSettings.c_cflag &= ~CSIZE;
SerialPortSettings.c_cflag |= CS8;
SerialPortSettings.c_cflag &= ~CRTSCTS;
SerialPortSettings.c_cflag |= CREAD | CLOCAL;
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/
/* Setting Time outs */
SerialPortSettings.c_cc[VMIN] = 10; /* Read at least 10 characters */
SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly */
if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0)
printf("\n ERROR ! in Setting attributes");
else
printf("\n BaudRate = 9600 \n StopBits = 1 \n Parity = none");
char write_buffer[] = "Hello/n";
int bytes_written = 0;
printf("\n %s written to ttyUSB0",write_buffer);
printf("\n %d Bytes written to ttyUSB0", bytes_written);
printf("\n +----------------------------------+\n\n");
//tcflush(fd, TCIFLUSH); /* Discards old data in the rx buffer */
char read_buffer[32];
int bytes_read = 0;
int i = 0;
write(fd,write_buffer,sizeof(write_buffer));/* use write() to send data to port */
usleep ((8 + 25) * 100); /* Delay */
read(fd,&read_buffer,32); /* Read the data */
printf("\n\n Bytes Rxed -%d", bytes_read); /* Print the number of bytes read */
printf("\n\n ");
for(i=0;i<bytes_read;i++) /*printing only the received characters*/
printf("%c",read_buffer[i]);
printf("\n +----------------------------------+\n\n\n");
close(fd); /* Close the serial port */
}
Thanks for your help, Have a nice day. 谢谢您的帮助,祝您有愉快的一天。
You have a variable called bytes_read
that is initialized to 0
. 您有一个名为
bytes_read
的变量,该变量已初始化为0
。 Nothing in the code changes that value. 代码中的任何内容都不会更改该值。 – user3386109
– user3386109
The statement read(fd,&read_buffer,32)
should be corrected to bytes_read = read(fd, read_buffer, 32)
… – sawdust 语句
read(fd,&read_buffer,32)
应该更正为bytes_read = read(fd, read_buffer, 32)
…–锯末
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.