Help on serial communication

Amit Garg Amit Garg" <amass@sasken.com
Wed, 12 May 2004 18:15:15 +0530


--=_IS_MIME_Boundary
Content-Type: multipart/alternative;
	boundary="----=_NextPart_000_0178_01C4384D.12FE27E0"

This is a multi-part message in MIME format.

------=_NextPart_000_0178_01C4384D.12FE27E0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,

I am trying to communicate with a switch with a test program on NULL =
modem,
similar to minicom. I am facing problem in communicating with switch =
using my program but able to=20
communicate successfully with minicom.

I am able to receive some of the characters from switch (login prompt, =
password ),=20
but this is not consistent. (Not the same always). Please refer to the =
output of my=20
program attached at the end of this mail.

Appreciate if you could let me know what minicom is doing during =
initialization that enables it to succeed.
What initialization/configuration needs to be done in the test program =
so that it succeeds to communicate with the switch.

I am attaching the following information with this mail.

1. Test program output.
2.  Configuration of the serial port as done by my test program.
3.  Configuration as done by minicom.
4.  Source code of test program.

Thanks and Regards
Amit Garg


Test program output
----------------------------
1st run-----

Please press enter to continue...

 login fail!

Username: guest

2nd run----

Please press enter to continue...

aa=08 =08=08 =08aaa=08 =08=08 =08aa

3rd run----

Please press enter to continue...

Username:=20
Password: helloworld


Configuration of the serial port as done by my test program.
-------------------------------------------------------------------------=
----------------------
speed 9600 baud; rows 0; columns 0; line =3D 0;
intr =3D ^C; quit =3D ^\; erase =3D ^?; kill =3D ^U; eof =3D ^D; eol =3D =
<undef>;
eol2 =3D <undef>; start =3D ^Q; stop =3D ^S; susp =3D ^Z; rprnt =3D ^R; =
werase =3D ^W;
lnext =3D ^V; flush =3D ^O; min =3D 1; time =3D 0;
-parenb -parodd cs7 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl =
-ixon
-ixoff -iuclc -ixany imaxbel
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 =
vt0 ff0
-isig -icanon -iexten -echo echoe echok -echonl -noflsh -xcase -tostop =
-echoprt
echoctl echoke

Configuration as done by minicom
-------------------------------------------------------
speed 9600 baud; rows 0; columns 0; line =3D 0;
intr =3D ^C; quit =3D ^\; erase =3D ^?; kill =3D ^U; eof =3D ^D; eol =3D =
<undef>;
eol2 =3D <undef>; start =3D ^Q; stop =3D ^S; susp =3D ^Z; rprnt =3D ^R; =
werase =3D ^W;
lnext =3D ^V; flush =3D ^O; min =3D 1; time =3D 5;
-parenb -parodd cs7 hupcl -cstopb cread clocal crtscts
ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon =
-ixoff
-iuclc -ixany -imaxbel
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 =
bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop
-echoprt -echoctl -echoke

Source code of test program.
-----------------------------------------------

#include<stdio.h>
#include<fcntl.h>
#include<termios.h>
#include<sys/ioctl.h>

#define BAUD_RATE B9600

int main()
{
 int s0,i;
 char buffer,keybuff[100];
 int size,status,prev_status =3D 1;
=20
 FILE *ptr;
=20
 fd_set input;

 struct timeval timeout;
=20
 struct termios options;

 struct termios savetty,tty;


 s0 =3D open("/dev/ttyS0", O_RDWR );
=20
 if( s0 < 0 )
 {
  printf("Can't open the serial port \n");
  exit(0);
 }
=20
 tcgetattr(s0,&options);
 cfmakeraw(&options);

 cfsetispeed(&options,BAUD_RATE);
 cfsetospeed(&options,BAUD_RATE);
 =20
 options.c_cflag &=3D ~CSIZE;
 options.c_cflag |=3D CS8;
 options.c_cflag &=3D ~PARENB;
 options.c_cflag |=3D ( CLOCAL | CREAD );
 options.c_oflag |=3D OPOST;

 tcsetattr(s0,TCSANOW,&options);

 FD_ZERO( &input );
 FD_SET( s0, &input );
 FD_SET( 0, &input );
=20
    printf ("Please press enter to continue...\n");
 do {
  status =3D select(s0+1,&input,0,0,NULL);
  if( status < 0 )
  {
   printf("Error in select\n");
   exit(0);
  }
  else
  {
   if( FD_ISSET( s0 , &input ) )
   {
     if( read(s0,&buffer,1) < 0 )
       perror("Error writing to stdin\n");=20
     printf("%c",buffer);
     ptr=3Dfopen("out.txt","a");
     fputc(buffer,ptr);
     fclose(ptr);
     fflush(stdout);
   }
   if( FD_ISSET( 0, &input ) )
   {
    fgets(keybuff,100,stdin);

    if( write(s0,keybuff,strlen(keybuff)) < (strlen(keybuff)) )
     perror("S0 write error:\n");=20
   }
  }
 =20
  FD_ZERO( &input );
  FD_SET( s0, &input );
  FD_SET( 0, &input );

 } while( status > 0 );
=20
 tcdrain(s0);
 tcflush(s0,TCIOFLUSH);

 tcsetattr(0,TCSADRAIN,&savetty); =20
 close(s0);
 =20
 exit(0);
}


------=_NextPart_000_0178_01C4384D.12FE27E0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2722.900" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I am trying to communicate with a =
switch with a=20
test program on NULL modem,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>similar to&nbsp;minicom. </FONT><FONT =
face=3DArial=20
size=3D2>I am facing problem in communicating with switch using my =
program but=20
able to&nbsp;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>communicate&nbsp;successfully with=20
minicom.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I am able to receive some of =
the&nbsp;characters=20
from switch (login prompt, password ), </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>but this is not consistent. =
</FONT><FONT face=3DArial=20
size=3D2>(Not the same always). </FONT><FONT face=3DArial =
size=3D2>Please refer to the=20
output of my </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>program attached&nbsp;at the&nbsp;end =
of this=20
mail.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Appreciate if you could&nbsp;let me=20
know&nbsp;what&nbsp;minicom is doing during initialization that enables =
it to=20
succeed.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>What initialization/configuration needs =
to be done=20
in the test program so that it succeeds to communicate with the=20
switch.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I am attaching the following =
information with this=20
mail.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>1. Test program output.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>2.&nbsp; Configuration of the serial =
port as done=20
by my test program.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>3.&nbsp;&nbsp;Configuration as done by=20
minicom.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>4.&nbsp; Source code of test =
program.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Thanks and Regards</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Amit Garg</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><STRONG>Test program output</STRONG></DIV>
<DIV><STRONG>----------------------------</STRONG></DIV>
<DIV><FONT face=3DArial size=3D2>1st run-----</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Please press enter to =
continue...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;login fail!</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Username: guest</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>2nd run----</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Please press enter to =
continue...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>aa=08 =08=08 =08aaa=08 =08=08 =
=08aa</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>3rd run----</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Please press enter to =
continue...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Username: <BR>Password: =
helloworld</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>
<DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>Configuration of the serial =
port as done by=20
my test program.</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>
<DIV><STRONG>------------------------------------------------------------=
-----------------------------------</STRONG></DIV></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>speed 9600 baud; rows 0; columns 0; =
line =3D=20
0;<BR>intr =3D ^C; quit =3D ^\; erase =3D ^?; kill =3D ^U; eof =3D ^D; =
eol =3D=20
&lt;undef&gt;;<BR>eol2 =3D &lt;undef&gt;; start =3D ^Q; stop =3D ^S; =
susp =3D ^Z; rprnt=20
=3D ^R; werase =3D ^W;<BR>lnext =3D ^V; flush =3D ^O; min =3D 1; time =
=3D 0;<BR>-parenb=20
-parodd cs7 hupcl -cstopb cread clocal -crtscts<BR>-ignbrk -brkint =
-ignpar=20
-parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon<BR>-ixoff -iuclc =
-ixany=20
imaxbel<BR>opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 =
cr0 tab0=20
bs0 vt0 ff0<BR>-isig -icanon -iexten -echo echoe echok -echonl -noflsh =
-xcase=20
-tostop -echoprt<BR>echoctl echoke</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>Configuration as done by=20
minicom</STRONG></FONT></DIV>
<DIV><STRONG>
<DIV><STRONG>-------------------------------------------------------</STR=
ONG></DIV></STRONG></DIV>
<DIV>speed 9600 baud; rows 0; columns 0; line =3D 0;<BR>intr =3D ^C; =
quit =3D ^\;=20
erase =3D ^?; kill =3D ^U; eof =3D ^D; eol =3D &lt;undef&gt;;<BR>eol2 =
=3D &lt;undef&gt;;=20
start =3D ^Q; stop =3D ^S; susp =3D ^Z; rprnt =3D ^R; werase =3D =
^W;<BR>lnext =3D ^V; flush=20
=3D ^O; min =3D 1; time =3D 5;<BR>-parenb -parodd cs7 hupcl -cstopb =
cread clocal=20
crtscts<BR>ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr =
-icrnl=20
-ixon -ixoff<BR>-iuclc -ixany -imaxbel<BR>-opost -olcuc -ocrnl -onlcr =
-onocr=20
-onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0<BR>-isig -icanon -iexten =
-echo=20
-echoe -echok -echonl -noflsh -xcase -tostop<BR>-echoprt -echoctl=20
-echoke<BR><STRONG></STRONG></DIV></DIV></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2><STRONG>Source code of test=20
program.</STRONG></FONT></DIV>
<DIV>-----------------------------------------------</DIV>
<DIV>&nbsp;</DIV>
<DIV>#include&lt;stdio.h&gt;<BR>#include&lt;fcntl.h&gt;<BR>#include&lt;te=
rmios.h&gt;<BR>#include&lt;sys/ioctl.h&gt;</DIV>
<DIV>&nbsp;</DIV>
<DIV>#define BAUD_RATE B9600<BR></DIV>
<DIV>int main()<BR>{<BR>&nbsp;int s0,i;<BR>&nbsp;char=20
buffer,keybuff[100];<BR>&nbsp;int size,status,prev_status =3D=20
1;<BR>&nbsp;<BR>&nbsp;FILE *ptr;<BR>&nbsp;<BR>&nbsp;fd_set input;</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;struct timeval timeout;<BR>&nbsp;<BR>&nbsp;struct termios=20
options;</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;struct termios savetty,tty;</DIV>
<DIV>&nbsp;</DIV>
<DIV><BR>&nbsp;s0 =3D open("/dev/ttyS0", O_RDWR =
);<BR>&nbsp;<BR>&nbsp;if( s0 &lt;=20
0 )<BR>&nbsp;{<BR>&nbsp;&nbsp;printf("Can't open the serial port=20
\n");<BR>&nbsp;&nbsp;exit(0);<BR>&nbsp;}<BR>&nbsp;<BR>&nbsp;tcgetattr(s0,=
&amp;options);<BR>&nbsp;cfmakeraw(&amp;options);</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;cfsetispeed(&amp;options,BAUD_RATE);<BR>&nbsp;cfsetospeed(&amp=
;options,BAUD_RATE);<BR>&nbsp;&nbsp;<BR>&nbsp;options.c_cflag=20
&amp;=3D ~CSIZE;<BR>&nbsp;options.c_cflag |=3D =
CS8;<BR>&nbsp;options.c_cflag &amp;=3D=20
~PARENB;<BR>&nbsp;options.c_cflag |=3D ( CLOCAL | CREAD=20
);<BR>&nbsp;options.c_oflag |=3D OPOST;</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;tcsetattr(s0,TCSANOW,&amp;options);</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;FD_ZERO( &amp;input );<BR>&nbsp;FD_SET( s0, &amp;input=20
);<BR>&nbsp;FD_SET( 0, &amp;input );<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp; =
printf=20
("Please press enter to continue...\n");<BR>&nbsp;do =
{<BR>&nbsp;&nbsp;status =3D=20
select(s0+1,&amp;input,0,0,NULL);<BR>&nbsp;&nbsp;if( status &lt; 0=20
)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;printf("Error in=20
select\n");<BR>&nbsp;&nbsp;&nbsp;exit(0);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp=
;else<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if(=20
FD_ISSET( s0 , &amp;input )=20
)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(=20
read(s0,&amp;buffer,1) &lt; 0=20
)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("Error writing to=20
stdin\n");=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%c",buffer);<BR>&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;ptr=3Dfopen("out.txt","a");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;fputc(buffer,ptr);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose(ptr);<BR>&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;fflush(stdout);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nb=
sp;&nbsp;&nbsp;if(=20
FD_ISSET( 0, &amp;input )=20
)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;fgets(keybuff,100,std=
in);</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;if( write(s0,keybuff,strlen(keybuff)) &lt;=20
(strlen(keybuff)) )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("S0 write=20
error:\n");&nbsp;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;=
<BR>&nbsp;&nbsp;FD_ZERO(=20
&amp;input );<BR>&nbsp;&nbsp;FD_SET( s0, &amp;input =
);<BR>&nbsp;&nbsp;FD_SET( 0,=20
&amp;input );</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;} while( status &gt; 0=20
);<BR>&nbsp;<BR>&nbsp;tcdrain(s0);<BR>&nbsp;tcflush(s0,TCIOFLUSH);</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;tcsetattr(0,TCSADRAIN,&amp;savetty);&nbsp;&nbsp;<BR>&nbsp;clos=
e(s0);<BR>&nbsp;&nbsp;<BR>&nbsp;exit(0);<BR>}<BR></DIV></FONT></DIV></BOD=
Y></HTML>

------=_NextPart_000_0178_01C4384D.12FE27E0--

--=_IS_MIME_Boundary
Content-Type: text/plain;charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

***********************************************************************

********************************************************************

SASKEN BUSINESS DISCLAIMER

This message may contain confidential, proprietary or legally Privileged information. In case you are not the original intended Recipient of the message, you must not, directly or indirectly, use, Disclose, distribute, print, or copy any part of this message and you are requested to delete it and inform the sender. Any views expressed in this message are those of the individual sender unless otherwise stated. Nothing contained in this message shall be construed as an offer or acceptance of any offer by Sasken Communication Technologies Limited ("Sasken") unless sent with that express intent and with due authority of Sasken. Sasken has taken enough precautions to prevent the spread of viruses. However the company accepts no liability for any damage caused by any virus transmitted by this email.

***********************************************************************

--=_IS_MIME_Boundary--