TCPIP or TCPClient: terminating string after last instance of terminator?
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a code that connects to an instrument over tcpip which communicates with JSON format:
device = tcpip('192.168.1.222', 49941);
device.Timeout = 5;
fopen(device);
M2Con.message.transmission_id = {999};
M2Con.message.op = 'start_link';
M2Con.message.parameters.ip_address = '192.168.1.7';
M2ConStat = jsondecode(query(device, jsonencode(M2Con)));
Connection to the device is established, the devices responds, the response is decoded from JSON. Everything works as expected.
However, since, as far as I understand, MATLAB is going to shift from the instrument toolbox to other commands, I'm switching all my instruments to use tcpclient instead of tcipip.
Now, the issues begin when I need to decode the response. If I rewrite my code to use tcpclient, I need to provide a termination character. Otherwise I cannot read response at all:
Error reading String.
Timeout occurred waiting for the String Terminator.
The new code is as follows:
device = tcpclient('192.168.1.222', 49941, 'Timeout', 5);
configureTerminator(device, 125)
M2Con.message.transmission_id = {999};
M2Con.message.op = 'start_link';
M2Con.message.parameters.ip_address = '192.168.1.7';
M2ConStat = jsondecode([writeread(device, jsonencode(M2Con)), '}}}']);
The default CR, LF, CR/LF do not work. I need to terminate with a '}' (ASCII = 125)
The problem is jsondecode expects several of the brackets at the end of the message, and since MATLAB terminates the string on the first bracket received, this results in an error. In this case I have added three '}}}' brakets to the response to properly decode the message.
However, other responses from the device can have four or five brackets. Can the terminator be configured in a way that will terminate after last '}' received? Otherwise I will need to manually add brackets case-by-case, which is somewhat annoying.
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!