Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Mihini/modbus

MODBUS

Variables

local sched = require 'sched'
local log = require 'log'
local modbus=require 'modbus'
local MODBUS_PORT="/dev/ttyACM0"
local MODBUS_CONF={ baudRate = 9600 }
local modbus_client

modbus.new(port, cfg, mode) : Creates a new modbus connection.

local function configModbus()
  modbus_client=modbus.new(MODBUS_PORT,MODBUS_CONF,"ASCII")
end

modbusdev:readHoldingRegisters(sid, address, length) Read a sequence of consecutive holding registers'

local function readModbusData()
 local data=modbus_client:readHoldingRegisters(1,0,5)
   if(data~=nil) then
     local _, Sensor1, Sensor2 = string.unpack(buffer, "h2")
     print(Sensor1..Sensor2)
   end
end

modbusdev:close()

Main Function

 local function main()
   configModbus()
    while(true) do
     sched.wait(3)
      readModbusData()
    sched.wait(3)
  end
end
 sched.run(main)
 sched.loop()
sched.run(main)
sched.loop()

Back to the top