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> </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 minicom. </FONT><FONT =
face=3DArial=20
size=3D2>I am facing problem in communicating with switch using my =
program but=20
able to </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>communicate successfully with=20
minicom.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>I am able to receive some of =
the 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 at the end =
of this=20
mail.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Appreciate if you could let me=20
know what 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> </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> </DIV>
<DIV><FONT face=3DArial size=3D2>1. Test program output.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>2. Configuration of the serial =
port as done=20
by my test program.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>3. Configuration as done by=20
minicom.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>4. Source code of test =
program.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </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> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </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> </DIV>
<DIV><FONT face=3DArial size=3D2>Please press enter to =
continue...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> login fail!</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Username: guest</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>2nd run----</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Please press enter to =
continue...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </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> </DIV>
<DIV><FONT face=3DArial size=3D2>3rd run----</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Please press enter to =
continue...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Username: <BR>Password: =
helloworld</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </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
<undef>;<BR>eol2 =3D <undef>; 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> </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 <undef>;<BR>eol2 =
=3D <undef>;=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> </DIV>
<DIV>#include<stdio.h><BR>#include<fcntl.h><BR>#include<te=
rmios.h><BR>#include<sys/ioctl.h></DIV>
<DIV> </DIV>
<DIV>#define BAUD_RATE B9600<BR></DIV>
<DIV>int main()<BR>{<BR> int s0,i;<BR> char=20
buffer,keybuff[100];<BR> int size,status,prev_status =3D=20
1;<BR> <BR> FILE *ptr;<BR> <BR> fd_set input;</DIV>
<DIV> </DIV>
<DIV> struct timeval timeout;<BR> <BR> struct termios=20
options;</DIV>
<DIV> </DIV>
<DIV> struct termios savetty,tty;</DIV>
<DIV> </DIV>
<DIV><BR> s0 =3D open("/dev/ttyS0", O_RDWR =
);<BR> <BR> if( s0 <=20
0 )<BR> {<BR> printf("Can't open the serial port=20
\n");<BR> exit(0);<BR> }<BR> <BR> tcgetattr(s0,=
&options);<BR> cfmakeraw(&options);</DIV>
<DIV> </DIV>
<DIV> cfsetispeed(&options,BAUD_RATE);<BR> cfsetospeed(&=
;options,BAUD_RATE);<BR> <BR> options.c_cflag=20
&=3D ~CSIZE;<BR> options.c_cflag |=3D =
CS8;<BR> options.c_cflag &=3D=20
~PARENB;<BR> options.c_cflag |=3D ( CLOCAL | CREAD=20
);<BR> options.c_oflag |=3D OPOST;</DIV>
<DIV> </DIV>
<DIV> tcsetattr(s0,TCSANOW,&options);</DIV>
<DIV> </DIV>
<DIV> FD_ZERO( &input );<BR> FD_SET( s0, &input=20
);<BR> FD_SET( 0, &input );<BR> <BR> =
printf=20
("Please press enter to continue...\n");<BR> do =
{<BR> status =3D=20
select(s0+1,&input,0,0,NULL);<BR> if( status < 0=20
)<BR> {<BR> printf("Error in=20
select\n");<BR> exit(0);<BR> }<BR>  =
;else<BR> {<BR> if(=20
FD_ISSET( s0 , &input )=20
)<BR> {<BR> if(=20
read(s0,&buffer,1) < 0=20
)<BR> perror("Error writing to=20
stdin\n");=20
<BR> printf("%c",buffer);<BR> &nb=
sp; ptr=3Dfopen("out.txt","a");<BR> &nb=
sp;fputc(buffer,ptr);<BR> fclose(ptr);<BR>&n=
bsp; fflush(stdout);<BR> }<BR>&nb=
sp; if(=20
FD_ISSET( 0, &input )=20
)<BR> {<BR> fgets(keybuff,100,std=
in);</DIV>
<DIV> </DIV>
<DIV> if( write(s0,keybuff,strlen(keybuff)) <=20
(strlen(keybuff)) )<BR> perror("S0 write=20
error:\n"); <BR> }<BR> }<BR> =
<BR> FD_ZERO(=20
&input );<BR> FD_SET( s0, &input =
);<BR> FD_SET( 0,=20
&input );</DIV>
<DIV> </DIV>
<DIV> } while( status > 0=20
);<BR> <BR> tcdrain(s0);<BR> tcflush(s0,TCIOFLUSH);</DIV>
<DIV> </DIV>
<DIV> tcsetattr(0,TCSADRAIN,&savetty); <BR> clos=
e(s0);<BR> <BR> 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--