Sponsored Content
Top Forums Programming problem while having a communication with serial port????? Post 302142453 by arunchaudhary19 on Friday 26th of October 2007 08:52:04 AM
Old 10-26-2007
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<<"open successfully\n";

status = ioctl(sPort,TCSETS,&t);
if(status==0)
{

for(count=0;count<20;count++)


{
cout<<"Enter :";
cin>>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<<"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<<"Error in select";


//Activity happen on some ports
if(r > 0)
{
//check all

for(i=0;i<FD_SETSIZE;i++)
{

if(FD_ISSET(i,&testfds))
{
//Activity on serial port
if(i == sPortdest)
{


rbyte= read(sPortdest,recvbuf,1);

if(rbyte > 0)
{
recvbuf[1]='\0';
cout<<recvbuf;
count++;
if((count%11)==0)
{
cout<<"\n\t";
//c++;
cout<<c<<"\t";
cin>>inp_check;
// }
usleep(1000);
}
}
}
}
}

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

}

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


}
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Serial port communication

We're running SCO Unix Openserver 5.05 and I'm having trouble with serial communication between the com2 serial port and a handheld device. Downloading data from Unix to the handheld works perfectly, but the other way around creates a major problem. I don't know whether it's a buffer overflow or... (1 Reply)
Discussion started by: Aretha
1 Replies

2. UNIX for Advanced & Expert Users

Serial Communication in UNIX

Hi, I am working on serial communication on Unix. Can anyone guide me through it. The steps required for RS 232 communication and can i do serial communication even if i dont have super user previledges. Thankx, Vicky (1 Reply)
Discussion started by: Vicky
1 Replies

3. UNIX for Dummies Questions & Answers

serial communication

This isn't really a unix question, or even a programming question, but I hope you guys can help. I want to create a program to control the electricity on a model railway. I have created the program to that it sends characters over the serial cable, but now I need to do the switch that will... (1 Reply)
Discussion started by: KrazyGuyPaul
1 Replies

4. IP Networking

ip communication through serial connection

I have serial modem connection between two computers. We have to login the remote computer through ip address. How can I get the ip address in this connection? (1 Reply)
Discussion started by: pcsaji
1 Replies

5. Programming

Problem regarding serial port and multithreading

hello, I am creating a application in which I first open the serial port and then create the thread..for reading the data comming through that serial port. from that same thread I create another thread that is write thread means for writing on the serial port... I am continously polling again... (3 Replies)
Discussion started by: arunchaudhary19
3 Replies

6. UNIX for Dummies Questions & Answers

problem with serial port BSD

Hello all , i have change my system debian linux to freebsd( pcBSD) all working as well , but the serial does not work correctly. Under linux the problem does not appears. my problem is than my serial port does work . the first step than i have make is look with dmesg if my serial are... (0 Replies)
Discussion started by: pitbac23
0 Replies

7. Solaris

Serial port problem

I am working with solaris 9 sparc and I want to connect physical device in serial port but when I am connecting it,It is showing the error window saying-- So can any1 tell me the reason or is there any package I have to add to work with serial port??? (2 Replies)
Discussion started by: smartgupta
2 Replies

8. Programming

problem in coding for GSM interfacing using serial port

i am having some coding problem in c for interfacing the GSM module through serial port. i want to send/receive sms. i have done all the setting for the port and know the AT command to use in this but i am actually having problem in reading and writing from serial port how to handle the signal when... (1 Reply)
Discussion started by: harsh_it
1 Replies

9. Programming

Serial communication failing

edit: never mind; mods please delete. I finally got so frustrated I figured it couldn't be the code and it must be the device on the other end. After pulling it out of its cradle, I realized it was a different model number than the one I was using before, and wasn't accepting my... (1 Reply)
Discussion started by: DreamWarrior
1 Replies

10. SCO

Problem finding what is using a serial port

How can I determine what process is currently using a serial port? A good bit of google searching hasn't turned up anything useful, but it seems like there has to be a way to do this without too much difficulty. When I first started looking into this problem, I assumed that when a port was in... (2 Replies)
Discussion started by: jdsnatl
2 Replies
All times are GMT -4. The time now is 07:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy