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

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.

 

Thanks for your suggestion, i learning these things, and now i have basic idea regarding shift registers. I am also reffering LabVIEW For Everyone Book to get better understanding, as we as practicing the things.

 

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.

 

Got Your Point, actually i saw a post on this on NI website from there i did that thing.

 

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

 

Oh, so sorry for that, i forget to remove that 50ms delay from loop. Thanks for pointing this.

 

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.

Yes you are right and i see how you did this, a new thing for me, you use shift registers for this task as well.

 

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.

 

Okay thanks for this suggestion, right now i don't have any idea regarding type def, but i am studying things and practicing, once i reach that portion i will update the enums

 

 

The following is the block diagram of my VI and now it is working properly.

 

VI.png

 

0x2C 0x03 0x02 0x01 0x24 0x04 -> UltraSonic Distance Packet 0x124 -> 292

0x2C 0x02 0x01 0x01 0x03 -> Turn On LED

0x2C 0x02 0x01 0x00 0x03 -> Turn Off LED

And this part is working properly, but i hadn't checked the cheksum.

Right now i am trying to calculate the Checksum in LabVIEW VI.

Checksum will simple addition or XOR.

Do i need to create a separate VI for this.

 

I am uploading my VI, please suggests about improvements.

0 Kudos
Message 11 of 27
(2,295 Views)

@xpress_embedo wrote:

0x2C 0x03 0x02 0x01 0x24 0x04 -> UltraSonic Distance Packet 0x124 -> 292

0x2C 0x02 0x01 0x01 0x03 -> Turn On LED

0x2C 0x02 0x01 0x00 0x03 -> Turn Off LED

And this part is working properly, but i hadn't checked the cheksum.

Right now i am trying to calculate the Checksum in LabVIEW VI.

Checksum will simple addition or XOR.

Do i need to create a separate VI for this.

 

I am uploading my VI, please suggests about improvements.


Which bytes are used for the checksum?  I am not seeing any real pattern with your sample data.  Assuming you are just doing a simple addition, there is the Add Array Elements that will do the job for you.


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 12 of 27
(2,273 Views)

Which bytes are used for the checksum?  I am not seeing any real pattern with your sample data.  Assuming you are just doing a simple addition, there is the Add Array Elements that will do the job for you.


 

 

The last byte will be used as checksum, currently i used some random value.

My Packet Structure is as follow:
Header, Length, Op Code, Data, Checksum

Length = Op Code + Data

Checksum = XOR Checksum of Bytes from Op Code to Last Byte of Data

Example:-

0x2C 0x02 0x01 0x01 0x00 (Here 0x00 is XOR Checksum of 0x01 0x01)
0x2C 0x02 0x01 0x00 0x01 (Here 0x01 is XOR Checksum of 0x01 0x00)
0x2C 0x03 0x02 0x01 0x24 0x27 (Here 0x27 is XOR Checksum of 0x02 0x01 0x27)

 

XOR Checksum is not neccessary, its my choice to use XOR or Simple Addition Checksum.

I am thinking of creating a separate Sub VI, that VI will have two inputs and Boolean Output.
First Input is Array and Second Input is Checksum.
VI will calculate the Checksum of Array and compare it with Checksum, if equal it output True and otherwise False.

0 Kudos
Message 13 of 27
(2,247 Views)
Solution
Accepted by topic author xpress_embedo

@xpress_embedo wrote:

XOR Checksum is not neccessary, its my choice to use XOR or Simple Addition Checksum.

I am thinking of creating a separate Sub VI, that VI will have two inputs and Boolean Output.
First Input is Array and Second Input is Checksum.
VI will calculate the Checksum of Array and compare it with Checksum, if equal it output True and otherwise False.


That is what I would do.  For the XOR, you will need a FOR loop and a shift register initialized to 0x00.  You can then autoindex on the array and XOR with the current value from the shift register.  Write the new value in the shift register.  Then it is just a simple equals comparison.


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 14 of 27
(2,222 Views)

One of the nice things about an XOR checksum is that you don't need to compare the XOR of the bytes up to the checksum with the checksum.  You can XOR all the bytes together, including the checksum.  If they match, the result will be zero.  By definition, XORing any number with itself results in all bits being zero.

Message 15 of 27
(2,197 Views)

Hey Thanks for your reply.

 

I did it, created a separate SUB VI to calculate the XOR Checksum.

XOR_Checksum.png

 

VI.png

 

Is it okay, please suggest if you find something worng in this.

Now i will use this Sub VI in my main project.

0 Kudos
Message 16 of 27
(2,181 Views)
Solution
Accepted by topic author xpress_embedo

That will work.

 

But you don't need array size or to wire it to the N.  With auto-indexing the For Loop will automatically the number of times for the number of elements in the array.

Message 17 of 27
(2,166 Views)

 

One of the nice things about an XOR checksum is that you don't need to compare the XOR of the bytes up to the checksum with the checksum.  You can XOR all the bytes together, including the checksum.  If they match, the result will be zero.  By definition, XORing any number with itself results in all bits being zero.


 

 

 

Thanks for pointing this, i forget that things about XORing, this makes my life little bit easy.

 

 


 

That will work.

 

But you don't need array size or to wire it to the N.  With auto-indexing the For Loop will automatically the number of times for the number of elements in the array.


 

 

Thanks for again pointing this thing.

I removed that array size block and it works perfectly.

 

0 Kudos
Message 18 of 27
(2,153 Views)

So here is my final VI, its working properly, but my stop button doesn't stops the VI.

Any reason behind this.

 

Apart from this how can i update the value of OP CODE case structures with some name rather than just number, like done in STATE case structure with the help of enum constants.

VI.png

 

I am also attaching the VI below.

 

Download All
0 Kudos
Message 19 of 27
(2,146 Views)

@xpress_embedo wrote:

Apart from this how can i update the value of OP CODE case structures with some name rather than just number, like done in STATE case structure with the help of enum constants.


You can make an enum and then use Type Cast to cast your byte OpCode into the new enum.

 

 


@xpress_embedo wrote:

So here is my final VI, its working properly, but my stop button doesn't stops the VI.

Any reason behind this.


My only thought here is if you do not have data constantly coming in.  The VISA Read defaults to a 10 second timeout.  So the VISA Read would have to timeout possibly before the button was even read and possibly another timeout before the loop can actually stop.  So this would be up to 20 seconds of waiting.  You could set your timeout to something shorter.  I would not go much below 1 second.


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 20 of 27
(2,135 Views)