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

A couple other tips.

 

Equal to 0 has it's own primitive that would allow you to get rid of the zero constant.

 

Index from Array automatically starts from 0 and increments downward.  So you can get rid of the 0 and 1 constants.

 

Enums could work for OP codes, but enums require all numbers to be consecutive.  If if you'd have op codes 1, 2, and 40 with names for instance.  You'd have to fill in something between 2 and 40 so that the number 40 could exist as a value in an enum.  I'd recommend just putting labels in each case, or use the subdiagram label in each case.

 

For the distance calculation, you don't need to convert to doubles and do all the math of multiplying by 256 and adding.  Use the Join Numbers function.  It will take two U8 bytes and join them into a U16 integer in one step.

Message 21 of 27
(2,136 Views)

 

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


 

 

Thanks I got your point, will try when i reach home.

 

0 Kudos
Message 22 of 27
(2,111 Views)

 

Equal to 0 has it's own primitive that would allow you to get rid of the zero constant.


 

 

Thanks for the suggestion, i replaced the equal to with comparision with zero.

 


Index from Array automatically starts from 0 and increments downward.  So you can get rid of the 0 and 1 constants.


 

 

Thanks, now those constants are removed.

 


Enums could work for OP codes, but enums require all numbers to be consecutive.  If if you'd have op codes 1, 2, and 40 with names for instance.  You'd have to fill in something between 2 and 40 so that the number 40 could exist as a value in an enum.  I'd recommend just putting labels in each case, or use the subdiagram label in each case.


 

 

OP_Code will be in sequential format and if required i will put some random names, but still that part is not working.

 


For the distance calculation, you don't need to convert to doubles and do all the math of multiplying by 256 and adding.  Use the Join Numbers function.  It will take two U8 bytes and join them into a U16 integer in one step


 

 

Thanks, this simplied my block diagram

 

VI attached below with the changes suggested and is working properly.

 

 

But that type cast doesn't work for me.

I type cast the Op_Code data coming from serial port to typecast block and used constant enums to define my Op_Codes which are as follow:

0 - OP_SYNC

1 - OP_LED

2 - OP_SENSOR

3 - OP_FUTURE

 

and then connected the output with case structure, i got all these values in Case Structure but whenever i receive any data type cast block converts it to 3, OP_FUTURE i did't get that why this is happening.

VI.png

0 Kudos
Message 23 of 27
(2,079 Views)

@xpress_embedo wrote:

and then connected the output with case structure, i got all these values in Case Structure but whenever i receive any data type cast block converts it to 3, OP_FUTURE i did't get that why this is happening.


If the recieved value is above 3, it will coerce to 3.


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 24 of 27
(2,065 Views)

But i am not sending Op Code greater than 2.

 

0 - OP_SYNC

1 - OP_LED

2 - OP_SENSOR

3 - OP_FUTURE

 

I separate out that section in new VI even then for 0 type casting is correct but when i change that to input to 1 i get output as OP_FUTURE

VI.png

 

VI.png VI.png  VI.png

 

I am attaching the VI also.

 

 

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

Change the representation of your enum to a U8.  It defaults to a U16.

 

The reason this is happening is that LabVIEW uses Big Endian.  Therefore the single byte you supply is used as the most significant byte and the lower byte is filled in with 0.  So your 1 is really 256 after casting to a U16, which is way above the range of the enum.  Therefore you get the last item in the enum.


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 26 of 27
(2,047 Views)

Change the representation of your enum to a U8.  It defaults to a U16.


 

 

Right Click on Enum Constant and then clicked on Edit Items and then change to U8

VI.png

 


@crossrulz wrote:

 

The reason this is happening is that LabVIEW uses Big Endian.  Therefore the single byte you supply is used as the most significant byte and the lower byte is filled in with 0.  So your 1 is really 256 after casting to a U16, which is way above the range of the enum.  Therefore you get the last item in the enum.




 

Thanks alot for the explaination, this makes me clear why that was happening.

 

0 Kudos
Message 27 of 27
(2,039 Views)