LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with serial communication

Hi,
I am facing the problem while communicating with a hardware interfaced using serial port. Every time I am sending a same command and expecting the echo when read back. But sometimes I am getting the diffrent responce, however I didn't get any error while my program runs. attaching the screen shot of my code here. Can some one tell me what's wrong with my code? I have tried varying delays and with and without the flush I/O buffur .
 
Thanks in advance.
 
 
 
 
 
0 Kudos
Message 1 of 8
(3,699 Views)

Hi

Try to insert a VISA event. Using it you can react if there is data at the port. Now you read what is available a short time after writing. If you look at the data (the second string visible in the picture), you see that the data you wrote is there, but not in the correct order. It seems as if your HW writes the answer several times. If you do not react on incoming data but read after some time, you do not exactly know where you start reading the data.

If you don't know how to use VISA events, let us know.

Thomas

Using LV8.0
--------------------------------------------------------------------
Don't be afraid to rate a good answer... 😉
--------------------------------------------------------------------
0 Kudos
Message 2 of 8
(3,693 Views)

Thanks for your valuable inputs !

Please guide on using VISA events. If possible send some sample code. My HW doesn't send answer several times, instead it just sends single responce to my single command/Query. I am reading data back after sufficient time (Tried it till 1000ms).

 

Thanks

Vikas

0 Kudos
Message 3 of 8
(3,681 Views)

As I don't know which LV version you are using, I attach a picture of a simple example. All the necessary Event vis are available in the VISA Events subpalette.

Thomas

Message Edited by becktho on 11-29-2006 10:07 AM

Using LV8.0
--------------------------------------------------------------------
Don't be afraid to rate a good answer... 😉
--------------------------------------------------------------------
Message 4 of 8
(3,678 Views)
Hi Vikask,
 
It seems problem of synchronization.
To avoid this remove delay of 250ms.
Somme needful steps
1. Initialize serial port.
2. Start While loop
3. Write Command
4. Check Bytes available at port and read those.
5. Concatenate those with preveious string data.
6. Check end of response.
 
This will make sure you are reading from correct byte and end at correct byte. You need little bit programming inside while loop for synchronization pplus avoid garbage. Sometimaes you may get garbage because of any external parameters.
 
Hope this will help you.
0 Kudos
Message 5 of 8
(3,674 Views)
I agree with removing or reducing the 250mS wait.  What is happening is that you are losing some bytes, or your Flush I/O Buffer at the end is wiping out some bytes that have been sent by the device.  So on your next read, you are reading in the middle of a sent string of bytes.  Get rid of the Flush I/O Buffer function at the end.
 
If you know you should read 70 bytes each time, put your Bytes at Port function in a loop, and exit only when bytes at port >= 70.  Then read 70 bytes, use a hard coded constant 70 for number of bytes to read, not the output of Bytes at Port.  So if the device sends a bunch of bytes, and the Bytes at Port loop reads like this on each iteration: 12, 24, 36, 48, 60, 72, then the loop will end.  You will read 70 bytes, and 2 bytes will still be left in the port.  On the next loop, Bytes at Port loop reads: 14 or more,... 76.  Again, read only 70 bytes.  This way you don't lose anything.
- tbob

Inventor of the WORM Global
Message 6 of 8
(3,638 Views)
I haven't read all the responses to your problem.  But just to throw my 2 cents in -

Make sure that another program isn't using the COM port behind your back.

I was having a lot of grief trying to get a serial program to work on my laptop and found out that the ActiveSync program for my PocketPC was accessing my COM ports waiting for a connection on one of them (even though the PocketPC is USB).  Disabling this option in the ActiveSync program enabled me to use the COM port in LabVIEW.
0 Kudos
Message 7 of 8
(3,634 Views)

Thanks for all the replies!

My problem solved combining your suggestions .

Thanks a lot!

Vikas

0 Kudos
Message 8 of 8
(3,606 Views)