11-03-2011 08:34 AM
I use VISA in serial communication with PIC18F4550 (mcu, USB imitate Serial), I found if I read immdiately afte write, I can't get the right data all the time. It seems the delay must be inseted between write and read funciton, and the delay should be greater than 0.35 sec.
Does the delay is necessary or I misused the VISA write and read function?
Solved! Go to Solution.
11-03-2011 08:56 AM
That delay is entirely dependent on the device you are communicating with. You also need to take into account that some time is required for the data to be placed onto the serial port buffers. It also depends on your baud rate. That said, is 350 msec unreasonable for serial communication? Not at all.
11-03-2011 09:08 AM
Another thing you can do is read the bytes at the port before reading.
Have a look at the Basic Serial Write and Read.vi from the example finder. You can modify this example to check for bytes at the port before allowing the read function. If you know how long your expected response is, you can even wait until the enitre messaage is at the port before allowing the read. Although if you do this, you should include some sort of timeout function just in case you don't get the message you're expecting.
11-03-2011 09:35 AM - edited 11-03-2011 09:39 AM
Also look at using the termination char and the timeout setting to your advantage.
1 Set the termination char (depends on YOUR PIC18F4550 device read the manual)
2 Set the timeout to something MUCH longer than is needed. (like 1000 ms)
Then write followed by the read. No delay, No Read bytes at port
For the Read function request bytes MUCH loner than bytes expected.
The Read function will wait for the full message ending with the termination char and will return with your message as soon as it gets the termination char. Or it will wait until the timeout before giving up on your device.
You can then decide what to do with the timeout error if the message never comes.
I have devices that take over 3 secs to reply because they have to go do something before they can reply. Some of my timeouts are set to as high as 10 sec. I adjust the timeout on the fly based on the command I send.