添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

When using Modbus/TCP to access registers on CS robot, each register stores 16 bit of data, which means it can store the number from 0 to 65535. When using external devices such like PLC to write a float to robot by Modbus/TCP, how to make the robot read the data properly?

The example below will write a float to register 256 and 257.

1. Create 2 register IO to handle the data on 256 and 257, name them m256 and m257. Make sure to select data type as Register Output.

2. In the software Modbus Poll, connect to the robot, use Function 16 to write data to multiple registers. Change data Display to float CD AB. Then write value 1.0 to register 256.




Load the script file: modbus_float, which can handle register data conversion. Functions for handling floats are packed in the script file.

Assuming using Simens PLC to send float to robot. Since Simens PLC puts the lower byte left, the argument used for function getModbusFloat need to reverse the order. Use Assignment instruction to store the data returned by the function to the local variable var_1.

The content of modbus_float.script is:

import struct
def Bytes2Float(d,BigEndian=False):
# Input 4 byte of data, convert it into 32 bit Float. Using Little Endian Encoding as default. If need to use Big Endian Encoding, please set the second argument to True.
    if BigEndian ==True:
        return struct.unpack('<f struct.pack="" else:="" return="" struct.unpack="">f', struct.pack('4B', *d))[0]
def Bytes2Int32(d,BigEndian=False):
# Input 4 byte of data, convert it into 32 bit signed integer. Using Little Endian Encoding as default. If need to use Big Endian Encoding, please set the second argument to True.
    if BigEndian ==True:
        return struct.unpack('<i struct.pack="" else:="" return="" struct.unpack="">i', struct.pack('4B', *d))[0]
def Bytes2Int16(d,BigEndian=False):
    # Input 2 byte of data, convert it into 16 bit signed integer. Using Little Endian Encoding as default. If need to use Big Endian Encoding, please set the second argument to True.
    if BigEndian ==True:
        return struct.unpack('<h struct.pack="" else:="" return="" struct.unpack="">h', struct.pack('2B', *d))[0]
def Float2Bytes(d,BigEndian=False):
    # Input a 32 bit float, convert it into 4 byte data . Using Little Endian Encoding as default. If need to use Big Endian Encoding, please set the second argument to True.
    if BigEndian ==True:
        return list(struct.unpack('4B',struct.pack('f',d)))
    else:
        return list(struct.unpack('4B',struct.pack('>f',d)))
def Int322Bytes(d,BigEndian=False):
    # Input a 32 bit signed integer, convert it into 4 byte data . Using Little Endian Encoding as default. If need to use Big Endian Encoding, please set the second argument to True.
    if BigEndian ==True:
        return list(struct.unpack('4B',struct.pack('<i else:="" return="" list="">i',d)))
def Int162Bytes(d,BigEndian=False):
    # Input a 16 bit signed integer, convert it into 2 byte data . Using Little Endian Encoding as default. If need to use Big Endian Encoding, please set the second argument to True.
    if BigEndian ==True:
        return list(struct.unpack('2B',struct.pack('<h else:="" return="" list="">h',d)))
def getModbusFloat(m_low,m_high):
    # get the data stored in m_low and m_high. m_low and m_high are Modbus Variable name
    # m_low stores data in the lower word, m_high stores data in the higher word
    # Which means the format is CD, AB
    low = modbus_get_signal_status(m_low)
    high = modbus_get_signal_status(m_high)
    d= [high>>8,high&255,low>>8,low&255]
    return Bytes2Float(d)
def setModbusFloat(data,m_low,m_high):
    # Convert the float to 2 unsigned 16 bit word, lower word stores in m_low, higher word stores in m_high 
    # Data format is CD,AB
    # so m_low is AB, m_high is CD
    d = Float2Bytes(data)
    low = d[2]*256+d[3]
    high = d[0]*256+d[1]
    modbus_set_output_register(m_low, low)
    modbus_set_output_register(m_high, high)</h></i></h></i></f>

The robot can write a float to 2 continues registers. Then the external device such like PLC can read float from these two continues registers.

For example:

Assuming robot has written a float to register 258 and 259. (Prepare them in Modbus Configuration)

In Modbus Poll, change the function code to 03 to read register, keep display using float CD AB.


In the robot task, use the function setModbusFloat(2.8,'m258','m259') for writing registers.

In Modbus Poll, the result read is 2.8

If the upper devices like Simen PLC are using reversed word order, just call function setModbusFloat with reversed second and third argument. Just like setModbusFloat(2.8,'m259','m258')

点击显示全文