LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to convert BCD from serial to decimal

Solved!
Go to solution

hello everyone, probably a dumb question. i have timestamp data in BCD format from serial port, which i want to convert to display as LV based timestamp. 

 

i have spend almost 4-5 hours trying to figure out how to do this - probably my brain has frozen now... 

 

can anyone help?

 

ex of data:

 

43(BCD) - should be displayed as 43 itself 

Now on LabVIEW 10.0 on Win7
0 Kudos
Message 1 of 9
(5,760 Views)

Hi Enthu,

 

timestamp data in BCD format from serial port

Can you explain this with more details? The serial port usually gives you string data…

 

43(BCD) - should be displayed as 43 itself

So you get "43" and you want to display "43"? What's the problem? 😄

(You really should provide better examples!)

 

When you get a hex value (or a byte with a value of) 0x43=0d67=0o103=0b01000011 you could

- convert this byte to a hexadecimal formatted string (NumberToHexString)

- use quotient&remainder (aka modulo) to divide the byte by 16 to get both nibbles…

Best regards,
GerdW


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

What have you tried.  (i.e. attach your VI).

 

Do you have an example of the string that comes in from teh serial port with an explanation of what it means?  Is each byte representing 1 digit?  Or are 2 digits packed into one byte?

0 Kudos
Message 3 of 9
(5,746 Views)

i have attached the vi. it looks messy, however, if you look at the indicator (normal display -1) & (normal display -2), they indicate the format of data as is read by serial port. 

 

now, if i right click on the indicator and select display as hex - i see the numerical value of the data (which are element 3 till element 6).

 

i would like my data to be seen/captured/saved as is seen in element 3 to element 6. hopefully, it is clear now what i would like to do

Now on LabVIEW 10.0 on Win7
Download All
0 Kudos
Message 4 of 9
(5,736 Views)
Solution
Accepted by topic author LV_Enthu

Sounds like you are looking to do something like this.

The Quotient & Remainder is used to seperate the upper and lower nibbles.  The upper nibble is then multiplied by 10 and added to the lower nibble.  From there, it is a matter of putting the data into the right location in the cluster.


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 5 of 9
(5,715 Views)

That worked, thanks a lot. no way i would have come up with that logic... 

 

just out of curiosity, what is the palm -> labview symbol on the top of your snapshot? seen that lot many times 

Now on LabVIEW 10.0 on Win7
0 Kudos
Message 6 of 9
(5,697 Views)

Those symbols are put there by the Code Capture Tool.

 

I've never thought about specifically what they mean before, but now that you've asked, I 'm guessing it represents a Drag To LabVIEW since you can take that image and drop it into a LabVIEW block diagram.  (You may need to drag the image to the desktop first so it creates a graphics file, then drag that file to LabVIEW, depending on your browser and windows settings.)

0 Kudos
Message 7 of 9
(5,683 Views)

Hi Enthu,

 

another solution with less math and more string formatting:

check.png

 

On your VI:

- Why do you set a timeout of 100s on your COM port? Do you really want to wait for >1½min to receive a single byte?

- What's the purpose of the wait function in the loop? You either wait for a byte or you have to process this byte: no additional wait time needed!

- Why do you read byte by byte when your data consists of atleast 6 bytes (of the timestamp)?

- What's the purpose of BytesAtPort? You don't need it…

- When it comes to handle hex/BCD/decimal conversion: always, ever show the radix of numbers and display mode of strings! (As the string constant in my snippet)

- You don't need to wire the constants at IndexArray function, it automatically starts to index with the first array element…

- There's a special comparison function to check for empty strings!

- When the received string is empty you should have received a timeout error: do proper error handling!

 

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 8 of 9
(5,642 Views)

@GerdW wrote:

- What's the purpose of BytesAtPort? You don't need it…


I'd argue that he does, just not the way he was trying to use it.

 

So I'm assuming the data comes in whenever it feels like it (not a continuous stream of data).  And based on the timeout (100 seconds!), it is rare to actually get any data.  So what I do in these situations is use the Bytes At Port just to see if a message has at least started to come in (ie see if there is any data on the port).  If there is data, read the entire message (in this case, 6 bytes).  If there is no data, put in a wait.  That 100ms sounds like a good wait time.  But this way we are making sure we process a complete message instead of a mixed up soup from two different messages.

The 100ms wait is in the FALSE case.

 

And a side note: That is a really odd baud rate being used.  Many serial ports likely will not support it.


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 9 of 9
(5,618 Views)