Hello,
I am using fbRecvFrom for receiving text messages with length around 1000 Bytes. The messages are not delimited with any symbol, so I cannot use fbRecvTxt or fbRecvChar. Unfortunately, I cannot use STRING variable for message reception as the STRING length is limited to 255 character. I can use ARRAY[1..1024] OF BYTE for receiving the message, but I am not able to convert it to STRING. Could you write me short snippet how it can be solved?
Even when I use STRING for shorter messages and then try to trim its length to length of received data, the result trimmed string is empty. For some strange reason the output variable of fbRecvFrom keeps to be zero. So the LEFT function correctly returns empty string:
VAR_OUTPUT
receivedData : STRING[255];
receivedDataString : STRING;
END_VAR
VAR
lastErrorCode : USINT;
receivedDataLength : UINT;
END_VAR
gRecvFrom( rq := true, chanCode := ETH1_UNI0,
lenRx := 255, data := void(receivedData),
error => lastErrorCode, lenData => receivedDataLength);
receivedDataString := LEFT(receivedData, receivedDataLength);
By the way, I recently found that there are a lot of troubles with receiving telegrams if you create more than one instance of fbRecvFrom for the same communication channel in your PLC. You have to create only one global variable instance for each channel and use it. Otherwise you may "receive" the same message from each instance. Well, when you consider the basis of TCP channel as a shared resource, then this behavior probably makes sense. But you should note it in the documentation I think.
Answers 2
The output variable lenData of the function block fbRecvFrom is set only when a data are received (output mesRec is true). Also if you use communication channel created by I/O Configurator you should set size of receiving zone the same as the LenRx input of the fbRecvFrom. In case the size of receiving zone is greater than the LenRx, data longer than the LenRx will be lost.
VAR_OUTPUT
receivedData : STRING[255];
receivedDataString : STRING;
END_VAR
VAR
lastErrorCode : USINT;
receivedDataLength : UINT;
END_VAR
gRecvFrom( rq := true, chanCode := ETH1_UNI0,
lenRx := 255, data := void(receivedData),
error => lastErrorCode, lenData => receivedDataLength);
IF gRecvFrom.mesRec THEN
receivedDataString := LEFT(receivedData, receivedDataLength);
END_IF;
Thank you for the answer. The missing test of mesRec was the but in the code.
In your code you are using string of size 255. It is a maximum size of the string type. Is it possible to handle longer strings? I mean to read more than 255 Bytes from a TCP receive buffer in one machine cycle? I can read it into array of Bytes, but I don't know hot to convert this array to a string then (possibly to many 255 Bytes long strings).
This question is solved.