modbus writeMultipleRegisters any ideas how to create the st

Hi
Does anyone have any ideas or an example on how to create the string values for the following:

mb:writeMultipleRegisters(mbDeviceAddress, mbRegisterAddrStart, values)

The SW doc isn’t a lot of help and the modbus lib is SW proprietary closed source so I can’t look for proper docs on the net

Thanks in advance

Ted

Well, rest assured, there will be an opensource release of the lua fwk soon (probably before the end of the month). The project will be hosted on Eclipse web site. Look for Mihini in google :wink:

Regarding the the way to create strings in Lua, the best approach is probably string.pack() that is documented in AAF documentation pack.

There may be a better way but the following may be helpful to someone

Let me know how you get on - Ted

function to_int16(value)
    value = string.reverse(value)
    byte0 = string.sub(value,1,2)
    byte0 = string.reverse(byte0)

    byte1 = string.sub(value,3,4)
    byte1 = string.reverse(byte1)

    b0 = string.char(tonumber("0x"..byte0))
    b1 = string.char(tonumber("0x"..byte1))
       -- print(b1,b0)
    result = b1 .. b0

    return(result)
end

function to_int8(value)
    byte = string.reverse(value)
    byte = string.sub(byte,1,2)
    byte = string.reverse(byte)
    -- print(byte)
    b = string.char(tonumber("0x"..byte))

    return(b)
end 

-- TEST
print(to_int16("0x4647") , string.len(to_int16("0x4647")))
print(to_int16("0x0000") , string.len(to_int16("0x0000")))

print(to_int8("0x45"))
print(to_int8("0x00"))

Using lua to do some coding to talk to a PLC. I was able to use the pack and unpack command.

The example below is packing shorts. You can pack anything.

local year		= 14
		local month	= 2
		local date		= 14
		local hour	    = 9
		local minute	= 32
		local seconds 	= 0
		local dayofweek = 5
		local mode		= 0
		local ampm	= 1
		
		-- seconds, minutes, hour, day:1 - Sunday, Day of Month, Month, Year (2 digits), Clock Mode: 0 - 24hour, 1 - 12, AM - 0/PM - 1, set=1
		buffer = string.pack("hhhhhhhhhh", seconds, minute, hour, dayofweek, date, month, year, mode, ampm,1)
                status, msg = sdevice:writeMultipleRegisters(1,register+registeroffset,buffer)

You can use the } in the pack command to swap the words.
Float = }f
Unsigned Long = }L
Signed Long = }l
Signed Sort = h
Unsigned Sort = H

When reading you can use the unpack command with the same qualifiers.