LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Continuously writing/reading 24 bytes of data to serial device

Solved!
Go to solution

Hey everyone,

This is the first time I am doing byte write/read through the visa drivers on LV. I am not sure if I am doing it properly though.

 

The main idea is to configure the 24 bytes packet (thought I might use the case structure + the array and add elements to array by the shift registers) write it to the device, it will respond in the same way. This will repeat until stopping or changing a byte in the packet.

 

I am adding the VI. Can someone who is experienced with this give me some feedback?

 

Thanks!

0 Kudos
Message 1 of 11
(3,268 Views)
Solution
Accepted by grumpy_numpy

It is difficult to give "generic VISA Advice" without knowing something about the Protocol your VISA Device uses.  One of the most common "beginner mistakes" that show up on the Forum are for users who are talking to "command-oriented" VISA interfaces where you type in a Command (ending in <CR><LF>) and VISA types back a response.  These use the default VISA Configure Serial Port, with Termination Character left to its default "True" value, and beginnners use Reads and the "Bytes at Port" property, not realizing they should ask for a read of 1000 bytes and immediately process it (as it will stop when it sees the <NL>).

 

You, on the other hand, appear to have a "binary" Serial device.  They are (in my experience) harder to handle, as you need to tailor your Protocol to match that of the Instrument.   Does your code do that?  I can't tell ...

 

Bob Schor

0 Kudos
Message 2 of 11
(3,253 Views)

Hey Bob,

 

Thanks for the reply! The device is a control board, using UART protocol. Originally, the control software is written in C++. The protocol operates in a simple Transmitter/Receiver way, where both exchange this 24 bytes packet forward/backward all the time.

I want to implement this in Labview as well. This is different than the simple devices and sensors, accepting serial commands and appending a CR character to a command will control the device property. In my case all properties are as bytes, and each byte controls something. In my VI at the moment the bytes are with 0 value, but in general, depending on the property value, they will be different. In the end, I hoped that the case structure, the loop and the shift registers, will create an array of the form (01 00 ff 00 ae 00 00... up to 24-th byte). The example values are arbitrary. Once generated this 24-bytes packet will be send forward and received back, until the program is stopped.

 

This is just the way I see it. Is it correct as a concept, or there is a much better way of handling byte packets in LV?

Here is a good time to say, that in general the LV continuous serial write/read example works, but there, although I know that the read should return 71 bytes, somehow, the returned values after the read stack-up, and at some point a time-out error pops up.

 

Thanks again!

0 Kudos
Message 3 of 11
(3,227 Views)

If you "know your protocol", I see no reason you couldn't generate a "Call and Response" (I just made up this term, largely because I don't know the proper name for what I'm about to describe) where you give a command and then follow with the appropriate "Read Response" code to read and parse, as needed, whatever comes back, handle the Response, then go on to the next Call/Response step.

 

I'm puzzled by your mention of a 71-byte packet coming back, as you seem to say that all packets are 24 bytes.  If one of the Calls includes "Send us additional data, 71 bytes in all", then you need to handle it right then.

 

Bob Schor

0 Kudos
Message 4 of 11
(3,181 Views)

Hey Bob, thanks for trying to help me. Probably, even I myself was not sure what I wanted to do, and me asking here was a bit premature. Anyway, the problem was I had the wrong byte chart for the board I was working on. Now I got the right one, and managed to build the control in a way. Probably is not professional, and an expert will laugh at me, but it works and I am satisfied. I used this to change bit values of each byte, then I build an array, encode it as a hex string, and send it to VISA WRITE. I know, it is cumbersome, as there are 24 bytes, and each with all these case structures and buttons... but it works. 🙂

 

grumpy_numpy_0-1623933993801.png

 

 

0 Kudos
Message 5 of 11
(2,768 Views)
@Bob_Schor wrote:

If you "know your protocol", I see no reason you couldn't generate a "Call and Response" (I just made up this term, largely because I don't know the proper name for what I'm about to describe) where you give a command and then follow with the appropriate "Read Response" code to read and parse, as needed, whatever comes back, handle the Response, then go on to the next Call/Response step.

 

Bob Schor


I think it's called polling (as opposed to streaming when device just continuously sends data).

 

Your solution seems unnecessarily convoluted, you could just wire your boolean controls into an array (build array function) and then use boolean aray to number conversion, it'd do essentially the same thing as you are trying here.

If you control how the protocol looks then i'd suggest what bob already said and include 1 more byte at the end and use it as a termination character (you just have to be careful that your termination character can't appear in the data packets or the read could end prematurely)

 

EDIT: 

This is how my packets usually look:
| Start packet | Data Size H | Data Size L | Sender | Receiver | Data ...    ... | XOR check | End Packet |

| 0x02 | 0x00 | 0x08 | 0x49 | 0x42 | 8 bytes of data | XOR check on Data Size, Data and Sender/Receiver bytes | 0x03 |

0 Kudos
Message 6 of 11
(2,759 Views)

Hi grumpy,

 


@grumpy_numpy wrote:

Anyway, the problem was I had the wrong byte chart for the board I was working on. Now I got the right one, and managed to build the control in a way.

Probably is not professional, and an expert will laugh at me,


Oh yeah! 😄

This is Rube-Goldberg!

 

What about building arrays of your boolean values and use BooleanArrayToNum to create your U8 data?

(Your case structures probably output "1" in the TRUE case: you could have used BooleanTo0/1 atleast…)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 7 of 11
(2,758 Views)

I'm attaching a Snippet of the code others have suggested to turn your 7 Booleans and one Numeric (what if someone put "-1" or "17" in for Lamp Ready?  My code will still work ...).  Boolean to Number (and Number to Boolean) are handy to know about, particularly when you get a numeric quantity from some hardware that stores the data in bit-reversed order, and you have to "reverse the bits" -- simply convert the number to a Boolean Array, Reverse 1D Array, and Convert Boolean Array to Number.

Booleans to Number.png

 

Bob Schor

0 Kudos
Message 8 of 11
(2,737 Views)

Hey Bob,

 

Thanks again for the helpful input! I did something a bit different, as I am converting the binary byte representation to decimal number for the packet. In any case, your suggestion reduced the volume significantly.

 

grumpy_numpy_0-1623996780539.png

 

0 Kudos
Message 9 of 11
(2,701 Views)

Hi grumpy,

 


@grumpy_numpy wrote:

In any case, your suggestion reduced the volume significantly.


Why don't you use our advice (BooleanArrayToNumber)? Why do you need to stick with that huge amount of multiply/add functions to create a simple U8 value from 8 boolean values?

 


@grumpy_numpy wrote:

I did something a bit different, as I am converting the binary byte representation to decimal number for the packet.


Your packet consists of a number of bytes.

It doesn't matter if their values are presented as "decimal number", or as hexadecimal/octal/binary numbers - that's just a cosmetic representation of the very same value!

 

  • Also that typecasting of numeric constants (probably U16 values) to an array of bytes and using two IndexArray functions (each) to determine 2 scalar bytes can be replaced by a simple SplitNumber function…
  • The case structures in the upper left of your image can be replaced by Select nodes to select between two numeric constants…
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 10 of 11
(2,698 Views)