LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Binary communication by serial port

Hi

 

I'm going to try not to sound to general about this, but there's a lot of things I don't understand, being completely new to labview. I've been browsing the forums for some hours, but I think my questions are even more basic than the answeres I find on the subjct.

 

I am to recieve a binary message on a serial port, where the first 8 bits will tell me the length of the message, and the rest is a message I need to pick out information from.

 

As of now, I'd be happy just to get labview to simulate some bits, and display it in the front window. From what I understand I'll later use the VISA to get the "real" information from the port. But I can't find anything to simulate 0's and 1's, nor to display binary information in the front window.

 

Is there anyone out there willing to set me up with a VI that simulates a binary message, maybe picks out the first 8 bits, and from this information decides wich bits in the rest of the message to display in the front window?

Really any information on the subject is much appreciated 🙂

 

/Olof

0 Kudos
Message 1 of 29
(5,304 Views)

If you want to generate random binary bits you can use this code, but it won't mean anything. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

It sounds to me like it would be easier to setup your visa communication and take some real data and then write your parsing routine based on the real data than trying to write code to simulate the binary messages you expect to receive.  Take a look at a shipping example called Advanced Serial Write/Read.vi, search for it under Help>>Find Examples...

 

 

Message Edited by jmcbee on 03-27-2009 08:23 AM
Message 2 of 29
(5,284 Views)

Once you get into it, you'll see that basic programming principles aren't really that much different with LabVIEW. It just takes a while to get used to a different way of implementing code. Stick with it, you'll get the hang of it. Smiley Wink

 

To your query: Don't think in terms of bits, but bytes. The VISA Read will read a stream of bytes from the serial port, since this is how serial communication works. This stream of bytes can be ASCII characters, or just a series of bytes which has to be interpreted a certain way. In your specific case it appears you have the latter. If the first byte tells you the length of the message then it's simply a numerical value. Since it's a single byte, you can have as many as 255 bytes to follow the first byte. The way to do this is to perform a VISA Read with a 1 wired to the number of bytes to read, and convert this to a number using the TypeCast function. This will tell you how many bytes follow. Then you can perform another VISA Read using that value for how many bytes to follow. This second VISA Read will be a string (since it always returns a string) which you convert to an array of bytes. This is your message. How this gets parsed depends on the message, but this will be documented with whatever instrument you're using. You can extract individual bytes using Index Array and/or Array Subset.

 

Here's a quick draft of this reading mechanism:

 

This should get you started, I hope. Write back if you need more help. 

Message 3 of 29
(5,283 Views)

Thanks a lot! Hopefully this will get me started, I'll get at it again after the weekend 😉

 

Just woundering, is there any was to display the data recieved in a list of some sort in the front window? So I might have a chance to figure out how to interpret the bytes sent.

 

0 Kudos
Message 4 of 29
(5,269 Views)
Don't know what kind of list you're looking for. The array that's labeled "message" in the figure I showed is a front panel indicator. It will show you all the bytes. You can change the format of the number to be displayed as decimal, hex, or binary, or even a combination of all of the above.
Message 5 of 29
(5,265 Views)

Yey, i got the sending and recieving working the way I want it, thanks alot once again!

 

Now I'm trying to combine it into one .vi, as I sometimes might need to send information, but still want it listening otherwise. I tried using a Case Structure, wired to a button, and thought it would execute the "true" structure and send the information when I pressed the button. I get it to recieve ok with this kind of setup, but when I try to send something, the read-VISA, not the write-VISA, times out. I'm running both VISA's on the same port, but can't see how this would be a problem, as it's duplex.

 

I'm not sure that Case Structure is the way to go, but I couldn't find anything else that would make my sending on-demand. Any ideas to why it's not working, or suggestions to how to do it properly?

 

Also, is there anyway to display data on different indicator, depending on what information it contains? I learned how to pick out information from a string from the previous help (thanks!), but can't find a way to send it to different indicators. I will be looking as the second bit, and depending on it's value I'd like to get it on the corresponding indicator.

 

/Olof

0 Kudos
Message 6 of 29
(5,221 Views)
Can you upload your VI? It's not clear to me how you set up your case structure.
0 Kudos
Message 7 of 29
(5,210 Views)

The VISA's should be set to the same port, and the information sent will be:

-first-

1 byte: length of information (not including the length-byte)

1 byte: Type of information (decides which indicator to display it on

x bytes: information

-last-

 

Also, the solution with the typecast and then remove the extra bytes that magically appeared might not be the best looking one... 😉

 

Edit: Made in Labview 8.6. If this doesn't work for you, I can attach some screens instead.

Message Edited by oloba on 03-30-2009 11:19 AM
0 Kudos
Message 8 of 29
(5,205 Views)

You only need 1 VISA resource control, OUTSIDE the while loop.

 

You only need 1 serial configure, OUTSIDE the while loop.

 

By configuring the serial port on every iteration, you are wiping out the bytes that are coming in before you get a chance to read them.

 

You should also put some Error Indicators on your VISA reads so you can see what errors are being generated without annoying popups.

Message 9 of 29
(5,191 Views)
And don't forget to close it when you're done (AFTER the while loop).
-Matt Bradley

************ kudos always appreciated, but only when deserved **************************




0 Kudos
Message 10 of 29
(5,184 Views)