Applies To:
  • CitectSCADA 1.00 1.01 1.10 1.11 1.20 2.00 2.01 3.00

Summary:
Question: I want to use the cicode Com port functions, however my protocol contains NULL (Hex 0x00) characters. When I use the COM port functions and there is a NULL character in the string, then I loose all characters after the NULL. How can I use these functions with this type of protocol. 

Solution:
The Com port functions support transmitting and receiving of the NULL (hex 0x00) character. However you must be very careful how you operate on the buffers, as most cicode functions will destroy the characters after the NULL. Also copying the buffer from one string to another will also loose the characters after the NULL. There is also a documented problem with the SetGetChar() with NULL characters, see Q1514 for details.

The only cicode functions which will not destroy the characters after a NULL are ComRead(), ComWrite(), SetGetChar() and StrSetChar().

Some cicode functions will not destroy the characters after the NULL, however they will just ignore the data. For example StrLength() will return the length of the string up to the first NULL character and ignore the rest. So for example the following code will not work correctly, not because the characters are lost, but because the nLength is incorrect.

nLength = StrLength(sBuf);
FOR I = 0 TO nLength DO
   nChar = StrGetChar(sBuf, I);
END

The only way to get the length of the buffer is from ComRead() as follows:

nLength = 127;
ComRead(hCom, sBuf, nLength, 1);
FOR I = 0 TO nLength DO
   nChar = StrGetChar(sBuf, I);
END

Most cicode string functions will operate in this way, ie just ignore the data after the NULL. Any cicode function which modifies the original string and returns a new string will loose the characters after the NULL. For example the following cicode functions will fail:

sNewBuf = StrLower(sBuf);
sNewBuf = StrLeft(sBuf, 20);
sNewBuf = StrMid(sBuf, 12, 4);

The last example is simply copying the buffer from one string to another. When the string is copied then you will loose the characters. This is also the case if you pass the string to a function. For example

sNewBuf = sBuf;
MyFunction(sBuf);

The correct way to transmit a string with NULL characters is as follows:

nLength = 127;
FOR I = 0 TO nLength DO
   StrSetChar(sBuf, I, nChar);
END
ComWrite(hCom, sBuf, nLength, 0);

The correct way to receive a string with NULL characters is as follows:

nLength = 127;
ComRead(hCom, sBuf, nLength, 1);
FOR I = 0 TO nLength DO
   nChar = StrGetChar(sBuf, I);
END

Because of the limitations of copying the buffers and passing to functions, you must process all of the data in the same function that reads the characters.

 

Keywords:
 

Attachments