problem while having a communication with serial port?????

hello,

I am gettin problem while sending and recieving data through seial port...
when I am sending Data then the reciever end is not able to recieve that data .....
Reciever end is running in infinite loop just polling after some time to check that there is data on the port and then again sleeps down....

whats going wrong ....I am not getting data at the recievers End ....
here I am copying my code of both ends..........

SENDER(just writing data on Serial Port):::::::::

#include<sys/types.h>
#include<iostream.h>
#include<termios.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<iostream.h>
#include<unistd.h>

main ()
{

struct termios t;
char *device = "/dev/ttyS0";

int sPort, rbyte, status, sPortdest,length;
long count=0;

unsigned char send1[32];

t.c_cc[VMIN] = 1;
t.c_cc[VTIME]=0;
t.c_cc[VEOF]='\n';

/* t.c_iflag &= ~(BRKINT|IGNPAR|PARMRK|INPCK|INLCR|IGNCR|ICRNL|IXON);
t.c_iflag |= (IGNBRK|ISTRIP|IXOFF);
t.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP);
t.c_cflag &= (CSIZE|CSTOPB|HUPCL|PARENB);
t.c_cflag |= (CLOCAL|CREAD|CS8);*/

t.c_iflag |= (ISIG);
// t.c_iflag &= ~BRKINT;
// t.c_iflag &= (IUCLC);
t.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;

sPort = open\(device,O\_RDWR|O\_NOCTTY|O_NDELAY\);



if\(sPort != -1\)
\{
	cout&lt;&lt;"open successfully\\n";
	
	status = ioctl\(sPort,TCSETS,&t\);
	if\(status==0\)
      	\{
	     
		for\(count=0;count&lt;20;count\+\+\)

		
		\{   
			cout&lt;&lt;"Enter :";
			cin&gt;&gt;send1;
                           //strcat\(\(char *\)send1,"\{MSS TYPE-D DAMA\}"\);
write\(sPort,"\{100223456\}",11\);		

// rbyte=write(sPort,send1,strlen((char *)send1));
cout<<"w b:"<<rbyte<<"\t Time:"<<count<<"\t string:"<<send1<<"\n";
usleep(1000);

		\}	

	  \}
	  close\(sPort\);
		
\}
else
\{
	cout&lt;&lt;"Device could not be opened";
\}

}

RECIEVER(just reading Data on serial port....)

#include<termios.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<stdio.h>
#include<sys/time.h>
#include<iostream.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>

main ()
{

struct termios t;
char *devicedest = "/dev/ttyS0";

unsigned char recvbuf[2];
int rbyte, status, sPortdest,r,i,c=0;
long count=0;

int inp_check=0;
int ifirst=0;

struct timeval timeout;
fd_set testfds,readfds;

t.c_cc[VMIN] = 1;
t.c_cc[VTIME]=0;
t.c_cc[VEOF]='\n';
/* t.c_iflag &= ~(BRKINT|IGNPAR|PARMRK|INPCK|INLCR|IGNCR|ICRNL|IXON);
t.c_iflag |= (IGNBRK|ISTRIP|IXOFF);
t.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP);
t.c_cflag &= (CSIZE|CSTOPB|HUPCL|PARENB);
t.c_cflag |= (CLOCAL|CREAD|CS8);*/
// t.c_iflag |= (ISIG);
// t.c_iflag &= ~(IUCLC);

t.c_iflag &= ~BRKINT;
t.c_iflag = (ISIG | IUCLC) ;
t.c_cflag = B9600 | CS8 | CREAD | CLOCAL| HUPCL;

sPortdest= open(devicedest,O_RDONLY|O_NOCTTY|O_NDELAY);
if(tcsetattr(sPortdest,TCSANOW,&t) != 0 )
cout<<"\n attribute not set";

FD_ZERO\(&readfds\);
FD_SET\(sPortdest,&readfds\);
   
   /* if\(ifirst == -1\)  //Set all attribute when first start
    \{
          status = ioctl\(sPortdest,TCSETS,t\);
          ifirst = 1;
    \}*/

if(sPortdest!=-1)
{
cout<<"Open successfully\n";
do
{
testfds = readfds;

timeout.tv_sec=0;
timeout.tv_usec=1000;

r=select\(FD\_SETSIZE,&testfds,\(fd_set *\)0,\(fd_set *\)0,&timeout\);

if\(r == -1\)
	cout&lt;&lt;"Error in select";	


						//Activity happen on some ports
if\(r &gt; 0\)
\{	
						//check all 
	
	for\(i=0;i&lt;FD_SETSIZE;i\+\+\)
	\{	
		
		if\(FD_ISSET\(i,&testfds\)\)
		\{
						//Activity on serial port
			if\(i == sPortdest\)
			\{
				
				
				rbyte= read\(sPortdest,recvbuf,1\);
    				
				if\(rbyte &gt; 0\)
	      			\{
		       			recvbuf[1]='\\0';
					cout&lt;&lt;recvbuf;
					count\+\+;
					if\(\(count%11\)==0\)
					\{
						cout&lt;&lt;"\\n\\t";
	 				//c\+\+;
						cout&lt;&lt;c&lt;&lt;"\\t";
						cin&gt;&gt;inp_check;
				//	\}
					usleep\(1000\);	 
				\}	
			\}	
		\}
	\}
\}	

						//Activity not happen on SP
if\(r == 0\)
\{
	//cout&lt;&lt;"\\nwaiting\\n";
	write\(sPortdest,"Test",4\);
	usleep\(1000\);

\}	

}while(1);
}
else
{
cout<<"Device could not be oepn successfully";
}

}

When writing communications code, start with some known methods for testing.

Rather than tear your hair out do the following....

  1. ensure machines are connected, with null modem if required

  2. run terminal emulator on those serial ports at both ends

  3. confirm terminal emulators exchange characters

  4. replace the send end with your sending program and check that the sender does send.

  5. stop that, and restart the terminal emulator

  6. stop the terminal emulator at the receiving end, start your receiving program

  7. confirm receiving program receives what ever is typed.

hi...
but where to find terminal emulator....
what it is in the system????
I am using Fedora 6 .....
thanks

have you tried minicom?

but I didnt have minicom in my system..........
then can I test with any other tool.........
or from where I acn get minicom???//

I am trying with minicom....
but its status is showing offline..........
CTRL-A Z for help | 9600 8N1 | NOR | Minicom 2.1 | VT102 | Offline
this line is seen at the bottom of minicom...........
please help how to send and recieve data through minicom.......
thanks & regards,

Yes, mine says offline, I think it refers to successful modem dialup.

but how to dial up modem.........
I had no idea about how to dial.......
how to make it online?????????

You don't have to.

Go through the configuration and remove the modem dialing entries.

Hi again,

I spoke with Keyspan and they assured me the USB to Serial device was working if I could use the Screen tool.

I asked why I was unable to redirect stdin/stdout to and from the /dev/tty. device and was told there was no reason I shouldn't be able to do so.

I have tried:

\#echo �A� &gt; /dev/ tty.USA28Xb2P1.1

The terminal window just hangs.

I have also tried #chat and #efax with no luck...

Has anyone had any luck with serial port communication from any other shell scripting language on a unix/linux box?

Thanks!

Corey

Subject: [nyphp-talk] Serial communication in Mac OS X
To: <talk@lists.nyphp.org>
Message-ID: <C42C2CE7.59796%corey@bmfenterprises.com>
Content-Type: text/plain; charset="us-ascii"

Hi All,

I am trying to connect to /dev/tty.USA28Xb2P1.1 which is a Keyspan USB to
Serial adapter. I have had success using the screen utility, #screen
/dev/tty.USA28Xb2P1.1 but I would like to use PHP to create a web interface
to a microcontroller.

I have attempted fopen with no luck:

> $port = "/dev/tty.USA28Xb2P1.1";
>
> $p = fopen($port, 'r+');
> $read = fread($p, filesize($port));
> fclose($p);
>
> echo $read;
>
> $p = fopen($port, 'r+');
> fwrite($p, "A");
> fclose($p);

I have tried SerProxy but cannot get a connection through telnet. I have
also tried LibSerial but could only find version 0.5.2 which does not yet
support PHP apparently... I have read about IOKit but I am not sure it will
get me where I want to be.

Any help would be greatly appreciated.

Thank you!

Corey