Jump to content

gudziel

New Members
  • Posts

    1
  • Joined

  • Last visited

Everything posted by gudziel

  1. Hello everyone. I'm a new member here and hope I can get a bit more understanding. Preface: My current position is Test Engineer who is moving into a programming position at my current job. I've self-taught my limited knowledge of programming (dealing mostly with Lua, but also a bit in Python) when my boss approached me with this opportunity. I have since created a few plugins, used with our products, for customers and they continue to grow in complexity. I consider programming to be a mental puzzle that I find extremely gratifying. My company is now paying for me to take classes at the University of Pittsburgh, which I'm currently enrolled at for a B.S. in Computer Science. Now to my questions. I've been tasked with understanding a current plugin for Modbus communication between a product and another controller. I've read how Modbus communication works, such as the proper port and how coils/registers are monitored and maintained. But I am having issues understanding why the plugin was approached in a certain way and what it is actually doing. I'd appreciate all help and guidance in helping me to understand some aspects and code that comprises the plugin. Here is a sample of one of the functions. I've attached certain local functions that might help to convey what the main function is doing. This is where a majority of my questions lie. The functions are in bold. I've greened out notes I've entered for details and colored in red my questions. I hope that it does not break any forum rules. If so I apologize. local function toTwoByte(value) return string.char(value / 255, value % 255) -- why is the value first divided by 255 and then modulo of 255 both captured? end local funcCodes = { readCoil = 1, readInput = 2, readHoldingReg = 3, readInputReg = 4, writeCoil = 5, presetSingleReg = 6, writeMultipleCoils = 15, presetMultipleReg = 16 } -- Read the status of all inputs. -- Returns an array with booleans local function readInputs(s) local req = string.char(0,0,0,0,0,6,unitId,2,0,0,0,6) local req = toTwoByte(0) .. toTwoByte(0) .. toTwoByte(6) .. string.char(unitId, funcCodes.readInput) .. toTwoByte(0) .. toTwoByte(6) -- Why is req variable first declared with set values and then redeclared in this way? s:write(req) -- s is declared in the global function as s=controller(host,port) local res = s:read(10) local out = {} for i = 1,6 do local statusBit = bit32.rshift(res:byte(10), i - 1) -- bit32.rshift means "Returns the number x shifted disp bits to the right neg is to the left" what is the point of right shifting bits (I'm guessing the 10th bit)? out[#out + 1] = bit32.band(statusBit, 1) == 1 -- bit32.band means "Returns the bitwise and of its operands" why do this? end return out end Overall, I am having an issue fully understanding how it is manipulating the bits and returning them. Again, I am apologetic if this is an improper method to ask my question. But there is a major disconnect in my head how and why these data strings are being sent and manipulated. In Modbus, I understand that the first six bits are Master, followed by (in this case) UnitID, then the data bits. I don't know why I'm having such a major disconnect seeing what this is really doing. Much of the other functions within the plugin, such as writecoils, readcoils, etc, are constructed in about the same way. Again, all input and assistance is very much appreciated. Thank you all.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.