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.

Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

serial reading and converting to HEX for processing data

Hi everybody!

 

I have to read a set of data provided by sensors that communicate via RS232 serial protocol. I've implemented a VI that reads continuosly the string output of the serial port, but I also have to process and analyze data to do other tasks with it. I thinked to use a consumer/producer structure because the reading process is faster than the analysis process.

 

In reading everything is OK, but I've the following problems:

 

1. There are three types of data I've to manage, each one with different length in bytes. I also need to convert them from ASCII to HEX. I do this with the Type Cast tool, but the converted characthers are followed by a big number of zeros before the next valid character.

 

By example, the valid set of data are:
- 24 04 C1 XX (first length)
- 24 09 CB XX XX XX XX XX XX (2nd length)
- 24 12 C9 XX XX XX XX XX XX XX XX XX (3rd length)

 

Instead, I read in the file that is created by the producer loop:
- 2404C1XX00000000000000002404C1XX00000000000000000000000000000000000000000000000...00000002409CBXXXXXXXXXXXX000000

 

2. First three bytes of each type are respectively the START of FRAME, LENGTH of FRAME, TYPE of DATA HEX codes. The VI has to be able to recognize the first one to split the whole set of data in small parts for being processed
in the right way.

 

Does someone have any idea?

 

I've attached a screenshot of my first attemp to to this.

0 Kudos
Message 1 of 6
(5,085 Views)

What I would do is have a WHILE loop inside the Producer loop.

The WHILE loop would keep reading bytes until I get 24 since I know that is the start of a message.

Then read another 2 bytes. Based on those 3 bytes determine how many more bytes to read.

Then when that message is complete send it off to the Consumer Loop.

Message 2 of 6
(5,065 Views)

Above msg is correct but I'm wondering why you want to translate to hex.

binary is more appropriate because that is the way the system (pc) works.

Is the instrument sending in hex?

Or do you want to see it in hex.

In that situation change the display of the numeric.

greetings from the Netherlands
Message 3 of 6
(5,063 Views)

So, you are telling me to pass the whole output string of the serial read to WHILE loop, and then check byte by byte if I receive the START of FRAME, go to next character and read the length of string to get the total substring I need to process the message and finally pass it to consumer loop. Right?  

The device send me messages in HEX, but I think it's the same thing, I should be able to process data also in binary mode.

0 Kudos
Message 4 of 6
(5,037 Views)

@Jader wrote:

So, you are telling me to pass the whole output string of the serial read to WHILE loop, and then check byte by byte if I receive the START of FRAME, go to next character and read the length of string to get the total substring I need to process the message and finally pass it to consumer loop. Right?  

The device send me messages in HEX, but I think it's the same thing, I should be able to process data also in binary mode.


No, you want to read one serial byte at a time.

Message 5 of 6
(5,027 Views)

I think I found the right way! Thanks for your advice! For now everything works good. Take a look to my application...

Thank you again!

0 Kudos
Message 6 of 6
(4,967 Views)