Skip navigation.
Home
The QNX Community Portal

View topic - Serial port reading data problem

Serial port reading data problem

For discussion of realtime and/or embedded programming.

Serial port reading data problem

Postby Ericxx » Mon Apr 06, 2009 4:34 pm

Hello, I am now using the QNX6.3.2 to read data from a serial port connected to a peripheral device.
The procedure is like this: i first send a request packet, the hardware should return the responding packet, and then read them into the buffer. And the data length each time i read should be 30 bytes. This is usually the case. But sometimes, the data bytes i could read from the serial port is less than 30 bytes, (I have given enough time for the hardware processing part to render the corresponding packet). And for the lost bytes, they are not the last few bytes in the packet. Instead, the bytes lost usually lie some random positions of the correct packet. what might be the possible reasons?

Thanks,
Eric
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Postby Tim » Mon Apr 06, 2009 11:00 pm

Eric,

Can you post your read code. That includes where you read from the serial port and where you place those bytes along with the code that processes those bytes.

Tim
Tim
Senior Member
 
Posts: 1388
Joined: Wed Mar 10, 2004 12:28 am

Postby juanplacco » Tue Apr 07, 2009 3:04 pm

How often the program fails?. Do you flush the input or output buffers after reading or writing?. Port configuration, flags, etc.? Hardware?
juanplacco
Senior Member
 
Posts: 318
Joined: Wed May 17, 2006 2:30 pm

Postby Ericxx » Tue Apr 07, 2009 6:36 pm

The read and write operations are done in a seperate thread. Codes as follows:

m_nsIM6= ::open("/dev/ser3", O_RDWR);
termios term;
tcgetattr(m_nsIM6, &term);
cfsetispeed(&term, 57600); //input and output baudrate
cfsetospeed(&term, 57600);
term.c_cflag = CS8 | CLOCAL | CREAD;
tcsetattr(m_nsIM6, TCSANOW, &term);

int nRead = read(m_nsIM6, m_szBuffer, 4096); // char m_szBuffer[4096]
/* ... some processing for the data received, could be igored,
since the problem still exist without this processing part .. */
the write(request) operation;

the read and write keep working with a period of 20ms, and the data length replied is 37 bytes for each request. Thanks for your suggestions.


Best
Eric
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Postby Ericxx » Tue Apr 07, 2009 6:37 pm

juanplacco wrote:How often the program fails?. Do you flush the input or output buffers after reading or writing?. Port configuration, flags, etc.? Hardware?


roughly like 1 bad packets every 30 correct packets, but the frequency is not so determined. No flush after the serial port initialization.
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Postby mario » Tue Apr 07, 2009 7:11 pm

Are you sure the bytes are transited correctly?
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Postby juanplacco » Tue Apr 07, 2009 8:21 pm

Check if serial port's status change in some moment... you can use stty -a < /dev/serX command.
juanplacco
Senior Member
 
Posts: 318
Joined: Wed May 17, 2006 2:30 pm

Postby Tim » Tue Apr 07, 2009 9:28 pm

Eric,

Ericxx wrote:The read and write keep working with a period of 20ms, and the data length replied is 37 bytes for each request. Thanks for your suggestions.


So each reply packet is a fixed 37 bytes. How big is the send packet? Is it also 37 bytes?

One other question, is your serial link entirely full duplex RS232 or is there any single duplex RS232 or RS485 involved here? I ask because it's possible the timing might be VERY tight on sending/receiving that much data on single duplex serial lines and thus you could get some corruption.

Since you didn't post exact code, it's hard to see if you have any coding mistakes that might explain the problem. For example are you memsetting your receive buffer to all 0's before doing the read() to ensure you don't have left over data in the read buffer?

If you don't have RS485 or a single duplex serial line, I'd try to code a much simpler example that uses no threads. Basically, you send your packet and then wait for a reply. You can time how long you wait for a response to make sure it's less than 20 ms and print an error if it's not. I'd make sure that works 100% reliably before going any further:

main
{
m_nsIM6= ::open("/dev/ser3", O_RDWR);
termios term;
tcgetattr(m_nsIM6, &term);
cfsetispeed(&term, 57600); //input and output baudrate
cfsetospeed(&term, 57600);
term.c_cflag = CS8 | CLOCAL | CREAD;
tcsetattr(m_nsIM6, TCSANOW, &term);

while(1)
{
write(yourPacket)
clock_getTime(beforeTime);
int nRead = read(m_nsIM6, m_szBuffer, 4096); // char m_szBuffer[4096]
clock_getTime(afterTime);
if (afterTime - beforeTime > 20)
{
print ("took too long to reply");
}
}
}


Tim
Tim
Senior Member
 
Posts: 1388
Joined: Wed Mar 10, 2004 12:28 am

Postby Ericxx » Wed Apr 08, 2009 5:46 am

Code list:
----------------------------------------------------------------------------------------------------------
uint64_t _cycle0; //initial clock cycle as the application start
uint64_t _cps = SYSPAGE_ENTRY(qtime)->cycles_per_sec; //cycles in one second
uint64_t _cpms = (uint64_t)(0.001*_cps);

inline double GetTime() { return (double)(ClockCycles()-_cycle0)/_cps; }


int main(int argc, char *argv[])
{
int nsIMU = open("/dev/ser1", O_RDWR);
termios term;
tcgetattr(nsIMU, &term);
cfsetispeed(&term, 57600); //input and output baudrate
cfsetospeed(&term, 57600);
term.c_cflag = CS8 | CLOCAL | CREAD;
tcsetattr(nsIMU, TCSANOW, &term);

char szBuffer[4096];
char bufAH[] = {0x55,0x55,0x47,0x50,0x02,0x41,0x30,0x94,0x87}; // A0
::write(nsIMU, bufAH, 9);
while(1)
{
double t1 = ::GetTime();
usleep(15000);
int nRead = read(nsIMU, szBuffer, 4096);
if (nRead>0)
{
for (int i=0;i<nRead;i++)
{
printf("%02x ", (unsigned char)szBuffer[i]);
}
cout<<endl;
cout<<"Time used: "<<::GetTime()-t1<<endl;
}
else
cout<<"No packets"<<endl;

::write(nsIMU, bufAH6, 9);
}
return 0;
}
----------------------------------------------------------------------------------------------------------
Since the hardware needs around 15ms to accept the request packet and render the corresponding packet, so 20ms is long enough (i used 15ms in the above code to wait for the response). And following is the output: obviously, most of the time, the packets are 37 bytes. But some are less than 37.
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Postby Ericxx » Wed Apr 08, 2009 5:51 am

Sorry, partial output of the test. Thanks.

Time used: 0.0167424
55 55 41 30 1e 00 ce 00 37 3a 52 ff ff 00 06 00 00 00 0f ff bc f3 2c 01 84 f7 13 f2 7b 22 c8 00 00 03 0d 3b d7
Time used: 0.0167591
55 55 41 30 1e 00 cd 00 37 3a 52 ff fa 00 01 ff fd 00 10 ff bb f3 2c 01 85 f7 12 f2 75 22 c8 00 00 03 0d 1a 37
Time used: 0.0167123
55 55 41 30 1e 00 cd 00 37 3a 52 00 02 00 01 00 04 00 10 ff ba f3 2c 01 16 f7 21 f2 8b 22 c8 00 00 03 0d 02 d2
Time used: 0.0167551
55 55 41 30 1e 00 cd 00 37 3a 50 ff fe ff ff ff fd 00 11 ff bb f3 2d 01 18 f7 2f f2 90 22 c8 00 00 03 0d 36 f3
Time used: 0.016743
55 55 41 00 37 3a 4f 00 04 00 05 ff f6 00 11 ff bd f3 2e 01 17 f7 2a f2 92 22 c8 00 00 03 0d ad 6a
Time used: 0.0167269
55 55 41 30 1e 00 ce 00 37 3a 4f 00 02 ff f9 ff ff 00 11 ff c2 f3 2e 01 14 f7 26 f2 91 22 c8 00 00 03 0d ef f2
Time used: 0.0167416
55 55 41 30 1e 00 ce 00 37 3a 4f ff fd 00 00 00 02 00 11 ff c4 f3 2e 01 86 f7 15 f2 81 22 c8 00 00 03 0d 5d 6f
Time used: 0.0167433
55 55 41 30 1e 00 ce 00 37 3a 4f ff fd 00 04 ff fe 00 10 ff c7 f3 2e 01 86 f7 16 f2 76 22 c8 00 00 03 0d 56 33
Time used: 0.016709
55 55 41 30 1e 00 cd 00 37 3a 4f 00 00 00 03 00 00 00 0f ff c5 f3 2d 01 84 f7 0f f2 78 22 c8 00 00
Time used: 0.0167432
03 0d 39 59 55 55 41 30 1e 00 cd 00 38 3a 4c ff fd 00 04 00 02 00 0e ff c4 f3 2d 01 18 f7 26 f2 91 22 c8 00 00 03 0d 71 cb
Time used: 0.0167581
55 55 41 30 1e 00 cd 00 37 3a 4d ff ff ff fb 00 04 00 0d ff c2 f3 2d 01 15 f7 26 f2 92 22 c8 00 00 03 0d 50 90
Time used: 0.0167055
55 55 41 30 1e 00 cd 00 37 3a 4c ff fd 00 03 ff f7 00 0d ff c2 f3 2d 01 18 f7 2d f2 90 22 c8 00 00 03 0d 28 47
Time used: 0.0167369
55 55 41 30 1e 00 cd 00 37 3a 4c ff f9 00 03 ff fc 00 0d ff c1 f3 2d 01 86 f7 16 f2 77 22 c8 00 00 03 0d d3 a2
Time used: 0.0167731
55 55 41 30 1e 00 cd 00 37 3a 4c 00 05 ff fe 00 04 00 0d ff bf f3 2d 01 83 f7 19 f2 7a 22 c8 00 00 03 0d 88 02
Time used: 0.0166979
55 55 41 30 1e 00 cd 00 37 3a 4c ff fe 00 03 00 01 00 0e ff bf f3 2c 01 83 f7 19 f2 7e 22 c8 00 00 03 0d e4 7f
Time used: 0.0167176
55 55 41 30 1e 00 cd 00 37 3a 49 00 02 ff fe ff fa 00 0d ff be f3 2c 01 87 f7 14 f2 75 22 c8 00 00 03 0d a6 f2
Time used: 0.0167607
55 55 41 30 1e 00 cd 00 37 3a 49 ff fe 00 04 ff ff 00 0d ff be f3 2b 01 19 f7 29 f2 8f 22 c8 00 00 03 0d 6e 9e
Time used: 0.0167293
55 55 41 30 1e 00 cd 00 37 3a 49 ff fa ff fc 00 03 00 0e ff bd f3 2b 01 1c f7 2b f2 92 22 c8 00 00 03 0d b8 04
Time used: 0.0167333
55 55 41 30 1e 00 cd 00 37 3a 49 00 07 00 00 ff f9 00 10 ff bd f3 2b 01 1a f7 2c f2 8d 22 c8 00 00 03 0d 0a 3f
Time used: 0.0167516
55 55 41 30 1e 00 cd 00 37 3a 49 00 09 00 00 00 03 00 11 ff bd f3 2b 01 84 f7 13 f2 70 22 c8 00 00 03 0d 0a 6c
Time used: 0.0167355
55 55 41 30 1e 00 cd 00 37 3a 49 00 00 00 00 ff f9 00 11 ff bd f3 2a 01 83 f7 14 f2 7a 22 c8 00 00 03 0d 39 df
Time used: 0.0167248
55 55 41 30 1e 00 cd 00 37 3a 47 ff fd 00 00 00 05 00 10 ff bd f3 2b 01 86 f7 16 f2 82 22 c8 00 00 03 0d be 5d
Time used: 0.0167432
55 55 41 30 1e 00 cd 00 37 3a 47 00 04 00 08 00 04 00 10 ff bd f3 2b 01 16 f7 23 f2 92 22 c8 00 00 03 0d 66 87
Time used: 0.0167246
55 55 41 30 1e 00 cd 00 37 3a 47 ff fb ff fd ff fd 00 10 ff be f3 2d 01 16 f7 29 f2 92 22 c8 00 00 03 0d f3 6c
Time used: 0.0167329
55 55 41 30 1e 00 cd 00 37 3a 47 00 03 00 00 00 00 00 0f ff be f3 2e 01 1c f7 2c f2 8f 22 c8 00 00 03 0d c5 b2
Time used: 0.0167521
55 55 41 30 1e 00 cd 00 38 3a 46 00 01 00 06 ff fe 00 0f ff be f3 2e 01 18 f7 2a f2 8d 22 c8 00
Time used: 0.0167297
00 03 0d 14 ef 55 55 41 30 1e 00 cd 00 37 3a 47 ff fe ff fe 00 03 00 10 ff be f3 2e 01 85 f7 17 f2 75 22 c8 00 00 03 0d b9 ec
Time used: 0.0167487
55 55 41 30 1e 00 cd 00 37 3a 44 00 0a ff fc ff f9 00 10 ff bd f3 2e 01 82 f7 12 f2 7b 22 c8 00 00 03 0d 7b 35
Time used: 0.0167062
55 55 41 30 1e 00 ce 00 37 3a 43 00 00 00 05 00 00 00 10 ff be f3 2e 01 86 f7 1a f2 75 22 c8 00 00 03 0d 98 51
Time used: 0.0167522
55 55 41 30 1e 00 ce 00 37 3a 44 ff fe 00 07 00 08 00 10 ff be f3 2e 01 1b f7 29 f2 90 22 c8 00 00 03 0d be 1b
Time used: 0.0167253
55 55 41 30 1e 00 cd 00 38 3a 44 ff fb 00 00 ff fd 00 0f ff be f3 2e 01 19 f7 22 f2 8f 22 c8 00 00 03 0d ae 28
Time used: 0.0167523
55 55 41 30 1e 00 cd 00 38 3a 43 00 00 00 00 ff fa 00 0e ff be f3 2d 01 1b f7 22 f2 8f 22 c8 00 00 03 0d 48 69
Time used: 0.0167308
55 55 41 30 1e 00 ce 00 37 3a 43 00 0b ff fc ff ff 00 0e ff be f3 2d 01 86 f7 0f f2 74 22 c8 00 00 03 0d 32 a6
Time used: 0.0167512
55 55 41 30 1e 00 ce 00 37 3a 41 ff fd 00 02 00 03 00 0e ff be f3 2d 01 81 f7 14 f2 75 22 c8 00 00 03 0d 23 17
Time used: 0.01672
55 55 41 30 1e 00 cd 00 37 3a 41 ff fa ff fa 00 00 00 0f ff bf f3 2d 01 82 f7 13 f2 78 22 c8 00 00 03 0d 70 7d
Time used: 0.0167508
55 55 41 30 1e 00 cd 00 37 3a 41 00 00 00 01 ff fc 00 0e ff bf f3 2d 01 82 f7 0f f2 6f 22 c8 00 00 03 0d da 75
Time used: 0.0167093
55 55 41 30 1e 00 cd 00 37 3a 41 00 09 00 06 ff f9 00 0e ff be f3 2d 01 19 f7 28 f2 91 22 c8 00 00 03 0d 9b 9d
Time used: 0.0167709
55 55 41 30 1e 00 cd 00 37 3a 41 ff fe ff fe 00 09 00 0d ff bc f3 2c 01 16 f7 28 f2 96 22 c8 00 00 03 0d 0d 6a
Time used: 0.0167097
55 55 41 30 1e 00 cd 00 37 3a 3e ff fe 00 00 ff f9 00 0c ff bb f3 2b 01 18 f7 26 f2 8f 22 c8 00 00 03 0d 03 e5
Time used: 0.0167399
55 55 41 30 1e 00 cd 00 37 3a 3e ff fb 00 00 ff f8 00 0d ff bb f3 2b 01 85 f7 14 f2 73 22 c8 00 00 03 0d 94 9f
Time used: 0.0167512
55 55 41 30 1e 00 cd 00 37 3a 3e ff fe 00 05 00 05 00 0e ff ba f3 2b 01 7e f7 0e f2 78 22 c8 00 00 03 0d 1d ac
Time used: 0.0167234
55 55 41 30 1e 00 cd 00 37 3a 3e ff ff 00 00 ff ff 00 0f ff bb f3 2c 01 82 f7 13 f2 79 22 c8 00 00 03 0d aa 19
Time used: 0.016781
55 55 41 30 1e 00 cd 00 37 3a 3e 00 02 ff fa 00 01 00 0f ff bd f3 2c 01 1a f7 23 f2 92 22 c8 00 00 03 0d 85 67
Time used: 0.0167152
55 55 41 30 1e 00 cd 00 37 3a 3e 00 01 00 06 ff ff 00 10 ff c0 f3 2e 01 1b f7 29 f2 98 22 c8 00 00 03 0d bd 28
Time used: 0.0167114
55 55 41 30 1e 00 cd 00 37 3a 3c 00 00 ff fc ff fd 00 10 ff c2 f3 2f 01 1d f7 2d f2 8f 22 c8 00 00 03 0d 99 33
Time used: 0.0167498
55 55 41 30 1e 00 cd 00 37 3a 3c 00 03 00 02 00 08 00 10 ff c3 f3 2f 01 19 f7 26 f2 8f 22 c8 00 00 03 0d 4d f5
Time used: 0.016729
55 55 41 30 1e 00 cd 00 37 3a 3d ff fb 00 00 00 0c 00 10 ff c5 f3 2f 01 83 f7 0f f2 72 22 c8 00 00 03 0d 97 7a
Time used: 0.0167486
55 55 41 30 1e 00 cd 00 37 3a 3c 00 03 00 01 ff f2 00 10 ff c6 f3 2f 01 7e f7 14 f2 74 22 c8 00 00 03 0d e7 f8
Time used: 0.0167708
55 55 41 30 1e 00 cd 00 37 3a 3c 00 01 ff fb 00 01 00 10 ff c5 f3 30 01 7d f7 0f f2 7c 22 c8 00 00 03 0d a6 5f
Time used: 0.0166912
55 55 41 30 1e 00 cd 00 37 3a 3c 00 02 00 00 ff ff 00 10 ff c4 f3 30 01 1a f7 26 f2 97 22 c8 00 00 03 0d 86 e8
Time used: 0.0172046
55 55 41 30 1e 00 cd 00 37 3a 3a 00 00 00 00 00 01 00 10 ff c3 f3 30 01 1c f7 29 f2 95 22 c8 00 00 03 0d 98 14
Time used: 0.0172124
55 55 41 30 1e 00 cd 00 37 3a 3a ff fe ff ff 00 00 00 0f ff c1 f3 30 01 1a f7 26 f2 a1 22 c8 00 00 03 0d 04 b3
Time used: 0.0167643
55 55 41 30 1e 00 cd 00 37 3a 3a 00 02 00 01 ff ff 00 0f ff bf f3 2f 01 1b f7 27 f2 95 22 c8 00 00 03 0d 8c a0
Time used: 0.0167289
55 55 41 30 1e 00 cd 00 37 3a 3a 00 02 00 01 ff fb 00 0f ff be f3 2f 01 82 f7 11 f2 76 22 c8 00 00 03 0d a2 f8
Time used: 0.0167338
55 55 41 30 1e 00 cd 00 37 3a 3a ff fe ff fb 00 02 00 0f ff bd f3 2d 01 80 f7 10 f2 7b 22 c8 00 00 03 0d 15 57
Time used: 0.0167279
55 55 41 30 1e 00 cd 00 37 3a 3a 00 04 00 01 00 00 00 0f ff bd f3 2d 01 7b f7 11 f2 74 22 c8 00 00
Time used: 0.016759
03 0d 13 bb 55 55 41 30 1e 00 ce 00 37 3a 38 00 0a ff fa ff fd 00 0f ff bd f3 2d 01 1d f7 2d f2 92 22 c8 00 00 03 0d 8a df
Time used: 0.0167602
55 55 41 30 1e 00 ce 00 37 3a 38 ff fd 00 02 ff ff 00 10 ff be f3 2e 01 1f f7 24 f2 98 22 c8 00 00 03 0d 11 5b
Time used: 0.0166998
55 55 41 30 1e 00 cd 00 37 3a 38 ff fb 00 02 00 00 00 11 ff c0 f3 2e 01 20 f7 28 f2 96 22 c8 00 00 03 0d 43 44
Time used: 0.0167266
55 55 41 30 1e 00 cd 00 37 3a 38 ff ff ff fb 00 06 00 10 ff c1 f3 2d 01 81 f7 1a f2 74 22 c8 00 00 03 0d a0 cc
Time used: 0.0167299
55 55 41 30 1e 00 ce 00 37 3a 38 00 02 00 03 ff ff 00 11 ff bf f3 2c 01 7f f7 10 f2 75 22 c8 00 00 03 0d 6b 54
Time used: 0.0167471
55 55 41 30 1e 00 cd 00 37 3a 37 ff f7 00 06 ff f9 00 12 ff be f3 2c 01 80 f7 14 f2 75 22 c8 00 00 03 0d 7e 44
Time used: 0.0167458
55 55 41 30 1e 00 cd 00 37 3a 36 00 01 ff fc ff ff 00 12 ff be f3 2d 01 7c f7 15 f2 75 22 c8 00 00 03 0d 0f 02
Time used: 0.0167414
55 55 41 30 1e 00 cd 00 37 3a 35 00 07 00 01 ff fb 00 11 ff be f3 2d 01 25 f7 28 f2 91 22 c8 00 00 03 0d ca be
Time used: 0.0167479
55 55 41 30 1e 00 cd 00 37 3a 35 ff fc ff fe 00 00 00 11 ff bf f3 2e 01 24 f7 2b f2 96 22 c8 00 00 03 0d 0d 3a
Time used: 0.0167359
55 55 41 30 1e 00 cd 00 38 3a 36 00 03 00 03 00 03 00 11 ff be f3 2e 01 21 f7 23 f2 8e 22 c8 00 00 03 0d 11 cc
Time used: 0.0167497
55 55 41 30 1e 00 cd 00 37 3a 35 00 04 ff fe ff f6 00 11 ff be f3 2e 01 77 f7 0d f2 76 22 c8 00 00 03 0d 53 40
Time used: 0.0167089
55 55 41 30 1e 00 cd 00 37 3a 35 00 00 ff fe 00 02 00 11 ff bb f3 2d 01 7c f7 13 f2 7f 22 c8 00 00 03 0d e8 11
Time used: 0.0167415
55 55 41 30 1e 00 cd 00 37 3a 33 ff fd 00 00 ff f7 00 11 ff ba f3 2b 01 7e f7 10 f2 76 22 c8 00 00 03 0d 47 63
Time used: 0.0167332
55 55 41 30 1e 00 ce 00 37 3a 33 00 02 00 03 00 08 00 12 ff ba f3 2b 01 26 f7 26 f2 94 22 c8 00 00 03 0d 65 ab
Time used: 0.0167403
55 55 41 30 1e 00 cd 00 37 3a 34 ff fb 00 01 00 03 00 12 ff bb f3 2b 01 23 f7 25 f2 91 22 c8 00 00 03 0d 27 fe
Time used: 0.0167189
55 55 41 30 1e 00 cd 00 37 3a 33 00 02 ff ff ff fb 00 13 ff bc f3 2c 01 22 f7 2d f2 92 22 c8 00 00 03 0d 43 ee
Time used: 0.0167396
55 55 41 30 1e 00 ce 00 37 3a 33 00 06 ff fe 00 05 00 13 ff bd f3 2d 01 20 f7 29 f2 96 22 c8 00
Time used: 0.0167433
00 03 0d 2d bc 55 55 41 30 1e 00 ce 00 37 3a 34 00 04 00 00 00 05 00 13 ff bd f3 2e 01 79 f7 18 f2 77 22 c8 00 00 03 0d 7e ec
Time used: 0.0167434
55 55 41 30 1e 00 ce 00 37 3a 32 ff fd ff fc 00 00 00 12 ff bf f3 2e 01 79 f7 15 f2 79 22 c8 00 00 03 0d 04 eb
Time used: 0.0166864
55 55 41 30 1e 00 cd 00 37 3a 32 ff fa 00 06 ff fe 00 11 ff bf f3 2e 01 7d f7 12 f2 73 22 c8 00 00 03 0d 09 2a
Time used: 0.0167479
55 55 41 30 1e 00 ce 00 37 3a 32 00 02 00 03 00 00 00 11 ff bf f3 2e 01 21 f7 29 f2 8d 22 c8 00 00 03 0d 59 d9
Time used: 0.016719
55 55 41 30 1e 00 ce 00 37 3a 32 ff ff ff ff ff ff 00 0f ff bf f3 2d 01 28 f7 27 f2 8f 22 c8 00 00 03 0d 26 d9
Time used: 0.0167552
55 55 41 30 1e 00 ce 00 38 3a 32 00 06 00 00 00 07 00 0f ff bf f3 2c 01 28 f7 26 f2 8f 22 c8 00 00 03 0d bb 19
Time used: 0.0167557
55 55 41 30 1e 00 ce 00 37 3a 32 00 02 ff fc 00 07 00 0f ff bf f3 2c 01 76 f7 10 f2 71 22 c8 00 00 03 0d fe 91
Time used: 0.0167337
55 55 00 ce 00 37 3a 31 00 02 ff fd ff f7 00 0e ff c0 f3 2d 01 73 f7 12 f2 73 22 c8 00 00 03 0d 65 6c
Time used: 0.0167553
55 55 41 30 1e 00 ce 00 37 3a 30 ff f8 00 03 ff fb 00 0d ff c1 f3 2d 01 76 f7 0e f2 7c 22 c8 00 00 03 0d c6 e4
Time used: 0.0166775
55 55 41 30 1e 00 cd 00 37 3a 30 ff fe 00 02 00 05 00 0d ff c1 f3 2e 01 7a f7 16 f2 7a 22 c8 00 00 03 0d 93 ed


Eric
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Postby Ericxx » Wed Apr 08, 2009 6:00 am

Tim wrote:So each reply packet is a fixed 37 bytes. How big is the send packet? Is it also 37 bytes?
Tim

the send(request) packet is 9 bytes long.

Tim wrote:One other question, is your serial link entirely full duplex RS232 or is there any single duplex RS232 or RS485 involved here? I ask because it's possible the timing might be VERY tight on sending/receiving that much data on single duplex serial lines and thus you could get some corruption.
Tim

our system uses the full duplex RS232.


Eric
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Postby Ericxx » Wed Apr 08, 2009 6:06 am

juanplacco wrote:Check if serial port's status change in some moment... you can use stty -a < /dev/serX command.


Thx, but in the output packet, most of the time is ok. So i don't think the status of the serial port may get changed during the execution of my program. Somehow finally i believe it might be the malfunction of the hardware itself, or the configuration of it. And I also don't see any further settings for the serial port. Look forward to your ideas. Thanks.

Eric
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Postby Tim » Wed Apr 08, 2009 11:50 am

Eric,
Ericxx wrote:Thx, but in the output packet, most of the time is ok. So i don't think the status of the serial port may get changed during the execution of my program. Somehow finally i believe it might be the malfunction of the hardware itself, or the configuration of it. And I also don't see any further settings for the serial port. Look forward to your ideas. Thanks.


It would have been nice to print out the number of bytes received as it's a pain to manually count them on each line ;-)

I assume that the reply packets always start with the '55 55 41' sequence. From looking at the output it's clear the device always replied each time to your request and there aren't any extra bytes waiting around in the serial buffer because if there were, each line wouldn't start with the '55 55 41' sequence but would instead have left over bytes from prior packets (which did happen twice time so your 15 ms time isn't quite enough time to wait so you might want to retry the test with 20ms wait and printing number of bytes received).

But yeah, it certainly appears as if the hardware is configured incorrectly/not working or else the documentation that the reply packet is always 37 bytes long isn't right.

The other things to do are:

Try another serial cable (I assume you have the device plugged in directly via a serial cable for testing) just on the off chance the cable has an issue.

Check the hardware docs to make sure you have flow control correct (H/W, S/W, none) on the serial port.

Tim
Tim
Senior Member
 
Posts: 1388
Joined: Wed Mar 10, 2004 12:28 am

Postby Ericxx » Wed Apr 08, 2009 12:42 pm

Thanks, Tim. just tried the 20ms suspension. the annex is the output.

The serial port configuration is: 8bits, no parity check, stopbit =1, no HW or SW flow control, which comply the settings of the hardware product. btw, how to set the start bit to 1?
Attachments
test1.rar
(11.64 KiB) Downloaded 149 times
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Postby Tim » Wed Apr 08, 2009 3:29 pm

Eric,

Ah, now that output file makes a TON more sense to me.

Here is the first few lines of the file:

Code: Select all
COMMENT:
NAV440, poll mode, A0, usleep(20000):

Bytes rcved: 1
55
Time used: 0.0332359
Bytes rcved: 36
55 41 30 1e 00 cb 00 3e 35 0a 00 03 00 00 00 05 00 12 ff c0 f3 2d 03 65 f4 b9 f1 e9 1c f3 00 00 03 00 2c e2
Time used: 0.0218138
Bytes rcved: 37
55 55 41 30 1e 00 cb 00 3e 35 0a ff fe ff ff ff ff 00 10 ff c0 f3 2d 03 3c f4 c0 f2 48 1c f3 00 00 03 00 60 85
Time used: 0.0217017
Bytes rcved: 1
55
Time used: 0.0311993
Bytes rcved: 36
55 41 30 1e 00 cb 00 3e 35 09 00 00 00 00 ff ff 00 12 ff c6 f3 2e 03 6b f4 bd f1 e6 1c f4 00 00 03 00 16 fd
Time used: 0.0223163
Bytes rcved: 37
55 55 41 30 1e 00 cb 00 3e 35 09 00 01 00 00 ff f5 00 12 ff c3 f3 2e 03 49 f4 c4 f1 fa 1c f4 00 00 03 00 7c 47
Time used: 0.02177


Notice, the first read returns 1 byte. This is because as soon as the first byte arrives on the serial line, the serial driver is waking up your program and you read 1 byte (the rest haven't physically arrived yet). Ie:

Bytes rcved: 1
55
Time used: 0.0332359

Now your program processes and prints out the above lines and sends another packet to the device and waits again for data.

Bytes rcved: 36
55 41 30 1e 00 cb 00 3e 35 0a 00 03 00 00 00 05 00 12 ff c0 f3 2d 03 65 f4 b9 f1 e9 1c f3 00 00 03 00 2c e2
Time used: 0.0218138

This time you are returning right away with the remaining 36 bytes from the 1st packet which the serial driver has buffered while your program was away printing out the single byte and sending another packet.

Then you print and wait again and once again you get woken on the 1st byte from the device and repeat the sequence all over again. Eventually things fall so far behind that muliple packets get queued up in the serial driver buffer and hence you see those large numbers of bytes returned.

The issue for you is that you are thinking the read() call will wait until it receives 37 bytes. But you told read() to expect anywhere up to 4096 bytes. But when you check the doc's on read() you'll see it can return fewer bytes than requested (or else you'd never return since you'll never get 4096 bytes).

What your read thread has to expect is that it's not going to get 37 bytes at a time. It's going to get them sometimes 1 at a time and sometimes 2 at a time, sometimes 36 at a time etc. Your code has to deal with getting fewer bytes than expected and put together the multiple strings into one 37 byte packet.

Tim
Tim
Senior Member
 
Posts: 1388
Joined: Wed Mar 10, 2004 12:28 am

Next

Return to Realtime and Embedded

Who is online

Users browsing this forum: No registered users and 5 guests