[求教]能不能看看哪里错了?用luars232和串行端口通信时报错
lua吧
全部回复
仅看楼主
level 6
希望可以得到好心人提示或者帮助,谢谢!有关luars232.dll,运行的时候总是在baud rate那一栏报错,具体楼下放。
2016年10月15日 08点10分 1
level 6
这是coding
local rs232 = require("luars232")
local portName = "COM3"
local out = io.stderr
-- open port
local e, p = rs232.open(portName)
if e ~= rs232.RS232_ERR_NOERROR then
-- handle error
out:write(string.format("can't open serial port '%s', error: '%s'\n",
portName, rs232.error_tostring(e)))
return
end
-- set port settings
assert(p:set_baud_rate(rs232.RS232_BAUD_9600) == rs232.RS232_ERR_NOERROR)
assert(p:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR)
assert(p:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR)
assert(p:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR)
assert(p:set_flow_control(rs232.RS232_FLOW_OFF) == rs232.RS232_ERR_NOERROR)
out:write(string.format("OK, port open with values '%s'\n", tostring(p)))
-- read with timeout
local read_len = 1 -- read two bytes
local timeout = 600 -- in miliseconds
for i=1,10 do
err, data, size = p:read(1, 600);
if data ~= nil then
print(string.byte(data, 1),size)
end
end
-- close
assert(p:close() == rs232.RS232_ERR_NOERROR)
2016年10月15日 08点10分 3
level 6
这是output给的报错
C:\Users\app\Desktop\ZeroBrane\bin\lua.exe: ...app\Desktop\LuaLightCircle\app\testSerial\testSerial.lua:20: calling 'set_baud_rate' on bad self (luars232 expected, got userdata)
stack traceback:
[C]: in function 'set_baud_rate'
...app\Desktop\LuaLightCircle\app\testSerial\testSerial.lua:20: in main chunk
[C]: at 0x00401b00
Program completed in 0.33 seconds (pid: 648).
2016年10月15日 08点10分 4
level 6
应该是原coding里的这一行有问题:
assert(p:set_baud_rate(rs232.RS232_BAUD_9600) == rs232.RS232_ERR_NOERROR)
唉心情无比抑郁,已经花了好长时间搞这玩意儿了,希望有人能指点一下到底哪里有问题,谢谢!
2016年10月15日 08点10分 5
1