LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   serial communication through rs232 in linux c (https://www.linuxquestions.org/questions/linux-newbie-8/serial-communication-through-rs232-in-linux-c-4175446704/)

ynmly 01-22-2013 03:05 AM

serial communication through rs232 in linux c
 
hi all,
i am new in linux, i am facing problem in serial communication
actually i have two computer on one hyper terminal is running and on another i am writing c program to communicate with hyper terminal.both PC are connected through rs232.
i have written some code and i am able to send data to hyper terminal.but i am not getting how i can read data from hyper terminal and write in linux PC.
any suggestion?
thanks in advance.

Below are my code

int main(int argc, char **argv)
{
struct termios options;
int fd;

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1)
{
printf("Could not open port /dev/ttyS0\n");
return 1;
}

tcgetattr(fd, &options); //get port options

cfsetispeed(&options, B9600); //set input baud rate
cfsetospeed(&options, B9600); //set output baud rate

options.c_cflag |= (CLOCAL | CREAD); //enable receiver and set local mode

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS; //disable hardware flow control
options.c_cflag &= ~(ICANON | ECHO | ISIG); //set raw input

options.c_cflag |= IXOFF; //disable software flow control

tcsetattr(fd, TCSANOW, &options); //set new port options

sleep(1);
int rc,count;
int size = 8;
unsigned char buf[10];
FILE *fp = NULL;
char ch;
int i=0;
printf("enter the data you want to send");

while((ch=getchar())!='\n')
{
write(fd,&ch,1);
}
close(fd);

printf("Finished.\n");

return 0;
}

jpollard 01-22-2013 09:02 PM

You have nothing there to read from the terminal.

Note - if you expect asynchronous input/output, you have to either use separate threads, or separate processes.

If you can write to the device as you say, your setup is done. All you need is a read(fd,...) loop instead of a write.

suicidaleggroll 01-22-2013 09:15 PM

It's not simultaneous I/O, but I implemented this setup not too long ago for a system that would send commands to a device, then read the response to check for an "OK" or "ER" response:
Code:

  fstep = (2.0/ssclk)*swpsp/1e6;
  sprintf(buffw,"fd %14.12lf\n",fstep);
  write(sfd,buffw,18);
  read(sfd,buffr,20);
  if (buffr[0] != 'O' && buffr[1] != 'K') {
    fprintf(stderr,"Communication error with synthesizer\n");
    fprintf(stderr,"Response to fd command was %c%c\n",buffr[0],buffr[1]);
    exit(0);
  }
  sleep(1);

and so on to the next command

buffw and buffr are of course two character buffers, and sfd is the file descriptor for the serial device.

Note that I am in NO way shape or form a decent C programmer. I'm an IDL/Fortran guy who can fudge my way through C to get something done, so there is most likely a much better way to implement some of what I was doing. However, it may give you some insight into a possible way to read from the serial device. If you spawn the read in a separate thread from your write, you should be able to get more-or-less simultaneous read/write capability.

ynmly 01-23-2013 02:17 AM

Quote:

Originally Posted by jpollard (Post 4875826)
You have nothing there to read from the terminal.

Note - if you expect asynchronous input/output, you have to either use separate threads, or separate processes.

If you can write to the device as you say, your setup is done. All you need is a read(fd,...) loop instead of a write.

thanks jpollard for your response ....that what i have given you is able to do communication with hyper terminal. i.e one way communication.when i run this code i am able to send data from linux box to hyper terminal...i am facing problem how to get data when i send data from hyper terminal to linux PC
can you plz suggest me with sample code?


All times are GMT -5. The time now is 07:01 AM.