LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Function to replace bytes in string

Hi, 

I am trying to get some data from a device. There are 4 packets and I built array to get only the data whith wich i wiil work and I have problem with finding function to replace 2 types of bytes in string.

There is a passage from data sheet in wich is what data should be replaced:

 

The frame layer sends the packet, consisting of data and a checksum between the start and end flag (frame),
using a byte-stuffing algorithm. With this algorithm, the addressee can detect the packets securely without
losing the synchronization due to extraneous data traffic. The reserved value 0xa8 is used for the start and
end flags . The second reserved character is the control byte with value 0xa9. If in the block “data” or
“checksum” a character appears which is the same as one of the flags of the control byte, it’s coded with two
bytes. First, the control byte is transmitted, and the second byte is the original data byte AND-linked with
0xdf (byte-stuffing).
0xa8 is coded as 0xa9, 0x88.
0xa9 is coded as 0xa9, 0x89.
The addressee can then clearly detect where one packet ends and the next one begins with the start and
end flags. If in the received data stream a control byte is detected, it is to be ignored and the following byte
is to be linked with 0x20 OR (destuffing).

 

Could anyone help me?

0 Kudos
Message 1 of 2
(3,239 Views)

I don't understand what your code is doing. Why are you reading only one byte at a time? You'll never be able to replace a 2-byte code that way, and byte 2 and 3 will always be empty.

 

You'll need to read a much larger number of bytes (ideally, an entire packet) at once. You might want to set the VISA termination character to A8, and then you can just read a large number of bytes and you'll get all the data up to the first instance of A8.

 

If you just need to replace those two particular sequences, you can do two instances of search and replace string. Make sure you replace the A8 sequence first. For the more general case, I would search the string for an instance of A9, split the string there, then concatenate everything prior to the match, the replacement character, and the remainder of the string past the character after A9.

0 Kudos
Message 2 of 2
(3,192 Views)