开发者

Reading and writing from serial port,through loopback in c in linux

开发者 https://www.devze.com 2023-04-10 14:49 出处:网络
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. I am able to write but not able to read.

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. I am able to write but not able to read. in read output it is showing 0 as the no. of bytes read by it. It is not showing error as -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开发者_C百科)      // 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. Therefore, it is very likely that the data has not been received by the time you call read. If you remove those flags, you should block in read until at least one character is available.


you must wait for a while for the data to become available. you can do this like the following:

  • very simply and just for test purpose by creating a small delay using a while loop and a sleep() function inside it and possibly a counter to try for a number of times.
  • you can use 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.
0

精彩评论

暂无评论...
验证码 换一张
取 消