I have a server that sends data using UDP and want to use smart-serial to receive these data.
Since there will be no triggers or timers or command/response pairs, how can I read the data and set param with the data?
Hi Dennis,
There are a few ways you could do this. One of them is work with a certain length in which case the connector will split up the data to packages of that length triggering the parameter every time. You can then check the data if it makes sense, if not, store it in another parameter, wait until the response triggers again, append the saved data with the new and see if it makes sense then and so on. Please find some more info on working with smart serial connectors here: Smart Serial
This ere is also a how-to use case that might interest you, please find it here: How to define serial and smart-serial responses in one and the same protocol
Hi Dennis, short answer is yes but dojo doesn’t allow me to easily post code here, please find a follow-up answer below.
Hi Dennis,
As stated in the comment Dojo doesn't allow me to reply nice pieces of XML in comments so here is the answer to your comment:
Yes, actually but actually you would only have 1 response which would look like this:
<Responses>
<Response id="1">
<Name>genericResponse</Name>
<Description>GenericResponse</Description>
<Content>
<Param>60</Param>
</Content>
</Response>
</Responses>
And Param 60 would look like this:
<Param id="60">
<Name>genericResponseParam</Name>
<Description>GenericResponseParam</Description>
<Type>read</Type>
<Interprete>
<RawType>other</RawType>
<LengthType>next param</LengthType>
<Type>string</Type>
</Interprete>
</Param>
After every response of “GenericResponse” you can trigger a QAction and within this QAction you can get the data from the response, check to see if there is still old data. This is some inspiration on how your Qaction might look like:
byte[] bytesBuffer = GetDataFromParameter(protocol, Parameter.messagebuffer_23);
byte[] responseBytes = GetDataFromParameter(protocol, Parameter.genericresponseparam_60);
bool bUpdateInput;
bool bUpdateOutput;if (responseBytes.Length == 0)
{
return;
}byte[] bytes = new byte[bytesBuffer.Length + responseBytes.Length];
// This code will append the old + new data
Buffer.BlockCopy(responseBytes, 0, bytes, bytesBuffer.Length, responseBytes.Length);// In this method we would try and make messages out of the data.
List<NsBusMessage> messages = GetCandidateMessages(protocol, bytes);// here we will process these messages
ProcessCandidateMessages(protocol, messages, out bUpdateInput, out bUpdateOutput);public static byte[] GetDataFromParameter(SLProtocol protocol, int ccspPid)
{
Array byteArray = (Array)protocol.GetData("PARAMETER", ccspPid);if (byteArray != null)
{
byte[] byteCommand = new byte[byteArray.Length];
for (int i = 0; i < byteArray.Length; i++)
{
byteCommand.SetValue(byteArray.GetValue(i), i);
}return byteCommand;
}return new byte[] { };
}
Thanks Timothy, now I understand this connector better and can start writing the protocol.
Hi Timothy,
How does the connector work, every time the data is received it will try to match it with the response/s?