在DM中运行此脚本会在首次执行期间导致以下错误。随后的执行在SPOpen(1,9600,1,0,8)上失败,我认为这意味着串行端口那时已打开,但第一次执行却没有打开。
什么是阻止与串行端口通信的意外错误?
var form = JsonConvert.DeserializeObject<List<object>>(comingData.ToString());
foreach (var item in form)
{
var cSharpClass = JsonConvert.DeserializeObject<dynamic>(item.ToString());
foreach (JProperty item2 in cSharpClass)
{
Console.WriteLine(item2.Name);
Console.WriteLine(item2.Value);
}
}
答案 0 :(得分:0)
我目前无法自己测试串行命令,确切的脚本代码当然取决于串行连接另一端的内容,即预期的内容和返回的内容。还有什么超时/延迟需要预期和照顾。 但是,我看到您的脚本存在两个直接问题:
我希望您的脚本看起来更类似于以下内容:
number port = 666
number baud = 9600
number stop = 10
number parity = 0
number data = 8
number portID
try
{
portID = SPOpen( port, baud, stop, parity, data )
Result( "\n Port ("+port+") opened, Handle ID: " + portID )
Result( "\n Sending messge:" + message )
string message = "*IDN?"
SPSendString( portID, message )
Result( "\n messge send." )
// Wait for response
Result( "\n Waiting for response." )
sleep( 0.3 )
number pendingBytes = SPGetPendingBytes(portID)
Result( "\n Pending bytes:" + pendingBytes )
number maxLength = 50
number bytes_back
string reply
while( pendingBytes > 1 )
{
reply += SPReceiveString( portID, maxLength, bytes_back )
pendingBytes = SPGetPendingBytes(portID)
}
Result( "\n Reply:" + Reply )
}
catch
{
// Any thrown error end up here.
// Ensures the port will not remain open
Result( "ERROR OCCURRED.\n" )
break
}
SPClose( portID )
Result( "\n Port ("+port+") closed, using Handle ID: " + portID )
上面是未经测试的代码,肯定会需要一些修改,但是它应该可以帮助您入门。等待结果时可能需要一些“延迟”,并且可能需要在while循环中等待特定的结果。