[英]Reading and writing from serial port,through loopback in c in linux
I am on a program to read and write from and to serial port using serial crosscable and loopback by soring 2nd and 3rd pin of cable. 我正在使用通过电缆的第2和第3针对串行交叉电缆和环回进行读写操作的程序。 I am able to write but not able to read.
我能写,但不能读。 in read output it is showing 0 as the no.
在读取输出中,将0显示为否。 of bytes read by it.
它读取的字节数。 It is not showing error as -1 .
它没有显示错误为-1。
#include <stdio.h> // standard input / output functions
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitionss
#include <time.h> // time calls
#include <sys/ioctl.h>
int open_port(void)
{
int fd; // file description for the serial port
fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if(fd == -1) // if open is unsucessful
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
{
fcntl(fd, F_SETFL, 0);
}
printf("%d",fd);
return(fd);
}
int configure_port(int fd) // configure the port
{
struct termios port_settings; // structure to store the port settings in
cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);
port_settings.c_cflag |= ( CLOCAL | CREAD );
port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
tcflush( fd, TCIOFLUSH );
tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
return(fd);
}
int main()
{
int fd= open_port();
int d=configure_port(fd);
printf("%d",d);
int bytes;
char mk[10];
scanf("%s",&mk);
int w=write(fd,mk,strlen(mk));
int y=ioctl(fd,FIONREAD,&bytes);
printf("%d\n",w);
perror("write");
printf("%d",y);
char buffer[80];
char *data;
int nbytes;
data=buffer;
nbytes=read(fd,data,5);
printf("the outputis \n%d\n\n",nbytes);
perror("read");
while(nbytes > 0)
{printf("datmukun %d\n\n",nbytes);
data+=nbytes;
if (data[-1]=='\n'||data[-1]=='\r')
break;
}
return 0;
}
Depending on your TTY-driver the O_NDELAY
and O_NONBLOCK
can cause read
to behave in a non-blocking manner. 根据您的TTY驱动程序,
O_NDELAY
和O_NONBLOCK
可能导致read
以非阻塞方式运行。 Therefore, it is very likely that the data has not been received by the time you call read
. 因此,很可能在您调用
read
时尚未收到数据。 If you remove those flags, you should block in read
until at least one character is available. 如果删除这些标志,则应阻止
read
直到至少有一个字符可用。
you must wait for a while for the data to become available. 您必须等待一段时间才能使数据可用。 you can do this like the following:
您可以按照以下步骤进行操作:
sleep()
function inside it and possibly a counter to try for a number of times. sleep()
函数以及可能要尝试多次的计数器创建一个小的延迟,可以非常简单且仅出于测试目的。 select()
function on your file descriptor to let your process go to sleep for a while and get notified when the data is available or the timeout has reached. select()
函数,让您的进程进入休眠状态一段时间,并在数据可用或达到超时时得到通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.