modbus_rtu_custom
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
modbus_rtu_custom [2019/01/09 12:18] – created emozolyak | modbus_rtu_custom [2023/11/30 10:01] (current) – emozolyak | ||
---|---|---|---|
Line 9: | Line 9: | ||
*Validation error message: Invalid register address. Valid ModBus addresses are Cxxx, DIxxx, IRxxx, HRxxx. | *Validation error message: Invalid register address. Valid ModBus addresses are Cxxx, DIxxx, IRxxx, HRxxx. | ||
Code: | Code: | ||
- | <code lua> | + | < |
-- MODBUS RTU Demo Driver | -- MODBUS RTU Demo Driver | ||
+ | |||
+ | function createDevices () | ||
+ | -- read FC write FC | ||
+ | addDevice({name = " | ||
+ | addDevice({name = " | ||
+ | addDevice({name = " | ||
+ | addDevice({name = " | ||
+ | addDevice({name = " | ||
+ | addDevice({name = " | ||
+ | addDevice({name = " | ||
+ | |||
+ | end | ||
+ | |||
+ | local errorCount = 0 | ||
+ | |||
+ | SLAVE_ADDR | ||
+ | FUNC_CODE | ||
+ | REG_ADDR_HI | ||
+ | REG_ADDR_LO | ||
+ | DATA_LEN_HI | ||
+ | DATA_LEN_LO | ||
+ | CRC_POS_LO | ||
+ | CRC_POS_HI | ||
+ | |||
+ | CRC_BIG_ENDIAN | ||
+ | if CRC_BIG_ENDIAN then | ||
+ | CRC_POS_HI | ||
+ | CRC_POS_LO | ||
+ | end | ||
+ | -- template | ||
+ | local request = {SLAVE_ADDR, | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | EXCEPTIONS = { " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | |||
+ | -- dataType – The type of data that the user specified for the register. 0 = Bit, 1 = Byte, 2 = Word, 3 = Double Word, 4 = UnixTime | ||
+ | DATATYPE | ||
+ | DATALEN | ||
+ | FORMAT | ||
+ | |||
+ | table.unpack = unpack | ||
+ | function readRegister (reg, device, unitId) | ||
+ | --------------------------------------------- FORMING REQUEST ---------------------------------------------- | ||
+ | request[SLAVE_ADDR] = unitId | ||
+ | |||
+ | request[FUNC_CODE] = device.xtraFields[1] | ||
+ | |||
+ | |||
+ | request[REG_ADDR_HI] = GetHiByte(reg.internalAddr) | ||
+ | request[REG_ADDR_LO] = GetLoByte(reg.internalAddr) | ||
+ | |||
+ | count = reg.dataType == DATATYPE.DW and 2 or 1 | ||
+ | | ||
+ | request[DATA_LEN_HI] = GetHiByte(count) | ||
+ | request[DATA_LEN_LO] = GetLoByte(count) | ||
+ | |||
+ | |||
+ | local crc = GetCRC(request, | ||
+ | local crcLo, crcHi -- will be used below too | ||
+ | crcLo = GetLoByte(crc) ; request[CRC_POS_LO] = crcLo | ||
+ | crcHi = GetHiByte(crc) ; request[CRC_POS_HI] = crcHi | ||
+ | |||
+ | ---------------------------------------------- SENDING REQUEST ---------------------------------------------- | ||
+ | if not (sendBytes(request)) then | ||
+ | DEBUG(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | ---------------------------------------------- RECEIVING REPLY ---------------------------------------------- | ||
+ | local respHead, respData, respCRC = {}, {}, {} | ||
+ | |||
+ | -- read Header with length | ||
+ | respHead = readBytes(3) | ||
+ | if (respHead == false) then | ||
+ | DEBUG(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | if (respHead[SLAVE_ADDR] ~= request[SLAVE_ADDR]) then | ||
+ | ERROR(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | if (respHead[FUNC_CODE] ~= request[FUNC_CODE]) then | ||
+ | if (respHead[FUNC_CODE] >= 0x81) then | ||
+ | ERROR(" | ||
+ | readBytes(2) -- read till the end | ||
+ | else | ||
+ | ERROR(" | ||
+ | end | ||
+ | return false; | ||
+ | end | ||
+ | |||
+ | local resp_Lentgh = respHead[3]; | ||
+ | respData = readBytes(resp_Lentgh) | ||
+ | if (respData == false) then | ||
+ | DEBUG(" | ||
+ | return false; | ||
+ | end | ||
+ | local tmpResponseTab = {} | ||
+ | for i,v in ipairs(respHead) do | ||
+ | table.insert(tmpResponseTab, | ||
+ | end | ||
+ | for i,v in ipairs(respData) do | ||
+ | table.insert(tmpResponseTab, | ||
+ | end | ||
+ | |||
+ | -- check CRC in reply | ||
+ | crc = GetCRC(tmpResponseTab, | ||
+ | crcLo = GetLoByte(crc) | ||
+ | crcHi = GetHiByte(crc) | ||
+ | |||
+ | respCRC = readBytes(2) | ||
+ | if (respCRC == false) then | ||
+ | DEBUG(" | ||
+ | return false; | ||
+ | end | ||
+ | if (respCRC[1] ~= crcLo) or (respCRC[2] ~= crcHi) then | ||
+ | DEBUG(" | ||
+ | return false; | ||
+ | end | ||
+ | |||
+ | if (device.name == " | ||
+ | if (resp_Lentgh ~= count) then | ||
+ | ERROR(" | ||
+ | return false; | ||
+ | end | ||
+ | else | ||
+ | if (resp_Lentgh ~= count*2) then | ||
+ | ERROR(" | ||
+ | return false; | ||
+ | end | ||
+ | end | ||
+ | ---------------------------------------------- RETURN DATA ---------------------------------------------- | ||
+ | |||
+ | local inversion = device.xtraFields[3] | ||
+ | return GetHexFromTable(respData, | ||
+ | |||
+ | end -- readRegister | ||
+ | |||
+ | function GetHexFromTable(inputTab, | ||
+ | if #inputTab > 2 then | ||
+ | local invert = _invert or false | ||
+ | if invert then | ||
+ | invertedInputTab = {} | ||
+ | half1, half2 = SplitInHalf(inputTab) | ||
+ | |||
+ | for i, v in pairs(half2) do table.insert(invertedInputTab, | ||
+ | for i, v in pairs(half1) do table.insert(invertedInputTab, | ||
+ | inputTab = invertedInputTab; | ||
+ | end | ||
+ | |||
+ | local toReturn = {} | ||
+ | for i,v in pairs(inputTab) do toReturn[i]=tonumber(string.format(" | ||
+ | |||
+ | return toReturn | ||
+ | else | ||
+ | -- get hex and concat it to number via string operatoin | ||
+ | -- TRACE(" | ||
+ | local numberAs_String = "" | ||
+ | local tmpStr = "" | ||
+ | |||
+ | for i,v in pairs(inputTab) do | ||
+ | tmpStr = string.format(" | ||
+ | if (#tmpStr == 1) then | ||
+ | tmpStr = " | ||
+ | end | ||
+ | numberAs_String = numberAs_String..tmpStr | ||
+ | end | ||
+ | -- TRACE(" | ||
+ | return tonumber(numberAs_String, | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function writeRegister (reg, device, unitId, newValue) | ||
+ | local inversion = not(device.xtraFields[3] == 1 or false) | ||
+ | |||
+ | reg.value_format = device.xtraFields[4] | ||
+ | if reg.dataType == DATATYPE.DW and reg.value_format == FORMAT.FLOAT_32 then | ||
+ | DEBUG(" | ||
+ | else | ||
+ | DEBUG(" | ||
+ | end | ||
+ | |||
+ | if device.name == " | ||
+ | | ||
+ | return true | ||
+ | end | ||
+ | | ||
+ | local wrRequest = {}; | ||
+ | wrRequest[SLAVE_ADDR] = unitId; | ||
+ | |||
+ | wrRequest[FUNC_CODE] = device.xtraFields[2] | ||
+ | |||
+ | wrRequest[REG_ADDR_HI] = GetHiByte(reg.internalAddr) | ||
+ | wrRequest[REG_ADDR_LO] = GetLoByte(reg.internalAddr) | ||
+ | |||
+ | local dataTable = GetDataAsTable(newValue, | ||
+ | if reg.dataType == DATATYPE.DW and inversion then | ||
+ | dataTableTmp = {} | ||
+ | dataTableTmp[1]=dataTable[3]; | ||
+ | dataTableTmp[3]=dataTable[1]; | ||
+ | dataTable=dataTableTmp | ||
+ | end | ||
+ | |||
+ | -- local coilsTmp = 0 | ||
+ | if (device.name == " | ||
+ | local coilsTmp = dataTable[2] * 0xFF | ||
+ | dataTable[2] = dataTable[1] | ||
+ | dataTable[1] = coilsTmp | ||
+ | end | ||
+ | wrRequest[DATA_LEN_HI] = dataTable[1] | ||
+ | wrRequest[DATA_LEN_LO] = dataTable[2] | ||
+ | |||
+ | local crc, crcLo, crcHi = 0,0,0 | ||
+ | crc = GetCRC(wrRequest, | ||
+ | crcLo = GetLoByte(crc) | ||
+ | crcHi = GetHiByte(crc) | ||
+ | wrRequest[CRC_POS_LO] = crcLo | ||
+ | wrRequest[CRC_POS_HI] = crcHi | ||
+ | |||
+ | local res = sendBytes(wrRequest); | ||
+ | if (res == false) then | ||
+ | DEBUG(" | ||
+ | return false; | ||
+ | end | ||
+ | |||
+ | -- read response | ||
+ | res = readBytes(8) | ||
+ | |||
+ | if (res == false) then | ||
+ | DEBUG(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | if (table.concat(res) ~= table.concat(wrRequest)) then | ||
+ | DEBUG(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | if (#dataTable == DATALEN.DW) then | ||
+ | -- TRACE(" | ||
+ | -- repeat steps for 2nd Word | ||
+ | wrRequest[REG_ADDR_HI] = GetHiByte(reg.internalAddr + 1) | ||
+ | wrRequest[REG_ADDR_LO] = GetLoByte(reg.internalAddr + 1) | ||
+ | wrRequest[DATA_LEN_HI] = dataTable[3] | ||
+ | wrRequest[DATA_LEN_LO] = dataTable[4] | ||
+ | | ||
+ | crc = GetCRC(wrRequest, | ||
+ | | ||
+ | wrRequest[CRC_POS_LO] = crcLo | ||
+ | wrRequest[CRC_POS_HI] = crcHi | ||
+ | | ||
+ | res = sendBytes(wrRequest); | ||
+ | if (res == false) then | ||
+ | DEBUG(" | ||
+ | return false; | ||
+ | end | ||
+ | end | ||
+ | return true | ||
+ | end | ||
+ | |||
+ | |||
+ | -- Calculating CRC16 for MODBUS RTU | ||
+ | function GetCRC(req, offset) | ||
+ | |||
+ | local crc = 0xffff | ||
+ | local mask = 0 | ||
+ | |||
+ | -- iterate bytes | ||
+ | for i=1,# | ||
+ | crc = bit.bxor(crc, | ||
+ | -- iterate bits in byte | ||
+ | for j=1,8 do | ||
+ | mask = bit.band(crc, | ||
+ | if mask == 0x0001 then | ||
+ | crc = bit.rshift(crc, | ||
+ | crc = bit.bxor(crc, | ||
+ | else | ||
+ | crc = bit.rshift(crc, | ||
+ | end | ||
+ | |||
+ | end | ||
+ | end | ||
+ | | ||
+ | end | ||
+ | |||
+ | function GetHiByte(c) | ||
+ | assert(c < 65536, "This is not a two bytes!" | ||
+ | -- DEBUG(" | ||
+ | | ||
+ | end | ||
+ | |||
+ | function GetLoByte(c) | ||
+ | assert(c < 65536, "This is not a two bytes!" | ||
+ | -- DEBUG(" | ||
+ | return bit.band(c, | ||
+ | end | ||
+ | |||
+ | function SplitInHalf(full) | ||
+ | local h1, h2 = {}, {} | ||
+ | local half = math.ceil(# | ||
+ | for i = 1, half do | ||
+ | table.insert(h1, | ||
+ | end | ||
+ | for i = half+1, #full do | ||
+ | table.insert(h2, | ||
+ | end | ||
+ | return h1,h2 | ||
+ | end | ||
+ | |||
+ | function GetDataAsTable(value, | ||
+ | |||
+ | local highWord, lowWord, tmpTable = 0, 0, {} | ||
+ | |||
+ | if (datatype ~= DATATYPE.DW) then | ||
+ | DEBUG(" | ||
+ | tmpTable[1] = GetHiByte(value) ; DEBUG(' | ||
+ | tmpTable[2] = GetLoByte(value) ; DEBUG(' | ||
+ | else | ||
+ | highWord | ||
+ | lowWord | ||
+ | |||
+ | tmpTable[1] = GetHiByte(highWord) | ||
+ | tmpTable[2] = GetLoByte(highWord) | ||
+ | tmpTable[3] = GetHiByte(lowWord) | ||
+ | tmpTable[4] = GetLoByte(lowWord) | ||
+ | | ||
+ | DEBUG(" | ||
+ | end | ||
+ | return tmpTable | ||
+ | end | ||
+ | |||
+ | function toBits(num, | ||
+ | -- returns a table of bits, most significant first. | ||
+ | bits = bits or math.max(1, select(2, math.frexp(num))) | ||
+ | local t = {} -- will contain the bits | ||
+ | for b = bits, 1, -1 do | ||
+ | t[b] = math.fmod(num, | ||
+ | num = math.floor((num - t[b]) / 2) | ||
+ | end | ||
+ | return t | ||
+ | end | ||
+ | |||
+ | |||
+ | function Bin2Hex(s) | ||
+ | assert (type(s) == " | ||
+ | -- s -> binary string | ||
+ | local bin2hex = { | ||
+ | [" | ||
+ | [" | ||
+ | [" | ||
+ | [" | ||
+ | } | ||
+ | |||
+ | tabBytes={} | ||
+ | | ||
+ | local l = 0 | ||
+ | local h, b = "", | ||
+ | local rem | ||
+ | | ||
+ | l = string.len(s) | ||
+ | rem = l % 4 | ||
+ | l = l-1 | ||
+ | |||
+ | -- need to prepend zeros to eliminate mod 4 | ||
+ | if (rem > 0) then | ||
+ | s = string.rep(" | ||
+ | end | ||
+ | |||
+ | for i = 1, l, 4 do | ||
+ | b = string.sub(s, | ||
+ | table.insert(tabBytes, | ||
+ | if not b then ERROR(" | ||
+ | TRACE(" | ||
+ | h = h..bin2hex[b] | ||
+ | end | ||
+ | |||
+ | return h, tabBytes | ||
+ | end | ||
+ | |||
+ | |||
+ | function hex2dec(hexstr) | ||
+ | return tonumber(hexstr, | ||
+ | end | ||
+ | |||
+ | function decodeIEEE754FloatToLua(input) | ||
+ | sign = input < 0 and -1 or 1 | ||
+ | input = math.abs(input) | ||
+ | | ||
+ | bitsTable = toBits(input, | ||
+ | | ||
+ | exponentTable={table.unpack(bitsTable, | ||
+ | mantissaTable={table.unpack(bitsTable, | ||
+ | |||
+ | mantissaStr, | ||
+ | mantissaNum = hex2dec(table.concat(manstissaTabBytes)) | ||
+ | |||
+ | |||
+ | exponentStr, | ||
+ | exponentNum = hex2dec(table.concat(exponentBytesTable)) | ||
+ | |||
+ | exponent = exponentNum - 127 -- 0 - 127 = -127 --> denormalized mode | ||
+ | mantissa=mantissaNum/ | ||
+ | |||
+ | if exponent == -127 then -- denormalized mode | ||
+ | exponent = -126 | ||
+ | mantissa = mantissa | ||
+ | else | ||
+ | mantissa = mantissa + 1 | ||
+ | end | ||
+ | |||
+ | float_number=math.ldexp(mantissa, | ||
+ | return float_number | ||
+ | end | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Version 2 ==== | ||
+ | <code lua> | ||
+ | -- MODBUS RTU Driver | ||
function createDevices () | function createDevices () | ||
Line 18: | Line 447: | ||
addDevice({name = " | addDevice({name = " | ||
addDevice({name = " | addDevice({name = " | ||
+ | addDevice({name = " | ||
| | ||
end | end | ||
Line 25: | Line 455: | ||
-- template | -- template | ||
local request = {1, 2, -- slaveId FC | local request = {1, 2, -- slaveId FC | ||
- | | + | |
- | | + | |
| | ||
- | } | + | } |
+ | |||
+ | local requestM = {1, 2, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 10, 11, -- Значение2 Hi, Lo | ||
+ | | ||
+ | | ||
+ | } | ||
EXCEPTIONS = {" | EXCEPTIONS = {" | ||
Line 134: | Line 574: | ||
end | end | ||
-- RETURN DATA -- | -- RETURN DATA -- | ||
- | return GetHexFromTable(respData) | + | |
+ | return respData | ||
end -- readRegister | end -- readRegister | ||
Line 147: | Line 588: | ||
return true | return true | ||
end | end | ||
- | | + | |
- | + | ERROR(" | |
- | -- slave address | + | |
- | wrRequest[1] | + | |
- | + | | |
- | -- function code | + | local n_byte |
- | wrRequest[2] = device.xtraFields[2] | + | |
- | + | ||
- | -- address of register | + | |
- | wrRequest[3] = GetHiByte(reg.internalAddr) | + | |
- | | + | |
- | + | ||
- | | + | |
| | ||
- | | + | |
- | | + | wrRequest[n_byte] |
- | | + | |
- | dataTable[2] = dataTable[1] | + | -- Функциональный код |
- | dataTable[1] = coilsTmp | + | n_byte = n_byte +1; |
+ | wrRequest[n_byte] = device.xtraFields[2] | ||
+ | |||
+ | -- Адрес первого регистра Hi, Lo | ||
+ | n_byte | ||
+ | wrRequest[n_byte] | ||
+ | | ||
+ | wrRequest[n_byte] = GetLoByte(reg.internalAddr) | ||
+ | |||
+ | -- копируем в dataTable | ||
+ | local dataTable = GetDataAsTable(newValue, | ||
+ | |||
+ | local kol = #dataTable; -- количество байт которые нужно записать | ||
+ | -- Количество регистров Hi, Lo | ||
+ | n_byte = n_byte +1; | ||
+ | wrRequest[n_byte] = 0; | ||
+ | n_byte = n_byte +1; | ||
+ | wrRequest[n_byte] = kol/2; | ||
+ | |||
+ | -- Количество байт далее | ||
+ | n_byte = n_byte +1; | ||
+ | wrRequest[n_byte] = kol; | ||
+ | |||
+ | -- Значение 1,2,3... Hi, Lo | ||
+ | for i = 1, kol do | ||
+ | | ||
+ | wrRequest[n_byte] = dataTable[i] | ||
+ | end | ||
+ | |||
+ | -- CRC | ||
+ | local crc, crcLo, crcHi = 0,0,0 | ||
+ | crc = GetCRC(wrRequest, | ||
+ | crcLo = GetLoByte(crc) | ||
+ | crcHi = GetHiByte(crc) | ||
+ | n_byte = n_byte +1; | ||
+ | wrRequest[n_byte] = crcLo | ||
+ | n_byte = n_byte +1; | ||
+ | wrRequest[n_byte] = crcHi | ||
+ | |||
+ | |||
+ | DEBUG(" | ||
+ | local res = sendBytes(wrRequest); | ||
+ | if (res == false) then | ||
+ | DEBUG(" | ||
+ | return false; | ||
+ | end | ||
+ | |||
+ | -- читаем ответ | ||
+ | res = readBytes(8) | ||
+ | |||
+ | if (res == false) then | ||
+ | DEBUG(" | ||
+ | return false | ||
end | end | ||
- | | + | |
+ | -- проверяем записалось или нет по количеству записанных регистров | ||
+ | if ( res[6] ~= (kol/2)) then | ||
+ | | ||
+ | return false | ||
+ | end | ||
+ | -------------------------------------- My Write MHR -------------------------------------- | ||
+ | |||
+ | return true | ||
+ | else | ||
| | ||
- | | + | local wrRequest = {}; |
- | wrRequest[6] = dataTable[2] | + | |
- | + | -- slave address | |
- | -- CRC | + | wrRequest[1] = unitId; |
- | local crc, crcLo, crcHi = 0,0,0 | + | |
- | crc = GetCRC(wrRequest, 0) | + | -- function code |
- | crcLo = GetLoByte(crc) | + | wrRequest[2] = device.xtraFields[2] |
- | crcHi = GetHiByte(crc) | + | |
- | wrRequest[7] = crcLo | + | -- address of register |
- | wrRequest[8] = crcHi | + | wrRequest[3] = GetHiByte(reg.internalAddr) |
- | + | wrRequest[4] = GetLoByte(reg.internalAddr) | |
- | DEBUG("Going to send this packet "..table.concat(wrRequest)) | + | |
- | local res = sendBytes(wrRequest); | + | |
- | if (res == false) then | + | |
- | | + | local coilsTmp |
- | | + | if (device.name == "C" |
- | end | + | |
- | + | dataTable[2] = dataTable[1] | |
- | -- читаем ответ | + | |
- | res = readBytes(8) | + | end |
- | + | DEBUG("# | |
- | if (res == false) then | + | |
- | DEBUG(" | + | wrRequest[5] = dataTable[1] |
- | return false | + | wrRequest[6] = dataTable[2] |
- | end | + | |
- | | + | -- CRC |
- | if (table.concat(res) ~= table.concat(wrRequest)) then | + | local crc, crcLo, crcHi = 0,0,0 |
- | DEBUG("Response does not match!") | + | crc = GetCRC(wrRequest, |
- | | + | crcLo = GetLoByte(crc) |
- | end | + | crcHi = GetHiByte(crc) |
- | + | ||
- | if (#dataTable | + | |
- | -- DWORD | + | |
- | -- repeat steps for 2nd Word | + | |
- | wrRequest[3] = GetHiByte(reg.internalAddr + 1) | + | |
- | | + | |
- | wrRequest[5] = dataTable[3] | + | |
- | wrRequest[6] = dataTable[4] | + | |
- | crc, crcLo, crcHi = GetCRC(wrRequest, | + | |
wrRequest[7] = crcLo | wrRequest[7] = crcLo | ||
wrRequest[8] = crcHi | wrRequest[8] = crcHi | ||
- | res = sendBytes(wrRequest); | + | |
+ | DEBUG(" | ||
+ | local res = sendBytes(wrRequest); | ||
if (res == false) then | if (res == false) then | ||
DEBUG(" | DEBUG(" | ||
return false; | return false; | ||
end | end | ||
- | | + | |
- | return true | + | -- читаем ответ |
+ | res = readBytes(8) | ||
+ | |||
+ | if (res == false) then | ||
+ | DEBUG(" | ||
+ | return false | ||
+ | | ||
+ | |||
+ | if (table.concat(res) ~= table.concat(wrRequest)) then | ||
+ | DEBUG(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | if (#dataTable == 4) then | ||
+ | -- если пишем DWORD | ||
+ | -- repeat steps for 2nd Word | ||
+ | wrRequest[3] = GetHiByte(reg.internalAddr + 1) | ||
+ | wrRequest[4] = GetLoByte(reg.internalAddr + 1) | ||
+ | wrRequest[5] = dataTable[3] | ||
+ | wrRequest[6] = dataTable[4] | ||
+ | |||
+ | wrRequest[7] = nil -- удаляем ячейку со старым CRC | ||
+ | wrRequest[8] = nil -- удаляем ячейку со старым CRC | ||
+ | |||
+ | -- CRC | ||
+ | crc = GetCRC(wrRequest, | ||
+ | crcLo = GetLoByte(crc) | ||
+ | crcHi = GetHiByte(crc) | ||
+ | wrRequest[7] = crcLo | ||
+ | wrRequest[8] = crcHi | ||
+ | |||
+ | DEBUG(" | ||
+ | res = sendBytes(wrRequest); | ||
+ | if (res == false) then | ||
+ | DEBUG(" | ||
+ | return false; | ||
+ | end | ||
+ | |||
+ | -- читаем ответ | ||
+ | res = readBytes(8) | ||
+ | |||
+ | if (res == false) then | ||
+ | DEBUG(" | ||
+ | return false | ||
+ | end | ||
+ | end | ||
+ | | ||
+ | end | ||
end | end | ||
Line 281: | Line 816: | ||
else | else | ||
highWord = bit.rshift(value, | highWord = bit.rshift(value, | ||
- | lowWord = bit.band(c,0xFFFF) | + | lowWord = bit.band(value,0xFFFF) |
| | ||
tmpTable[1] = GetHiByte(highWord) | tmpTable[1] = GetHiByte(highWord) | ||
Line 290: | Line 825: | ||
return tmpTable | return tmpTable | ||
end | end | ||
+ | |||
+ | |||
</ | </ | ||
modbus_rtu_custom.1547036328.txt.gz · Last modified: 2019/01/09 12:18 by emozolyak