From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Store data coming from Serial Port in Buffer and then take action?

Solved!
Go to solution

Hello! Everyone,

I am new to LabVIEW and i am taking help from various videos and stuffs available online to get started with LabVIEW.

But i have to complete an assignment, that's why need help.

 

initially i had see some of the videos tutorial and learnt about the LabVIEW interface and done one small project from communicating with Serial Port (Receiving and Sending Data).

Now coming to my task.

My LabVIEW application will send some packet to query state to my device or to control and based on the response packet i have to display values.

The Packet Structure is as

Header, Length, Op_Code, Data, Checksum

There is no terminator like Carriage Return or Line Feed or anything else.

 

In C language i read data coming from the Serial Port, and based on Length i conclude whether i receive the complete packet or not and then by recalculating the checksum value, integrity of packet is checked, and then based on which Op-Code was sent decision will be made.

(I am dealing with hexadecimal data)

 

But how can i do this in LabVIEW.
Any tutorial or any referece will be helpful for me.

 

0 Kudos
Message 1 of 27
(5,483 Views)

@xpress_embedo wrote:

In C language i read data coming from the Serial Port, and based on Length i conclude whether i receive the complete packet or not and then by recalculating the checksum value, integrity of packet is checked, and then based on which Op-Code was sent decision will be made.


You do the exact same thing with LabVIEW.  A quick way to get started with using a serial port is to look at the Simple Serial Write/Read example in the LabVIEW Example Finder (Help->Find Examples).  It shows the basics, but I would not use it as part of your program.  And since you are decing with hex/binary data, make sure the Termination Character is turned OFF (boolean input on the Configure Serial Port).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 27
(5,460 Views)

Thanks for your reply.
I had done basic serial read write program, i read the Serial Data and displayed it on string indicator.

My problem is that how to decode the packet and based on decoding how to take action.

 

And since you are decing with hex/binary data, make sure the Termination Character is turned OFF (boolean input on the Configure Serial Port).

 

As per your comments i did this with the help of this link.

http://digital.ni.com/public.nsf/allkb/6E2C88C43C76A77386256975006DB10A

 

 

Any Suggestion please on decoding part.

 

I saw on this forum that typecast can be used.

Any tutorial on this or reference will be helpful.

0 Kudos
Message 3 of 27
(5,367 Views)

@xpress_embedo wrote:

Any Suggestion please on decoding part.


That part depends on the instrument and the data inside the packet.  A LOT more details are needed to be able to help any there.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 27
(5,351 Views)

The device which sends data is Arduino UNO (Microcontroller Based Open Source Hardware).

 

The data will be as follow:

Header (0x2B) Length Op Code Data Checksum

Right Now i just have to two Op Code but in future it will be added for other things.

 

So Op Code = 0x01 then packet is like

0x2B 0x02 0x01 0x01 (Calculated Checksum) (Turn on a Led on Board and LabVIEW Front Panel)

 

So Op Code = 0x02 then packet is like

0x2B 0x03 0x02 0x01 0x55 (Calculated Checksum) (in this packet 0x155 is ultrasonic sensor value )

 

So when this pakcet is received by LabVIEW, it must decode it based on the length and then check its integrity using checksum.

And the based on Op Code and Data Value front panel will be updated.

 

0 Kudos
Message 5 of 27
(5,311 Views)

Read one byte at a time until it is equal to x2B.

 

Read one byte to get the length.

 

Read X bytes to data and  checksum.

 

Run the collected data through whatever algorithm the checksum is based on to verify the received data is good.

 

Repeat for the next commands.

 

 

Message 6 of 27
(5,297 Views)

And use a Case Structure to perform the different commands based on the opcode.  After reading the message, use the String To Byte Array and the Index Array to get the opcode out of the data.  That integer value can then be wired to the case selector tunnel.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 7 of 27
(5,279 Views)

Thanks for your suggestion.

Now i got some idea, how to do it.

I will try to implement my C code in LabVIEW. (I will read data 1 byte at a time)

 

Will tell my progress on this soon.

0 Kudos
Message 8 of 27
(5,243 Views)

Finnaly I did Something, which is as follow

 

VI.png


I created four states using enum -> IDLE_STATE, LENGTH_STATE, OP_CODE, DATA, CHECKSUM
Whenever i receive data serialy, based on the Current State i checked which type of data it is, like if i am in IDLE_STATE then i will check for header, if it is header then i will move to the next length state and if not then IDLE_STATE will remain idle.
To change the state of enum automatically in code i use local variables. (Will Hide the front panel control of enum as it is not required there)
Then in LENGTH state i do nothing just incrementing the enum state to OP_CODE State.
Then in Op_Code state if Op_Code is 1 then i light the led control otherwise not.

So this packet

0x2C 0x12 0x02 -> Turn off LED

and

0x2C 0x12 0x01 -> Turn on LED

 

Length has no meaning right now, i think declaring length as global variable will work for me.

 

0 Kudos
Message 9 of 27
(5,186 Views)
Solution
Accepted by topic author xpress_embedo

1. I suggest you learn how to use Shift Registers instead of locals and globals.  They are a lot more efficient (memory and execution speed) and make it a lot easier to read the diagram.

2. You do not need that property node inside of the loop.  There is an input to the Configure Serial Port for turning off the termination character.

3. You do not need the wait because the VISA Read will limit the loop rate if no data is coming in.

4. Once you have the length, you can just read the rest of the entire message and process it all at once.  This will make things A LOT faster.

5. You really should make that enum a type def so that you can make sure all of your enum constants have the same possible values.  If you have to add a state later, you only have to update your enum in one location then.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Download All
Message 10 of 27
(5,173 Views)