RS485 and Python

Because i came to school 2 weeks late, (because of an extended summer vacation) i already had 2 project going and i didn’t knew what to do. In special subject 2, my coordinator teacher “suggested” (told me what to do :D) to play a bit with RS485 and make a script in Python related to that.

Here is a quick system overview:

Description/requirements:

– the client has to communicate to the server using a client/server app made in python

– the server must send various commands to the RS485 via a serial connection

– the client must be able to send commands to the RS485 and get a response back

– the client/server app must have a simple GUI

The device

I played with members of the I-7000 family. I-7000 is a family of network data acquisition and control modules.

– provides analog-to-digital, digital-to-analog, digital input/output, timer/counter and other functions

– can be remotely controlled using a set of commands (DCON protocol)

– communication between the module and the host is in ASCII format via an RS-485 bi-directional serial bus standard

This is the inside :

This is the outside:

The one in the right will act as an adapter, and the one who i’m communicating with is the one in the middle (I-7044).

>>> insert stuff about rs485 <<<<

Time for action!

The first thing to do was to establish the client/server communication which was relatively easy to make. It was my first socket-related programming thing and google helped me with this. The python code can be found on my github. I later added the GUI and modified the script so it would use classes.

The next thing was to connect the server to the RS485 and see if i can talk to it.

I plugged in the USB into my PC. I looked into my computers logs (dmesg) to see on which port it’s connected. It was /dev/ttyUSB0 . I then fired up Putty and established a serial connection using these options(which i know of them from the manuals and from Per, the coordinator teacher):

Baudrate : 9600 bps
Flow control: none
Data bits: 8
Stop bits: 1
Parity: none

Another way to do this was to use the screen command:

screen /dev/ttyUSB0 9600 (where 9600 was the baudrate)

In both the Putty and screen cases i have used the “$022” (without the quotes) command which reads the devices configuration, and as you can see i get a response (which according to the manual, is correct).

After knowing that i can talk to the device, i started looking on how to do that using python. Upon some internet searches i found out about the serial library in python:

import serial

ser = serial.Serial(
    port=’/dev/ttyUSB0′,
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)
ser.open()

More on my github.

Ok, i have the client/server app, established the serial connection, now what? My next challange was to light one LED connected to one of the DOs (digital outputs), and the light other LEDs separately. This was the really tricky part, because we did a bad wiring on the device so the side that we wanted to turn on didn’t had any power supply.

The manual on how to set the digital outputs:

#AABBDD

# is a delimiter character
AA is the address of the reading module(legal values are from 00 to FF)
BB is the output command. For multi-channel output, the BB = 00, 0A or 0B
selects which output group will be used.
DD sets the outputs on or off. Legal values are 0016 to FF16.

I found out that the device i was talking to had the address of 02 (lucky guess). Then i decided to use the “#0200FF” as it can be seen in the picture below.
After issuing the command the LEDs were turned on!

 

After this i had to play a bit with the commands but i was unsuccseful in lighting the LEDs separately. I could light LED2 and then light LED3 but that would turn off LED2. ON how i did that , check the code.