LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

VISA Wait Event and Readback data from Serial Port

Hi all,

 

 I need help to solve my data read back problem from Com port. My Vi operation is

 

(1)Read one byte data each time.

(2)When press send(open) ,write command to VISA Write to send data to hardware side(to relay on).

(3)When press send(close) ,write command to VISA Write to send data to hardware side(to relay off).

 

In this program, I used RS232 serial port,9600 baud rate,none priority,1 stop bit,none flow control,8 bits data.

 

From hardware side, it send 54 bytes data frame on every 100 ms to PC.(interval from first byte of first frame to first byte of second frame is 100ms)

 

The main problem I am facing is read back data from serial port. When I press send(open)  button, it send command to VISA Write and relay on at hardware at once. But when I read back changes data from serial port(VISA Read),I received no data changes (I am sure data is already changed at hardware side, so relay was on). I waited 5 ms-10 ms, although I received data but it don't change anything. I suspected Buffer and I tried to flush all data after write Visa, but it is impossible because all data missed out.Please help me how can I solve this problem(to get all changes data from hardware side to PC at once)? without missing any data sending from hardware side.

 

Really appreciate for all advices and help.

0 Kudos
Message 1 of 7
(5,156 Views)

What kind of data are you sending?  Are you sending (A) ASCII data or (B) HEX?

 

If (A), this method might work. Normally, people use a special termination character such as 0x0D, 0x0A, etc... (any non-ASCII byte).  Make sure you know exactly what they are so you know when to read when to end.

 

If (B), try using force polling method where LV continuously checks for the number of bytes at the port. 

1.jpg

Make sure know what is the sync preamble and number bytes encoded for this packet so you know when to start reading and when to end.

Message Edited by lavalava on 04-11-2010 08:19 AM
0 Kudos
Message 2 of 7
(5,152 Views)

Hi lavalava,

 

  Thanks a lot for your advice  🙂 .In my Vi, I am sending HEX code. 

 

eg, my sent data to Hardware is ,

 

byte 0(Header1) =0xEE

byte 1(Header2) =0xCC

byte 2(Command) =--

byte 3(data) =--

byte 4(LSB checksum) =--

byte 5(MSB checksum) =--

 

 

received data from hardware side is 54 bytes,(every 100 ms interval)

 

byte0(Header1) =0xAA

byte1(Header2) =0xBB

byte 2 to byte 51 (data) =-----------------------------------------------------------------------------------------

byte 52(LSB checksum) =--

byte 53 (MSB checksum) =--

 

I already tried to use (bytes at serial port), but I cannot use this one because I need to check header AABB every time. so, if I use to catch (read)54 bytes every time, it is not sure to get complete frame at every time. So, one frame or half frame may be missed out. 

 

I mean data frame may be,

 

(i)           AABB------------------------------------------------------(54 bytes)

 

                       or 

 

(ii)          ------------------------------AABB------------------------ (54 bytes)

 

for second case, i will miss out this frame of data.

 

So, I need to read one byte by one byte  and check header.In my program, I haven't added code to check header yet. I am still trying to get the correct read back data.To read data one byte by one byte is OK,the main problem is to get changes data atonce when I sent command to hardware side. (I think it may be buffer problem, many data may be queued in buffer??????????????)

 

I hope all of your advice more.

 

Thanks,

lablearner

Message Edited by lablearner on 04-11-2010 08:43 AM
0 Kudos
Message 3 of 7
(5,135 Views)

lablearner wrote:

Hi lavalava,

 

  Thanks a lot for your advice.In my Vi, I am sending HEX code. 

 

eg, my sent data to Hardware is ,

 

byte 0(Header1) =0xEE

byte 1(Header2) =0xCC

byte 2(Command) =--

byte 3(data) =--

byte 4(LSB checksum) =--

byte 5(MSB checksum) =--

 

 

received data from hardware side is 54 bytes,(every 100 ms interval)

 

byte0(Header1) =0xAA

byte1(Header2) =0xBB

byte 2 to byte 51 (data) =-----------------------------------------------------------------------------------------

byte 52(LSB checksum) =--

byte 53 (MSB checksum) =--

 


Thanks,

lablearner


No this doesn't look right.  After 0xAABB header, you should have a packet size somewhere ....without this magic number, it's impossible to determine your packet end point.  Check your manual.  Just think about it, you're dealing with asynchronous serial here ...it's impossible to tell without these two key criterias: (1) header or sync preamble and (2) packet size.  You gotta have both.

 

 

Message Edited by lavalava on 04-11-2010 08:49 AM
0 Kudos
Message 4 of 7
(5,131 Views)

Hi lavalava,

 

 

Thanks again. I already have header 0xAABB and checksum data(2 bytes) . So, checksum data can be used to check frame is correct or not ? I only have a problem at Labview side that is not received changes data from hardware at once. Don't you think it is not buffer queue problem??? I taught so many data frame are continuously sending to PC side from hardware. So, when i send command to hardware side, it work properly and send changes back to PC side. But previous sent data still queuing in buffer and VISA is still receiving them. So cannot get changes at once?????????????  How can modify my program?

 

Thanks 

 

lablearner

 

0 Kudos
Message 5 of 7
(5,121 Views)

How can you modify your program?  Just get rid of all VISA driven events and replace it with continous polling method.

 

 

0 Kudos
Message 6 of 7
(5,093 Views)

This is what I meant, obviously I've never tested it but it should give you an idea as how to approach this problem.

 

Keep in mind why I emphasized the important of encoding both the header & packet length.  This is what I meant.  Normally, this is what you should have for each packet going in/out:

 

[header][packet length][data][crc16 or 32]

 

If you don't bother encoding the packet length then you'll run into cases where your header turns out to be the same as your CRC and that will cause your reader to align itself into wrong position.  This is a big deal.  Maybe not so big since you're using 2 bytes header but I've seen this case happened before.  I believe this could be your problem.

Message Edited by lavalava on 04-11-2010 03:26 PM
Message 7 of 7
(5,090 Views)