Jump to: navigation, search

Mihini/modbus

< Mihini
Revision as of 09:03, 10 March 2015 by Niranjan.babu.in.bosch.com (Talk | contribs) (Created page with "== MODBUS == '''Variables''' local sched = require 'sched' local log = require 'log' local modbus=require 'modbus' local MODBUS_PORT="/dev/ttyACM0" local MODBUS_CONF={ b...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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()