Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

HOW TO READ SERIAL DATA WITH SYNCHRONIZATION

Solved!
Go to solution

I have a question regarding my serial data acquisition application. In my application, I am using visa read to acquire serial data sent by an inertial measurement unit. When I first power up the sensor, it sends data in the following format only once:

header 1:0x8011 followed by  1 data word for checksum, 1 data word (each data word 16bits).

and then the sensor starts sending data in the following format:

header 2:0x7FFF followed by 1 data word for checksum, 9 data word (each data word 16bits).

(Note: Checksum is defined as  a 2s-complement of the 16-bit sum of the rest of the entire message) 

In my application, to keep the problem simple, I am using a wait function to make sure that the initial data tagged header 1 is sent. Then I start reading 20 bytes using VISA Read (header 2 plus the associated data words are 20 bytes), as I am mostly interested in the the data words tagged with header 2.

 

However, when I do that, the data that I am getting does not start with 7FFF character and, as a result, my 20 byte data can not be parsed properly. My question is how can I make the "VISA read" block read serial port only when the data is preceded by "7FFF".  Once I solve this part, my strategy will be reading 20 byte (including the 4 byte header) data assembly and parse it accordingly.

 

Another question, I am wondering if you can tell me how to use the checksum given it is defined as "a 2s-complement of the 16-bit sum of the rest of the entire message". Note the data tagged with header 1 is 1 message.

I would really appreciate if you responded my request.

Thank you!

Serdar 

0 Kudos
Message 1 of 13
(5,658 Views)

How I did this in the past:

Collect the data from the serial port into a shift register string and use the match pattern  ... here is a blind try (5min)

 

Example_VI_BD.png

Greetings from Germany
Henrik

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'


Message 2 of 13
(5,649 Views)

Thank you very much for the response. I'll try as soon as I get back to my lab.  Before doing that, I have one more question;I don't think I can use the match pattern because I don't know what the characters are in the serial data except for the header?

Regards 

0 Kudos
Message 3 of 13
(5,644 Views)

The dot in the match pattern is a wildcard for exact one character (read the help for match pattern and follow the 'special characters' for match pattern' link ((I put this to my help-favorits)))

You can use the match regular expression vi to do an even more specific search at the cost of process load. Here the expression would be something like:

\x7F\xFF.{20}  assuming that the 7FFF means a two byte hex value. Instead of the dot you can be more specific \x7F\xFF[0-9]{20} would accept only 20 ASCII numbers 0 to 9 after the header.

Regular expression are very powerful here's a website only on this

Greetings from Germany
Henrik

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'


0 Kudos
Message 4 of 13
(5,625 Views)

Hi Henrik;

Thank you so much for the code. Now, I am able to get the data assembly to work on. However, now I have a different problem. When I try to convert A hexadecimal string, say FF (it may not be a hexadecimal string but this is my best shot as to what it could be) into a number using "hexadecimal to number" block, I keep getting only zero whereas I am expecting to get 255. Do you have any idea how I can convert this into a number? Note that this value could be any number between -32750 and +32750.

Also, when I checked the lenght of the FF character with the "string lenght" function I am getting 1 whereas I am expecting 2. Do you have any idea what is going on here?

Thank you so much. I really appreciated your answers.

PS:I attached the file to the post. 

Regards,

Serdar 

0 Kudos
Message 5 of 13
(5,610 Views)
Instead of using that function, just typecast to a U8. The function only works for ASCII strings (i.e. the ASCII character F ). The string length is 1 because that is the length of a single character - 8 bits.
0 Kudos
Message 6 of 13
(5,605 Views)

Thank you very much for the response. Actually the hexadecimal string that I would like to read is "FF7F" 16 bit data word. When I typecast it to U16 I am getting 127 while expecting 65407. (By the way, according to the manuel of the device, this could be a value between -32750 and +32750; almost the half of the 65407. I don't know if this has sometiong to do with signed , unsigned thing? Do you have any idea)

Also, could you please let me know how I can read it as 65407?

Thank you.

Serdar 

0 Kudos
Message 7 of 13
(5,597 Views)
First, go to the front panel and for the substring indicator, right click on it and select Hex display. If the hex display is actually showing FF7F, the type cast will result in 65407. Post back with the hex string value.
0 Kudos
Message 8 of 13
(5,591 Views)
Thank you very much for the response. Yes I went the front panel and selected Hex display. The hex display is actually showing FF7F but when I typescast it , I am still getting 127 for some reason...Any idea?
Thank you. 
0 Kudos
Message 9 of 13
(5,582 Views)

here are some conversions, note the numeric Display, that need no code and work directly in the frontpanel (this will work only for the 'binary' representations , [UI][64,32,16,8])

 

conversions.png

Greetings from Germany
Henrik

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'


Message 10 of 13
(5,575 Views)