Lua: Working with the Modbus TCP/IP Protocol -
this question based off previous question asked concerning similar topic: lua: working bit32 library change states of i/o's . i'm trying use lua program that, when plc changes state of coil @ given address (only 2 addresses used) triggers reaction in piece of equipment. have code exact same previous topic. has code actually doing , not bit32 library. run code don't in understand in linux ide , make changes until can make sense of it. producing weird reactions can't make sense of.
code example:
local unitid = 1 local funccodes = { readcoil = 1, readinput = 2, readholdingreg = 3, readinputreg = 4, writecoil = 5, presetsinglereg = 6, writemultiplecoils = 15, presetmultiplereg = 16 } local function totwobyte(value) return string.char(value / 255, value % 255) end local coil = 1 local function readcoil(s, coil) local req = totwobyte(0) .. totwobyte(0) .. totwobyte(6) .. string.char(unitid, funccodes.readcoil) .. totwobyte(coil - 1) .. totwobyte(1) s:write(req) --(s address of i/o module) local res = s:read(10) return res:byte(10) == 1 -- returns true or false if 10th bit ==1 think??? please confirm end
the line sets local req
part i'm not making sense of. because of earlier post, understand totwobyte
function , refreshed on bits & byte manipulation (truly excellent way). particular string reason confusion. if run in demo @ lua.org error "lua number has no integer representation". if separate following given ascii characters represent numbers (which know string.char
returns ascii representation of given digit). if run in linux ide, displays bunch of boxes, each containing 4 digits; 2 on top of other two. hard distinguish of boxes , content overlapping.
i know there modbus library may able use. rather prefer understand i'm new programming in general.
why receive different returned results windows vs linux? string "local req
" when built @ point i/o module. , don't understand how req
variable translates proper string contains of information used read/write given coil or register.
if needs better examples or has further questions need answer, please let me know.
cheers!
eta: modbus tcp/ip protocol, not rtu. sorry.
Comments
Post a Comment