Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
if serialPort is None:
sys.exit('No module connected (check cable)')

print("Please enter the MODBUS slave address (1...255)")
slave = 0
while (slave < 1) or (slave > 255):
slave = int(input("slave: ")) # use raw_input in python 2.x
print("Please enter the MODBUS subordinate address (1...255)")
subordinate = 0
while (subordinate < 1) or (subordinate > 255):
subordinate = int(input("subordinate: ")) # use raw_input in python 2.x

reg = 0
while (reg < 1) or (reg >= 50000) or (reg % 10000) == 0:
Expand All @@ -34,13 +34,13 @@

while True:
if reg >= 40001:
val = serialPort.modbusReadInputRegisters(slave, reg - 40001, 1)[0]
val = serialPort.modbusReadInputRegisters(subordinate, reg - 40001, 1)[0]
elif reg >= 30001:
val = serialPort.modbusReadRegisters(slave, reg - 30001, 1)[0]
val = serialPort.modbusReadRegisters(subordinate, reg - 30001, 1)[0]
elif reg >= 10001:
val = serialPort.modbusReadInputBits(slave, reg - 10001, 1)[0]
val = serialPort.modbusReadInputBits(subordinate, reg - 10001, 1)[0]
else:
val = serialPort.modbusReadBits(slave, reg - 1, 1)[0]
val = serialPort.modbusReadBits(subordinate, reg - 1, 1)[0]

print("Current value: " + str(val))
print("Press ENTER to read again, Q to quit")
Expand All @@ -53,7 +53,7 @@
if cmd != "" and ((reg % 30000) < 10000):
val = int(cmd)
if reg >= 30001:
serialPort.modbusWriteRegister(slave, reg - 30001, val)
serialPort.modbusWriteRegister(subordinate, reg - 30001, val)
else:
serialPort.modbusWriteBit(slave, reg - 1, val)
serialPort.modbusWriteBit(subordinate, reg - 1, val)

20 changes: 10 additions & 10 deletions yoctolib_python/Examples/Prog-Modbus/modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
if serialPort is None:
sys.exit('No module connected (check cable)')

print("Please enter the MODBUS slave address (1...255)")
slave = 0
while (slave < 1) or (slave > 255):
slave = int(input("slave: ")) # use raw_input in python 2.x
print("Please enter the MODBUS subordinate address (1...255)")
subordinate = 0
while (subordinate < 1) or (subordinate > 255):
subordinate = int(input("subordinate: ")) # use raw_input in python 2.x

reg = 0
while (reg < 1) or (reg >= 50000) or (reg % 10000) == 0:
Expand All @@ -34,13 +34,13 @@

while True:
if reg >= 40001:
val = serialPort.modbusReadInputRegisters(slave, reg - 40001, 1)[0]
val = serialPort.modbusReadInputRegisters(subordinate, reg - 40001, 1)[0]
elif reg >= 30001:
val = serialPort.modbusReadRegisters(slave, reg - 30001, 1)[0]
val = serialPort.modbusReadRegisters(subordinate, reg - 30001, 1)[0]
elif reg >= 10001:
val = serialPort.modbusReadInputBits(slave, reg - 10001, 1)[0]
val = serialPort.modbusReadInputBits(subordinate, reg - 10001, 1)[0]
else:
val = serialPort.modbusReadBits(slave, reg - 1, 1)[0]
val = serialPort.modbusReadBits(subordinate, reg - 1, 1)[0]

print("Current value: " + str(val))
print("Press ENTER to read again, Q to quit")
Expand All @@ -53,7 +53,7 @@
if cmd != "" and ((reg % 30000) < 10000):
val = int(cmd)
if reg >= 30001:
serialPort.modbusWriteRegister(slave, reg - 30001, val)
serialPort.modbusWriteRegister(subordinate, reg - 30001, val)
else:
serialPort.modbusWriteBit(slave, reg - 1, val)
serialPort.modbusWriteBit(subordinate, reg - 1, val)

68 changes: 34 additions & 34 deletions yoctolib_python/Sources/yocto_serialport.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def writeLine(self, text):
def writeMODBUS(self, hexString):
"""
Sends a MODBUS message (provided as a hexadecimal string) to the serial port.
The message must start with the slave address. The MODBUS CRC/LRC is
The message must start with the subordinate address. The MODBUS CRC/LRC is
automatically added by the function. This function does not wait for a reply.

@param hexString : a hexadecimal message string, including device address but no CRC/LRC
Expand Down Expand Up @@ -970,12 +970,12 @@ def queryLine(self, query, maxWait):
res = self._json_get_string(YString2Byte(msgarr[0]))
return res

def queryMODBUS(self, slaveNo, pduBytes):
def queryMODBUS(self, subordinateNo, pduBytes):
"""
Sends a message to a specified MODBUS slave connected to the serial port, and reads the
Sends a message to a specified MODBUS subordinate connected to the serial port, and reads the
reply, if any. The message is the PDU, provided as a vector of bytes.

@param slaveNo : the address of the slave MODBUS device to query
@param subordinateNo : the address of the subordinate MODBUS device to query
@param pduBytes : the message to send (PDU), as a vector of bytes. The first byte of the
PDU is the MODBUS function code.

Expand All @@ -997,8 +997,8 @@ def queryMODBUS(self, slaveNo, pduBytes):
# hexb
funCode = pduBytes[0]
nib = ((funCode) >> (4))
pat = "" + ("%02X" % slaveNo) + "[" + ("%X" % nib) + "" + ("%X" % (nib+8)) + "]" + ("%X" % ((funCode) & (15))) + ".*"
cmd = "" + ("%02X" % slaveNo) + "" + ("%02X" % funCode)
pat = "" + ("%02X" % subordinateNo) + "[" + ("%X" % nib) + "" + ("%X" % (nib+8)) + "]" + ("%X" % ((funCode) & (15))) + ".*"
cmd = "" + ("%02X" % subordinateNo) + "" + ("%02X" % funCode)
i = 1
while i < len(pduBytes):
cmd = "" + cmd + "" + ("%02X" % ((pduBytes[i]) & (0xff)))
Expand All @@ -1008,7 +1008,7 @@ def queryMODBUS(self, slaveNo, pduBytes):
msgs = self._download(url)
reps = self._json_get_array(msgs)
if not (len(reps) > 1):
self._throw(YAPI.IO_ERROR, "no reply from slave")
self._throw(YAPI.IO_ERROR, "no reply from subordinate")
if len(reps) > 1:
rep = self._json_get_string(YString2Byte(reps[0]))
replen = ((len(rep) - 3) >> (1))
Expand All @@ -1029,12 +1029,12 @@ def queryMODBUS(self, slaveNo, pduBytes):
self._throw(YAPI.INVALID_ARGUMENT, "MODBUS error: failed to execute function")
return res

def modbusReadBits(self, slaveNo, pduAddr, nBits):
def modbusReadBits(self, subordinateNo, pduAddr, nBits):
"""
Reads one or more contiguous internal bits (or coil status) from a MODBUS serial device.
This method uses the MODBUS function code 0x01 (Read Coils).

@param slaveNo : the address of the slave MODBUS device to query
@param subordinateNo : the address of the subordinate MODBUS device to query
@param pduAddr : the relative address of the first bit/coil to read (zero-based)
@param nBits : the number of bits/coils to read

Expand All @@ -1057,7 +1057,7 @@ def modbusReadBits(self, slaveNo, pduAddr, nBits):
pdu.append(((nBits) & (0xff)))

# // may throw an exception
reply = self.queryMODBUS(slaveNo, pdu)
reply = self.queryMODBUS(subordinateNo, pdu)
if len(reply) == 0:
return res
if reply[0] != pdu[0]:
Expand All @@ -1082,12 +1082,12 @@ def modbusReadBits(self, slaveNo, pduAddr, nBits):

return res

def modbusReadInputBits(self, slaveNo, pduAddr, nBits):
def modbusReadInputBits(self, subordinateNo, pduAddr, nBits):
"""
Reads one or more contiguous input bits (or discrete inputs) from a MODBUS serial device.
This method uses the MODBUS function code 0x02 (Read Discrete Inputs).

@param slaveNo : the address of the slave MODBUS device to query
@param subordinateNo : the address of the subordinate MODBUS device to query
@param pduAddr : the relative address of the first bit/input to read (zero-based)
@param nBits : the number of bits/inputs to read

Expand All @@ -1110,7 +1110,7 @@ def modbusReadInputBits(self, slaveNo, pduAddr, nBits):
pdu.append(((nBits) & (0xff)))

# // may throw an exception
reply = self.queryMODBUS(slaveNo, pdu)
reply = self.queryMODBUS(subordinateNo, pdu)
if len(reply) == 0:
return res
if reply[0] != pdu[0]:
Expand All @@ -1135,12 +1135,12 @@ def modbusReadInputBits(self, slaveNo, pduAddr, nBits):

return res

def modbusReadRegisters(self, slaveNo, pduAddr, nWords):
def modbusReadRegisters(self, subordinateNo, pduAddr, nWords):
"""
Reads one or more contiguous internal registers (holding registers) from a MODBUS serial device.
This method uses the MODBUS function code 0x03 (Read Holding Registers).

@param slaveNo : the address of the slave MODBUS device to query
@param subordinateNo : the address of the subordinate MODBUS device to query
@param pduAddr : the relative address of the first holding register to read (zero-based)
@param nWords : the number of holding registers to read

Expand All @@ -1162,7 +1162,7 @@ def modbusReadRegisters(self, slaveNo, pduAddr, nWords):
pdu.append(((nWords) & (0xff)))

# // may throw an exception
reply = self.queryMODBUS(slaveNo, pdu)
reply = self.queryMODBUS(subordinateNo, pdu)
if len(reply) == 0:
return res
if reply[0] != pdu[0]:
Expand All @@ -1180,12 +1180,12 @@ def modbusReadRegisters(self, slaveNo, pduAddr, nWords):

return res

def modbusReadInputRegisters(self, slaveNo, pduAddr, nWords):
def modbusReadInputRegisters(self, subordinateNo, pduAddr, nWords):
"""
Reads one or more contiguous input registers (read-only registers) from a MODBUS serial device.
This method uses the MODBUS function code 0x04 (Read Input Registers).

@param slaveNo : the address of the slave MODBUS device to query
@param subordinateNo : the address of the subordinate MODBUS device to query
@param pduAddr : the relative address of the first input register to read (zero-based)
@param nWords : the number of input registers to read

Expand All @@ -1207,7 +1207,7 @@ def modbusReadInputRegisters(self, slaveNo, pduAddr, nWords):
pdu.append(((nWords) & (0xff)))

# // may throw an exception
reply = self.queryMODBUS(slaveNo, pdu)
reply = self.queryMODBUS(subordinateNo, pdu)
if len(reply) == 0:
return res
if reply[0] != pdu[0]:
Expand All @@ -1225,12 +1225,12 @@ def modbusReadInputRegisters(self, slaveNo, pduAddr, nWords):

return res

def modbusWriteBit(self, slaveNo, pduAddr, value):
def modbusWriteBit(self, subordinateNo, pduAddr, value):
"""
Sets a single internal bit (or coil) on a MODBUS serial device.
This method uses the MODBUS function code 0x05 (Write Single Coil).

@param slaveNo : the address of the slave MODBUS device to drive
@param subordinateNo : the address of the subordinate MODBUS device to drive
@param pduAddr : the relative address of the bit/coil to set (zero-based)
@param value : the value to set (0 for OFF state, non-zero for ON state)

Expand All @@ -1252,20 +1252,20 @@ def modbusWriteBit(self, slaveNo, pduAddr, value):
pdu.append(0x00)

# // may throw an exception
reply = self.queryMODBUS(slaveNo, pdu)
reply = self.queryMODBUS(subordinateNo, pdu)
if len(reply) == 0:
return res
if reply[0] != pdu[0]:
return res
res = 1
return res

def modbusWriteBits(self, slaveNo, pduAddr, bits):
def modbusWriteBits(self, subordinateNo, pduAddr, bits):
"""
Sets several contiguous internal bits (or coils) on a MODBUS serial device.
This method uses the MODBUS function code 0x0f (Write Multiple Coils).

@param slaveNo : the address of the slave MODBUS device to drive
@param subordinateNo : the address of the subordinate MODBUS device to drive
@param pduAddr : the relative address of the first bit/coil to set (zero-based)
@param bits : the vector of bits to be set (one integer per bit)

Expand Down Expand Up @@ -1308,7 +1308,7 @@ def modbusWriteBits(self, slaveNo, pduAddr, bits):
pdu.append(val)

# // may throw an exception
reply = self.queryMODBUS(slaveNo, pdu)
reply = self.queryMODBUS(subordinateNo, pdu)
if len(reply) == 0:
return res
if reply[0] != pdu[0]:
Expand All @@ -1317,12 +1317,12 @@ def modbusWriteBits(self, slaveNo, pduAddr, bits):
res = res + reply[4]
return res

def modbusWriteRegister(self, slaveNo, pduAddr, value):
def modbusWriteRegister(self, subordinateNo, pduAddr, value):
"""
Sets a single internal register (or holding register) on a MODBUS serial device.
This method uses the MODBUS function code 0x06 (Write Single Register).

@param slaveNo : the address of the slave MODBUS device to drive
@param subordinateNo : the address of the subordinate MODBUS device to drive
@param pduAddr : the relative address of the register to set (zero-based)
@param value : the 16 bit value to set

Expand All @@ -1344,20 +1344,20 @@ def modbusWriteRegister(self, slaveNo, pduAddr, value):
pdu.append(((value) & (0xff)))

# // may throw an exception
reply = self.queryMODBUS(slaveNo, pdu)
reply = self.queryMODBUS(subordinateNo, pdu)
if len(reply) == 0:
return res
if reply[0] != pdu[0]:
return res
res = 1
return res

def modbusWriteRegisters(self, slaveNo, pduAddr, values):
def modbusWriteRegisters(self, subordinateNo, pduAddr, values):
"""
Sets several contiguous internal registers (or holding registers) on a MODBUS serial device.
This method uses the MODBUS function code 0x10 (Write Multiple Registers).

@param slaveNo : the address of the slave MODBUS device to drive
@param subordinateNo : the address of the subordinate MODBUS device to drive
@param pduAddr : the relative address of the first internal register to set (zero-based)
@param values : the vector of 16 bit values to set

Expand Down Expand Up @@ -1390,7 +1390,7 @@ def modbusWriteRegisters(self, slaveNo, pduAddr, values):
regpos = regpos + 1

# // may throw an exception
reply = self.queryMODBUS(slaveNo, pdu)
reply = self.queryMODBUS(subordinateNo, pdu)
if len(reply) == 0:
return res
if reply[0] != pdu[0]:
Expand All @@ -1399,13 +1399,13 @@ def modbusWriteRegisters(self, slaveNo, pduAddr, values):
res = res + reply[4]
return res

def modbusWriteAndReadRegisters(self, slaveNo, pduWriteAddr, values, pduReadAddr, nReadWords):
def modbusWriteAndReadRegisters(self, subordinateNo, pduWriteAddr, values, pduReadAddr, nReadWords):
"""
Sets several contiguous internal registers (holding registers) on a MODBUS serial device,
then performs a contiguous read of a set of (possibly different) internal registers.
This method uses the MODBUS function code 0x17 (Read/Write Multiple Registers).

@param slaveNo : the address of the slave MODBUS device to drive
@param subordinateNo : the address of the subordinate MODBUS device to drive
@param pduWriteAddr : the relative address of the first internal register to set (zero-based)
@param values : the vector of 16 bit values to set
@param pduReadAddr : the relative address of the first internal register to read (zero-based)
Expand Down Expand Up @@ -1444,7 +1444,7 @@ def modbusWriteAndReadRegisters(self, slaveNo, pduWriteAddr, values, pduReadAddr
regpos = regpos + 1

# // may throw an exception
reply = self.queryMODBUS(slaveNo, pdu)
reply = self.queryMODBUS(subordinateNo, pdu)
if len(reply) == 0:
return res
if reply[0] != pdu[0]:
Expand Down