Many of the older Additel devices can communicate over RS-232 cables with Serial Communication. In addition, a few of our devices, even though they use USB cables, still communicate using Serial Communication. This method of communication is very simple and effective, provided you get the paramaters correct.
First, make sure you get the driver for your particular RS-232 cable installed. If you don't have the driver, your system is not going to be able to communicate with the device. There is a chance that Windows will automatically install the driver for your cable, but for most cable manufacturers, you will need to install it from their website. Make sure you get it directly from the manufacturers website - and not another website pretending to offer drivers, but really offering you malware.
In this example, we are going to use Python 3 (in this case, version 3.9.1, although newer versions should work fine too), which you can download here or here.
You'll also need to install pySerial, by following the instructions here. pySerial is a 3rd party library that makes communicating with serial devices really easy.
# import the pyserial library
import serial
# open a serial port
port = serial.Serial( port='COM1',
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1 )
# encode the command to get the device SN in bytes, and write it to the device
command = "255:R:OCODE:1\r\n"
command_in_bytes = bytes(command, 'utf-8')
port.write(command_in_bytes)
# read the first 500 characters (or less if it times out), convert it to a string, and print it
response = port.read(size=500)
response_as_str = bytes.decode(response, 'utf-8')
print(response_as_str)Let's go over this example step by step:
- First, we get the pySerial library loaded.
# import the pyserial library
import serial- Next, we open the serial port. This is probably the hardest part, because you need to make sure you open the serial port with the correct settings. These settings are determined in different ways. For example:
-
The Port is automatically generated by your computer once you plug in your RS232 cable and install it's driver (if you don't have a driver installed, you can't interact with the cable). On Windows, this looks like
COM1orCOM2. On other platforms, it will probably be/dev/ttyS1or/dev/ttyS2. You can see a list of these generated ports by using pySerial'sserial.tools.list_ports, or on Windows, by going to Device Manager, and looking under thePorts (COM & LPT)section. -
The Baud Rate is set by your Additel device. You can oftentimes set it via going into the device settings. To learn how to do this, check out your device manual on additel.com. The default for all Additel devices is
9600, so if you can't figure out what it is that's a good place to start. -
The Bytes Size and Stop Bits can be changed on some Additel devices, but not on others. The default for many Additel devices is bytesize of
8or7, and stopbits of1or2, so try a few combinations of those. -
The Parity is always
None.
# open a serial port
port = serial.Serial( port='COM1',
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1 )- Once you get the serial port set up, you need to write some data to the port. Unfortunately, the port only likes binary data so you need to convert it to bytes before sending the command (other serial libraries may not require you to do this). You can find these commands here, if they are available. Commands for Additel units over serial tend to come in one of two structures:
-
The first command structure is called SCPI. SCPI looks like this:
MEAS:PRES1?\r\n. It consists of several segments separated by colons that describe what an operation does (in this case,MEAS:PRES1measures pressure from Sensor 1). Commands asking for information are followed by a?indicating they are a query. Paramaters are separarated from the command and other paramaters by a comma. Finally, the command terminates with\r\n. -
The second command structure doesn't have a name. It looks like this:
255:R:OCODE:1\r\n. It consists of several segments, separated by colons. The first segment255references the address (we always reccomend using255). The next is usually anRorW(for read or write). The third is the actual command - in this case, it isOCODE, a command which gets a unit's serial number (this command will differ from device to device). Then, after that you put paramaters, separated by colons (if you have no paramaters, just stick a1there). Finally, the command terminates with\r\n.
# encode the command to get the device SN in bytes, and write it to the device
command = "255:R:OCODE:1\r\n"
command_in_bytes = bytes(command, 'utf-8')
port.write(command_in_bytes)- Finally you can recieve the response from the Additel device through the port. pySerial works by specifying an amount of characters you'd like to recieve, and also has a timeout if things take too long (other libraries may do this differently). The data is returned as bytes, and you need to turn it into a string before printing it.
# read the first 500 characters (or less if it times out), convert it to a string, and print it
response = port.read(size=500)
response_as_str = bytes.decode(response, 'utf-8')
print(response_as_str)And that is pretty much it. You can now communicate with your Additel devices with Serial Communication.