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; } |
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. |
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; 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. |
Quote:
can you plz suggest me with sample code? |
All times are GMT -5. The time now is 07:01 AM. |