LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Serial Port communication program always lose one byte data: 0x00 (https://www.linuxquestions.org/questions/programming-9/serial-port-communication-program-always-lose-one-byte-data-0x00-775463/)

leon.zcom 12-13-2009 11:35 PM

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.

estabroo 12-14-2009 09:24 AM

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

leon.zcom 12-14-2009 07:00 PM

I printed the output, it definitely lost 0x00 .


All times are GMT -5. The time now is 03:23 PM.