The use of the ESP8266 in the world of IoT

User avatar
By haimgbs
#14241 Is there any progress related to that ?
the ability to add the MODBUS protocol and sensor for each ESP

i am offering here a business opportunity
User avatar
By radim100
#14818 Hi , This is not exactly what you need but may be good starting point .
Modbus TCP bridge . TCP in MODBUS RTU on RXD and TXD out .
-----------------------------------------------------------------------------------------
--========================================
-- Modbus TCP bridge
-- listen on port XX
--========================================

packet={}
packet_ptr=1
T_ID=0x0000
P_ID=0x0000
U_ID=0x0000
P_LEN=0x00

crc=0xFFFF
for i=1,20 do packet[i]=0x00 end

uart.setup(0,9600,8,0,1,0)
uart.write(0,"Start")


--========================================
-- Initialize TCP server
-- listen on port XX
--========================================
sv=net.createServer(net.TCP, 60)
global_c = nil
sv:listen(502, function(c)
if global_c~=nil then
global_c:close()
end
global_c=c
c:on("receive",function(sck,pl) SendToSlave(pl) end)
end)

--========================================
-- Receive data back from Slave
--========================================
uart.on("data",1, function(data)

if global_c~=nil then

if ( packet_ptr==3)
then
if ( packet[2]<5)
then
MB_len=string.byte(data,1)+3
else
MB_len=4+2
end
end
packet[packet_ptr]=string.byte(data,1)
if ((MB_len>0) and (packet_ptr>=(MB_len+2)))
then
SendTCPBack()
end
packet_ptr=packet_ptr+1
end

end, 0)


--========================================
-- Receive data back from Slave
--========================================
function SendTCPBack()
if global_c~=nil then

global_c:send(word2str(T_ID))
global_c:send(word2str(P_ID))
global_c:send(word2str(MB_len))



for i=1,MB_len
do
global_c:send(string.char(packet[i]))
end
end
end

--========================================
-- Receive data back from Slave
--========================================
function SendToSlave(pld)
local i
local nb=0
local str

--uart.write(0,pld)
--uart.write(0,' ')
T_ID=string.byte(pld,1)*256+string.byte(pld,2)
P_ID=string.byte(pld,3)*256+string.byte(pld,4)
P_LEN=string.byte(pld,5)*256+string.byte(pld,6)
U_ID=string.byte(pld,7)

crc=0xFFFF

nb=P_LEN
for i=1,nb,1
do
b=string.byte(pld,i+6)
uart.write(0,string.char(b))
MBusCRC(b)
end

str=string.char(bit.band(crc,0xFF))
uart.write(0,str)
str=string.char(bit.rshift(crc,8))
uart.write(0,str)

packet_ptr=1
MB_len=0

end

--==========================================================
-- MbusCRC
--==========================================================

function MBusCRC(b)

crc = bit.bxor(crc, b)

for i=1,8 do
local j = bit.band(crc, 1)
crc = bit.rshift(crc, 1)
if j ~= 0 then
crc = bit.bxor(crc, 0xA001)
end
end
end
--========================================
-- 2 bytes to string
--========================================
function word2str(w)
local s
s=string.char(bit.rshift(w,8))
s=s..string.char(bit.band(w,0xFF))
return (s)
end




-----------------------------------------------------------------------------------------