FAQ TWedge
How to Read Data in a Loop Without Using StartListen?
How to Read Data in a Loop Without Using StartListen?
Given the case you want to access a specific COM port from two scripts, but you can only open it from one script at a time. Therefore, you cannot use StartListen() because this will occupy the port all the time while the script (data collection) is active.
Below we show you how to open the port, read data, and then close the port so that the other script can access it.
hConn2.Open(funcOnConnectionLost2);
var arr2Data = null;
retries2 = 60; // wait maximum 1 minute for data (60 * 1000 ms)
while (retries2-- > 0)
{
try
{
Print ("Waiting for data from " + hConn2.Port);
var currData = hConn2.ReadData(100, 1000); // try to read max 100 Bytes in 1 sec
if (arr2Data == null)
arr2Data = currData; // first data packet
else
arr2Data.concat(currData); // subsequent data packet -> append
}
catch (err)
{
// stop when we have data already (no data packet after successful read)
if (arr2Data != null)
break;
}
}
processData2 (arr2Data);
if (hConn2.IsOpen)
closeConnection2();