Announcement

Collapse
No announcement yet.

Communication using MODBUS RTU

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Communication using MODBUS RTU

    Hello,

    I have a Lantronix PremierWave XN device which is communicating with anemometers using the MODBUS RTU protocol. I am using python with the minimalmodbus package (https://pypi.python.org/pypi/MinimalModbus/). Currently, I am able to communicate with the device, however, I am unable to understand the error I am getting.

    The code :
    Code:
    #!/usr/bin/env python
    import minimalmodbus
    
    print "WTF!"
    
    port = '/dev/ttyS1'
    instrument = minimalmodbus.Instrument(port, 2) # port name, slave address (in decimal)
    instrument.serial.baudrate = 115200
    instrument.serial.bytesize = 8
    instrument.serial.stopbits = 1
    instrument.serial.timeout = 1
    instrument.debug = True
    instrument.mode = minimalmodbus.MODE_RTU
    print instrument
    
    wind_speed = instrument.read_float(1, functioncode=4,numberOfRegisters=2)
    print "Wind Speed ", wind_speed
    The error I am getting is :

    Code:
    WTF!
    minimalmodbus.Instrument<id=0x405c17b8, address=2, mode=rtu, close_port_after_each_call=False, precalculate_read_size=True, debug=True, serial=Serial<id=0x405bdb98, open=True>(port='/dev/ttyS1', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=False, rtscts=False, dsrdtr=False)>
    
    MinimalModbus debug mode. Writing to instrument (expecting 9 bytes back): '\x02\x04\x00\x01\x00\x02 8' (02 04 00 01 00 02 20 38)
    MinimalModbus debug mode. No sleep required before write. Time since previous read: 94447227.0 ms, minimum silent period: 0.33 ms.
    MinimalModbus debug mode. Response from instrument: '\x00\x00\xff\x00\xff\x00\x00\x8c\xff' (00 00 FF 00 FF 00 00 8C FF) (9 bytes), roundtrip time: 21.8 ms. Timeout setting: 1000.0 ms.
    
    Traceback (most recent call last):
      File "/ltrx_user/python_modbus.py", line 16, in <module>
        wind_speed = instrument.read_float(1, functioncode=4,numberOfRegisters=2)
      File "/usr/lib/python2.7/site-packages/minimalmodbus.py", line 392, in read_float
        return self._genericCommand(functioncode, registeraddress, numberOfRegisters=numberOfRegisters, payloadformat='float')
      File "/usr/lib/python2.7/site-packages/minimalmodbus.py", line 697, in _genericCommand
        payloadFromSlave = self._performCommand(functioncode, payloadToSlave)
      File "/usr/lib/python2.7/site-packages/minimalmodbus.py", line 798, in _performCommand
        payloadFromSlave = _extractPayload(response, self.address, self.mode, functioncode)
      File "/usr/lib/python2.7/site-packages/minimalmodbus.py", line 1075, in _extractPayload
        raise ValueError(text)
    ValueError: Checksum error in rtu mode: '\x8c\xff' instead of '?$' . The response is: '\x00\x00\xff\x00\xff\x00\x00\x8c\xff' (plain response: '\x00\x00\xff\x00\xff\x00\x00\x8c\xff')
    [24030 refs]

    Can someone explain me the problem I am facing.

    Thanks,
    Balkrishna.

  • #2
    The minimalmodbus program you're using is raising an exception because checksum received doesn't match calculated. See line 1075 of the program you're using:

    https://github.com/pyhys/minimalmodb...nimalmodbus.py

    It doesn't seem like this is related to the PremierWave XN specifically, but probably has more to do with the Modbus slave you have attached. Seems to be a fairly common problem. See the following threads that might help you:

    http://electronics.stackexchange.com...-outputs-error
    http://stackoverflow.com/questions/2...uts-valueerror
    http://control.com/thread/1026186143

    Comment


    • #3
      Thanks for the reply, mariano. Could figure out how to solve the issue yet. Working on it.

      Comment


      • #4
        Thanks for all the help. I figured out the problem. It had to do with the way the "Line" was configured on the PremierWave. Also with the option whether termination resistors are enabled or disabled.

        I had another question, Is it possible to use the xSenso with python too ? If so do you have any document for the same ?

        Comment


        • #5
          Glad to hear that it's working. As for the xSenso, it doesn't have Python capabilities built in.

          Comment


          • #6
            Thanks for the help mariano. Now I am programming the lantronix to communicate using the ASCII protocol but with little success. The device does communicate via the MODBUS-RTU protocol however it doesnt do so successfully with the ASCII protocol. The program runs but there is no output generated.
            Code:
            import serial
            print "WTF!"
            
            port = '/dev/ttyS1'
            
            #Instrument 1
            slave = 2
            print slave,port
            
            ser = serial.Serial(
                port,
                baudrate=19200,
                parity=serial.PARITY_NONE,
                stopbits=serial.STOPBITS_ONE,
                bytesize=serial.EIGHTBITS
            )
            #ser.open()
            print "Close"
            ser.close()
            print "Open"
            ser.open()
            message='#02'
            while True:
            	ser.write(message+'\r')
            	response=ser.read(1000)
            	print response
            
                # #closing the serial connection
            ser.close()
            I have confirmed the slave address and the message should return some output which shows the device is connected.

            My question is what configurations should be adopted for the "Line" in PremierWave ? And is there any error I am making in the script ?

            Comment


            • #7
              The configuration for the Line needs to be set to "None", since your Python script is opening the serial line, instead of the Tunnel protocol.

              Since you're using /dev/ttyS1, are you connected to serial port 2 on the PremierWave XN? The XN has /dev/ttyS1 connected to serial port 2, and /dev/ttyS2 connected to serial port 1. So that might be the issue as well.

              Mariano

              Comment


              • #8
                Thanks for your reply. I am using serial port 2. So no worries with it. I changed the line to None however, I am still stuck when the program tries to readout from the device... I mean the read function
                Code:
                response=ser.read(1000)
                I think I have to check with the manufacturer of the wattmeter we are using.

                Thanks,
                Balkrishna.

                Comment


                • #9
                  Is your device supposed to send you 1000 bytes?

                  The ser.read(1000) will block indefinitely until it receives 1000 bytes, since the serial port was opened without a timeout.

                  Comment


                  • #10
                    Thanks. I made the corrections ... I got the program working with a serial port connection to the PC ... however am not getting any data output from the lantronix .... To be specific ...
                    From the PC :
                    Code:
                    connected to : COM7
                    6
                    ['>0.00000;0.00000;0.01758;0.00000;------.\r']
                    ['>0.00000;0.00000;0.01758;0.00000;------.\r']
                    ['>0.00000;0.00000;0.01758;0.00000;------.\r']
                    ['>0.00000;0.00000;0.01758;0.00000;------.\r']
                    ['>0.00000;0.00000;0.01758;0.00000;------.\r']
                    While from the lantronix

                    Code:
                    connected to : /dev/ttyS1
                    6
                    []
                    []
                    []
                    []
                    []
                    [24034 refs]

                    Both these outputs for the program :

                    Code:
                    #!/usr/bin/env python
                    import serial
                    import time
                    
                    
                    
                    print "WTF!"
                    
                    port = '/dev/ttyS1' or 'COM7' in case of windows
                    
                    #Instrument 1
                    slave = 2
                    print slave,port
                    
                    ser = serial.Serial(
                        port,
                        baudrate=19200, 
                    	timeout=1,
                        parity=serial.PARITY_NONE,
                        stopbits=serial.STOPBITS_ONE,
                        bytesize=serial.EIGHTBITS
                    )
                    print ser.isOpen()
                    print("connected to : " + ser.portstr)
                    print ser.write('#027W\r'.encode())
                    
                    cnt=0
                    while True:
                    	ser.write('#027W\r'.encode())
                    	response = ser.readlines()
                    	print (response)
                    	cnt=cnt+1
                    	if (cnt >= 5):
                    		break
                    	
                    ser.close()

                    Comment


                    • #11
                      I got it working .... Changing the line to "Tunnel" worked.

                      Comment


                      • #12
                        Hi ... The output read by the lantronix premierwave xc is quite strange. Instead of normal characters I am seeing a weird output as follows :
                        Code:
                        Anemometer connected to : /dev/ttyS2
                        
                        ['\x00KKKk2\x06\x06\x06\x06\x19v\x06\x06\x06&\x06F6\x06f\x16\x0b\x0bk6F\xd6\n']
                        ['\x00KKKk\x96\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06&\x06F\x16\x06f\x16\x0b\x0bk&&k\n']
                        ['\x00KKKk2\x06\x06\x06\x06YV\x06\x06\x06&\x06F6\x06f\x16\x0b\x0bk66\xd6\n']
                        ['\x00KKKk\x96\x06\x06\x06\x06\x06\x16f\x06\x06\x06&\x06FF\x06f\x16\x0b\x0bk\x8b6V\xd6\n']
                        ['\x00KKKk\x96\x06\x06\x06\x06\x06\x06v\x06\x06\x06&\x06F3f\x16\x0b\x0bk6V\xd6\n']
                        ['\x00KKKk\x963\x06\x06\x06\x06\x06\x06v\x06\x06\x06&\x06F3f\x16\x0b\x0bk6V\xd6\n']
                        ['\x00KKKk2\x06\x06\x06\x06\x19\x06\x06\x06\x06&\x06FF\x06f\x16\x0b\x0bk&Vk\n']
                        ['\x00KKKk\x963\x06\x06\x06\x06\x06\x16\x06\x06\x06\x06&\x06FF\x06f\x16\x0b\x0bk&fk\n']
                        ['\x00KKKk\x8b\x96\x06\x06\x06\x06\x06\x16V\x06\x06\x06&\x06FF\x06f\x16\x0b\x0bk6F\xf8']
                        ['\x00KKKk\x96\x06\x06\x06\x06\x06\x16f\x06\x06\x06&\x16\x86v\x06f\x16\x0b\x0bk\x8b6Fk\n']
                        [24058 refs]
                        I am not able to make sense of this output at all. Also this output is not seen when using the same data acquisition program with PC. Anyway out of this situation ?
                        Last edited by iceman; 04-06-2016, 01:34 PM. Reason: changed the name of the device

                        Comment

                        Working...
                        X