Beispiel #1
0
    def create_outgoing_frame(self, a_unit, a_reg, a_count, a_data, a_type):
        if a_count is None:
            a_count = 0

        if isinstance(a_reg, str):
            a_reg = int(a_reg)-1
            a_reg = str(a_reg)

        tx_frame = '{0} {1} {2} '.format(str_hex(a_unit, 2), get_fc(a_type, a_count), str_hex(a_reg, 4))

        # Fill frame based on supported function code
        if a_type == mb_cmd[2]:
            # Write single register, as the data is sent as hexadecimal number, we need to convert to string
            if a_count == 1:
                tx_frame = tx_frame + "{0} ".format(str_hex(str(a_data), 4))

            # In case of multiple write:
            # ID + FC + Reg + Count + Count (bytes, 8-bit wide) + data
            else:
                tx_frame = tx_frame + "{0} {1} ".format(str_hex(a_count, 4), str_hex(str(int(a_count)*2), 2))
                for val in a_data:
                    tx_frame = tx_frame + "{0} ".format(str_hex(val, 4))

        # Read operation, in both holding/input register types the structure is the same
        else:
            tx_frame = tx_frame + "{0} ".format(str_hex(a_count, 4))

        # Calculate CRC and append
        crc_frame = tx_frame.replace(' ', '').decode('hex')
        tx_frame = tx_frame + '{0}'.format(str_hex(computeCRC(crc_frame), 4))
        # print tx_frame
        return tx_frame
Beispiel #2
0
 def buildPacket(self, message):
     packet = struct.pack('>BB',
         message.unit_id,
         message.function_code) + message.encode()
     packet += struct.pack(">H", computeCRC(packet))
     packet = '%s%s%s' % (self.__start, packet, self.__end)
     return packet
Beispiel #3
0
def append_crc(builder):
    crc = ut.computeCRC(ut.make_byte_string(builder.to_string()))
    #print(crc)
    hex_str = '%04x' % crc
    hex1 = int(hex_str[0] + hex_str[1], 16)
    hex2 = int(hex_str[2] + hex_str[3], 16)

    builder.add_8bit_uint(hex1)
    builder.add_8bit_uint(hex2)
Beispiel #4
0
    def buildPacket(self, message):
        ''' Creates a ready to send modbus packet

        :param message: The populated request/response to send
        '''
        data = message.encode()
        packet = struct.pack('>BB', message.unit_id,
                             message.function_code) + data
        packet += struct.pack(">H", computeCRC(packet))
        return packet
Beispiel #5
0
    def buildPacket(self, message):
        ''' Creates a ready to send modbus packet

        :param message: The populated request/response to send
        '''
        data = message.encode()
        packet = struct.pack('>BB',
            message.unit_id,
            message.function_code) + data
        packet += struct.pack(">H", computeCRC(packet))
        return packet
Beispiel #6
0
    def buildPacket(self, message):
        ''' Creates a ready to send modbus packet

        :param message: The request/response to send
        :returns: The encoded packet
        '''
        data = self._preflight(message.encode())
        packet = struct.pack('>BB', message.unit_id,
                             message.function_code) + data
        packet += struct.pack(">H", computeCRC(packet))
        return packet
    def buildPacket(self, message):
        """
        Creates a ready to send modbus packet

        :param message: The populated request/response to send
        """
        data = message.encode()
        packet = struct.pack(RTU_FRAME_HEADER, message.unit_id,
                             message.function_code) + data
        packet += struct.pack(">H", computeCRC(packet))
        message.transaction_id = message.unit_id  # Ensure that transaction is actually the unit id for serial comms
        return packet
Beispiel #8
0
    def buildPacket(self, message):
        ''' Creates a ready to send modbus packet

        :param message: The request/response to send
        :returns: The encoded packet
        '''
        data = self._preflight(message.encode())
        packet = struct.pack('>BB',
                message.unit_id,
                message.function_code) + data
        packet = packet + struct.pack("<H", computeCRC(packet))
        return packet
Beispiel #9
0
    def buildPacket(self, message):
        """ Creates a ready to send modbus packet

        :param message: The request/response to send
        :returns: The encoded packet
        """
        data = self._preflight(message.encode())
        packet = struct.pack(BINARY_FRAME_HEADER, message.unit_id,
                             message.function_code) + data
        packet += struct.pack(">H", computeCRC(packet))
        packet = self._start + packet + self._end
        return packet
Beispiel #10
0
    def buildPacket(self, message):
        """ Creates a ready to send modbus packet

        :param message: The request/response to send
        :returns: The encoded packet
        """
        data = self._preflight(message.encode())
        packet = struct.pack(BINARY_FRAME_HEADER,
                             message.unit_id,
                             message.function_code) + data
        packet += struct.pack(">H", computeCRC(packet))
        packet = self._start + packet + self._end
        return packet
Beispiel #11
0
    def buildPacket(self, message):
        """
        Creates a ready to send modbus packet

        :param message: The populated request/response to send
        """
        data = message.encode()
        packet = struct.pack(RTU_FRAME_HEADER,
                             message.unit_id,
                             message.function_code) + data
        packet += struct.pack(">H", computeCRC(packet))
        message.transaction_id = message.unit_id  # Ensure that transaction is actually the unit id for serial comms
        return packet
def make_modbus(m):
    method = m[0]

    if method == 'tcp':
        return TcpClient(m[1], int(m[2]))

    if method == 'udp':
        return UdpClient(m[1], int(m[2]))

    tty = m[1]
    rate = int(m[2])

    if tty in serial_ports:
        client = serial_ports[tty]
        if client.baudrate != rate:
            raise Exception('rate mismatch on %s' % tty)
        return client.get()

    dev = '/dev/%s' % tty
    client = SerialClient(method, port=dev, baudrate=rate)
    if not client.connect():
        client.put()
        return None

    serial_ports[tty] = client

    # send some harmless messages to the broadcast address to
    # let rate detection in devices adapt
    packet = bytes([0x00, 0x08, 0x00, 0x00, 0x55, 0x55])
    packet += struct.pack('>H', computeCRC(packet))

    for i in range(12):
        client.socket.write(packet)
        time.sleep(0.1)

    return client