在DM中运行此脚本会导致在第一次执行期间出现以下错误。随后的执行在SPOpen(1,9600,1,0,8)上失败,我认为这意味着串行端口在那时是打开的,但第一次执行表明它没有打开。
阻止与串行端口通信的意外错误是什么?

SPOpen(1,9600,1,0,8)
SPOpen( "COM1" )
SPSendString(1, "*IDN?" )
string message
number test
message = SPReceiveString(1,8,test)
Result("Acquisition "+message+" "+test+"\n")
SPClose(1)发布于 2019-02-07 06:21:30
我现在不能自己测试串行命令,确切的脚本代码当然取决于串行连接的另一端是什么,即期望什么和返回什么。以及需要预期和关注的超时/延迟。但是,我可以看到您的脚本中有两个直接问题:
我希望你的脚本看起来更像下面这样:
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循环中等待特定的结果。
https://stackoverflow.com/questions/54561157
复制相似问题