regarding serial ports

From: emb in linux (haranath.t_at_gmail.com)
Date: 11/15/05


Date: 15 Nov 2005 05:24:50 -0800

hai all,
Here i want to communicate with two serial ports interactivelly.(i.e)If
i send a byte to com1 i have to receive in com2 correpondingly.Here I
am getting the subsequent bytes but in between i am getting some junk
charecters too.(For that i have connected a null modem cable in
between com1 and com2.)

For clear Understanding i am giving my transmitting and receving code.

// Transmitting code

#include <stdio.h> /* Standard input/output definitions */
#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 definitions */

int
main()
{
  int fd,c,i;
  struct termios options;
  char c, buff[10];
  fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY );
  if (fd == -1)
 perror("Unable to open /dev/ttyS0\n");
 else
 {
  fprintf(stdout,"enter any byte");
  fgets(buff,sizeof(buff),stdin);
  fcntl(fd, F_SETFL, 0);
  tcgetattr(fd, &options);
  cfsetispeed(&options,B115200);
  cfsetospeed(&options,B115200);
  options.c_cflag |= (CLOCAL | CREAD);
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_cflag |=(ICANON | ECHO | ECHOE);
  options.c_oflag |=OPOST;
  tcsetattr(fd, TCSANOW, &options);
  while((c=fgetc(stdin)!=EOF)
{
  buff[i]=c;
  if(write(fd,buff,1)<0)
  fprintf(stderr,"Write() failed!\n");
   i++;
  fputc(buff,stdout);
}
  close (fd);
 }
 return (0);
 }

output:
enter any byte:a
 enter any byte:b
 enter any byte:c
it will close whenever we give ctrl+d

//Receiving

#include <stdio.h> /* Standard input/output definitions */
#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 definitions */

  int main()
{
 int fd,i=0;
  char buff[10];
  struct termios options;
  fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY );
  if (fd == -1)
  perror("Unable to open /dev/ttyS1\n");
  else
  {
    fcntl(fd, F_SETFL, 0);
     tcgetattr(fd, &options);
     cfsetispeed(&options,B115200);
     cfsetospeed(&options,B115200);
     options.c_cflag |= (CLOCAL | CREAD);
     options.c_cflag &= ~PARENB;
     options.c_cflag &= ~CSTOPB;
     options.c_cflag &= ~CSIZE;
     options.c_cflag |= CS8;
     options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
     options.c_oflag &= ~OPOST;
     options.c_cc[VMIN]=1;
     options.c_cc[VTIME]=10;
     tcsetattr(fd, TCSANOW, &options);
     fprintf(stdout,"In server\n");
     while(1)
     {
     if (read(fd,buff,10)<0)
     fprintf(stderr,"Read() failed!\n");
     fputs(buff,stdout);
     }
     close (fd);
   }
    return (0);
    }

output:a
           [JUNK ----------------------->]b
           [JUNK------------------------->]c
here i want to clear the buffer after receiving 1st byte.so that i can
put that function in loop.Is there any function to do that?
Hope i explaned clearly.If you have any guide other than posix
standard,please give me the url.
Thanks in advance.

                  With Regards

                   emb in linux.



Relevant Pages

  • Re: serial port interfaced EEPROM reader no longer detected after temporary removal
    ... I wonder if the original instance of COM1 got set to be COM2? ... In the advanced options I looked at the possibility to rename the port, and noticed that COM2 has behind it, but as far as I know I don't have COM2 on my system. ... The manufacturer of the EEPROM reader tells me that the software scans for available serial ports and you can set one that is available. ...
    (microsoft.public.windowsxp.hardware)
  • Re: serial port interfaced EEPROM reader no longer detected after temporary removal
    ... But the craziest thing is that nowhere do I see COM2 except in the rename option for COM1. ... In the advanced options I looked at the possibility to rename the port, and noticed that COM2 has behind it, but as far as I know I don't have COM2 on my system. ... The manufacturer of the EEPROM reader tells me that the software scans for available serial ports and you can set one that is available. ...
    (microsoft.public.windowsxp.hardware)
  • Re: FTP server comprimised
    ... Com1, Com2 etc are reserved names hence why you were prevented from ... This appears to be a favorit trick of ... the server for others to download. ...
    (microsoft.public.win2000.security)
  • COM equality testing
    ... the window object shows the same ... Dim ie, com1, com2, doc, wnd, div, out ... same, diff, diff, diff, diff, diff ...
    (microsoft.public.scripting.vbscript)
  • Re: Reading names of comports [COM1, COM2 etc]
    ... I want to see COM1, COM2 etc. ... // WICCOMPORTERROR of an error is detected ... In my Delphi 5 help file, there is some example code demonstrating how to use the results of that function to get the data you really need. ...
    (alt.comp.lang.borland-delphi)