element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Sci Fi Your Pi
  • Challenges & Projects
  • Design Challenges
  • Sci Fi Your Pi
  • More
  • Cancel
Sci Fi Your Pi
Blog Escape From the Past: RS-422 Communications
  • Blog
  • Forum
  • Documents
  • Files
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: frellwan
  • Date Created: 24 Jul 2015 6:52 PM Date Created
  • Views 2644 views
  • Likes 4 likes
  • Comments 1 comment
  • escape_the_past
Related
Recommended

Escape From the Past: RS-422 Communications

frellwan
frellwan
24 Jul 2015

As mentioned in a previous post I will be using python Twisted architecture for the asynchronous communications that are required I chose to use twisted partly because I was interested in learning more about it but also because it supports all the functionality that I needed serial communication(for  RS-232RS-232 and RS-422 protocols FTP and SMTP all in one package Twisted certainly is capable of much more but these are the communication channels I need to support

 

All of the popular Linux distributions maintain a python-twisted package as well as packaged versions of Twisted’s dependencies. To install Twisted on a dpkg-based system, run:

sudo apt-get install python-twisted

 

 

To verify that the installation worked and that you have the desired version of Twisted installed, import the twisted module from a Python prompt:

$ python
>>> 
Python 2.7.3 (default, Mar 8 2014, 05:13:23)
Type "help", "copyright", "credits" or "license" for more information.
>>> import twisted
>>> twisted.__version__
'1.0.0'
[GCC 4.6.3] on linux2

 

If the import twisted statement runs with no errors, you have a working Twisted installation.

 

 

RS-422 communication

I am communicating to a Fenner M-Trim motor controller over a RS-422 serial connection. The protocol can be found in the User Manual in chapter 7 (http://www.contrexinc.com/PDF/UserManuals/M-TrimUserManual.pdf). The protocol is a simple 12 byte ASCII string and uses ‘\x02’ as the STX (start of transmission) and ‘\x03’ as the ETX (end of transmission).

 

The protocol looks like this:

Char#

1

2

3

4

5

6

7

8

9

10

11

12

Desc

STX

DEV# 10’s

DEV# 1’s

MSG Type

Par# 10’s

Par# 1’s

Data 1000’s

Data 100’s

Data 10’s

Data 1’s

Data Format

ETX

 

The MSG Type determines whether it is a read or write packet. The Data format determines the decimal position for the data.

 

The code for this portion of the project can be found at https://github.com/frellwan/SciFy-Pi.git in the Serial/MTrim folder.

The finished code will differ slightly due to the fact that the communication on this port will be driven by the SLC 5/04 processor, but for testing purposes I have added code to read and write to parameters to show the concept.

 

As mentioned in a previous post I am using a USB to RS422 cable(USB-RS422-WE-1800-BTUSB-RS422-WE-1800-BT) to be able to communicate to the Fenner M-Trim controller. The data sheet can be found here: USB-RS422-FTDI

The cable pinout can be seen below:

image

The TXD-/TXD+ on the cable is connected to the RX-/RX+ on the MTrim and the RX-/RX+ on the cable is connected to the TX-/TX+ on the MTrim. The GND on the cable is connected to the GND on the MTrim. Once connected it works very well. The USB side of the cable has 2 LED's - a red for transmit and a green for receive. So it is very easy to see when signal transmission is happening. With the exception of the price of this cable, I am very pleased with its performance.

 

 

I borrowed heavily from the Pymodbus source code. This package is well proven and seemed like a good starting point. The serialport instantiating is very simple

class SerialMTrimClient(serialport.SerialPort):

  def __init__(self, factory, *args, **kwargs):
  ''' Setup the client and start listening on the serial port

  :param factory: The factory to build clients with
  '''
  protocol = factory.buildProtocol()
  self.decoder = ClientDecoder()
  serialport.SerialPort.__init__(self, protocol, *args, **kwargs)


Options = Options()
config = SafeConfigParser()
config.read([options['config']])

framer = AsciiFramer(ClientDecoder())
factory = MTrimFactory(framer)

RS422port = config.get('RS-422', 'host')
RS422baud = config.getint('RS-422', 'baudrate')

SerialMTrimClient(factory, RS422port, reactor, baudrate = RS422baud)

log.debug("Starting the client")

reactor.run()

 

At this point the MTrimProtocol is now responsible for encoding and sending the ASCII string to the MTrin controller.

The Mtrim has 3 types of commands – read, write and control command. Each command type has a different way to encode the data, so they each have their own code for encoding and decoding messages.

class ParameterSendRequest(PDU):
    '''
    Class for writing a MTrim register
    '''
    _frame_size = 12

    def __init__(self, address=1, parameter=1, value=0, **kwargs):
        ''' Initializes a new instance

        :param address: The address of the device to write to
        :paramater: The parameter to write to
        :value: The value to write to the parameter
        '''
        PDU.__init__(self, **kwargs)
        self.address = address
        self.parameter = parameter
        self.msgType = '3'
        self.value = value
        self.Data = ''
        self.dataFormat = '0'
        self.packet = ''

    def encode(self):
        ''' Encodes the request packet

        :return: The encoded packet
        '''
        if (not self.skip_encode):
            self.packet = struct.pack('>B', 0x02)        #STX
            if (self.address <10):
                self.packet += '0' + str(self.address)
            else:
                self.packet += str(self.address)

            self.packet += self.msgType

            if (self.parameter < 10):
                self.packet += '0' + str(self.parameter)
            else:
                self.packet += str(self.parameter)

            strValue = str(self.value)
            if(self.parameter in [20,21,22,23]):
                if (self.value >= 0):
                    if strValue.find('.') == -1:
                        self.dataFormat = '0'
                    else:
                        self.dataFormat = str((len(strValue)-1)-strValue.find('.'))
                if (self.value < 0):
                    if strValue.find('.') == -1:
                        self.dataFormat = '4'
                    else:
                        self.dataFormat = str((len(strValue)-1)-strValue.find('.')+4)
            else:
                self.dataFormat = '0'
              
            if strValue.find('.') == -1:
                for c in range(4-len(strValue)):
                    self.Data += '0'
                self.Data += strValue
            else:
                for c in range(5-len(strValue)):
                    self.Data += '0'
                self.Data += "".join(strValue.split('.'))
                  
            self.packet += self.Data
            self.packet += self.dataFormat
            self.packet += struct.pack('>B', 0x03)       #ETX
                      
        return self.packet

    def decode(self, data):
        ''' Decode a register request packet

        :param data: The request to decode
        '''
        self.address = int(data[1])*10 + int(data[2])
        self.msgType = data[3]
        self.parameter = int(data[4])*10 + int(data[5])
        self.dataFormat = int(data[10])

        self.value = int(data[6:10])
        if (self.dataFormat <= 3):
            self.Data = self.value/(10**self.dataFormat)
        else:
            self.Data = -1 * self.value/(10**(self.dataFormat-4))

        return self
      
    def __str__(self):
        ''' Returns a string representation of the instance

        :returns: A string representation of the instance
        '''
        return "ParameterSendRequest (%d,%d,%d)" % (self.address, self.parameter, self.Data)

 

Here is some video - it didn't turn out as well as I had hoped - hopefully I can get a better video soon

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

  • Sign in to reply
  • balearicdynamics
    balearicdynamics over 10 years ago

    Nice work. Waiting for your next video.

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube