LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2009, 11:35 PM   #1
leon.zcom
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Rep: Reputation: 0
Serial Port communication program always lose one byte data: 0x00


Hello everyone, I wrote a serial port communication program to access a equipment. coad as follow:

int main(void)
{
int fd = 0;
int nread = 0,i = 0,nwrite = 0, tmpread = 0, m = 0, n = 0 ;
char cmd_burst_data[7]={0xAA,0x55,0x11,0x00,0x00,0x77,0x77};
char recvBuf[512]={0};
char *precvBuf = recvBuf;
int xchData[50];
fd_set rd;
struct timeval timeout={0};
timeout.tv_sec=2;
timeout.tv_usec=0;

if( (fd=open_port(fd)) < 0 )
{
printf("open_port error\n");
return;
}

if( (i=set_opt(fd,19200,8,'N',1)) < 0)
{
printf("set_opt error\n");
}
while(m <3)
{

for(i=0;i <7;i++)
write(fd,&cmd_burst_data[i],1);

n = 0;
nread = 0;
precvBuf = recvBuf;
memset(precvBuf,0,sizeof(recvBuf));

while(n <3)
{
FD_ZERO(&rd);
FD_SET(fd,&rd);
select(fd+1,&rd,0,0,&timeout);
if(FD_ISSET(fd,&rd))
{
if(0 < (tmpread = read(fd,precvBuf,512)) )
{
for(i=0;i <tmpread;i++)
{
if( (precvBuf[i] == 0x77) && (precvBuf[i-1] == 0x77) )
goto out;
}
nread += tmpread;
precvBuf += tmpread;
}
}
n++;
}
m++;
}
out:
close(fd);
return;
}

// set port

static int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newio,oldio;

if( tcgetattr(fd,&oldio) != 0 )
{
printf("SetupSerial 1");
return -1;
}
bzero(&newio,sizeof(newio));

newio.c_cflag |= CLOCAL | CREAD;
newio.c_cflag &= ~CSIZE;

switch(nBits)
{
case 7:
newio.c_cflag |= CS7;
break;
case 8:
newio.c_cflag |= CS8;
break;
}

switch(nEvent)
{
case '0': //odd
newio.c_cflag |= PARENB;
newio.c_cflag |= PARODD;
newio.c_cflag |= (INPCK | ISTRIP);
break;
case 'E': //even
newio.c_cflag |= PARENB;
newio.c_cflag &= ~PARODD;
newio.c_cflag |= (INPCK | ISTRIP);
break;
case 'N': //none
newio.c_cflag &= ~PARENB;
break;
}

switch(nSpeed)
{
case 2400:
cfsetispeed(&newio,B2400);
cfsetospeed(&newio,B2400);
break;
case 9600:
cfsetispeed(&newio,B9600);
cfsetospeed(&newio,B9600);
break;
case 19200:
cfsetispeed(&newio,B19200);
cfsetospeed(&newio,B19200);
break;
case 115200:
cfsetispeed(&newio,B115200);
cfsetospeed(&newio,B115200);
break;
default:
cfsetispeed(&newio,B9600);
cfsetospeed(&newio,B9600);
break;
}

if( nStop == 1 )
newio.c_cflag &= ~CSTOPB;
else if(nStop == 2)
newio.c_cflag |= CSTOPB;

newio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

newio.c_oflag &= ~OPOST;
newio.c_iflag &= ~(IXON | IXOFF | IXANY);
newio.c_cc[VTIME] = 150;
newio.c_cc[VMIN] = 0;

tcflush(fd,TCIFLUSH);

if( (tcsetattr(fd,TCSANOW,&newio)) != 0 )
{
printf("com set error\n");
return -1;
}
printf("set done!\n");
return 0;
}

//open port
static int open_port(int fd)
{
fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if( -1 == fd )
{
printf("can't open serial prot\n");
return -1;
}

if( fcntl(fd,F_SETFL,0) < 0 )
printf("fcntl failed!\n");
else
printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));

printf("fd-open=%d\n",fd);
return fd;
}

normally,the received data should be: 0xAA,0x55,0x11,0xc0,0x0d,0xc0,0x10,0x20,0x30,0x00,0x00,0x00,0x08,0x77,0x77
my program always received these data by two times:
first read: 0xAA,0x55,0x11,0xc0,0x0d,0xc0,0x10,0x20,0x30,
second read: 0x00,0x00,0x00,0x08,0x77,0x77
but the problem came out: the second read always lose the first 0x00,
it read just 0x00,0x00,0x08,0x77,0x77.

If someone could fixed the problem , I really appreciate your help.
 
Old 12-14-2009, 09:24 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Have you verfied that that lost 0x00 isn't being read in the first read now instead, as in:

first read: 0xAA,0x55,0x11,0xc0,0x0d,0xc0,0x10,0x20,0x30,0x00
second read: 0x00,0x00,0x08,0x77,0x77
 
Old 12-14-2009, 07:00 PM   #3
leon.zcom
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Original Poster
Rep: Reputation: 0
I printed the output, it definitely lost 0x00 .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial port program not outputting the right data coffeecoffee Programming 2 04-11-2009 10:48 AM
serial port opening and closing byte derek_ou Programming 2 08-27-2008 11:14 AM
How to send byte from serial port neutron001 Linux - Software 9 03-31-2008 05:48 PM
Serial port communication.. brianbek Linux - Software 2 01-23-2006 12:43 PM
serial port communication vidyaraj Linux - Software 2 03-14-2004 11:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration