RS-232 APP


I am trying to create an app for the Rasberry PI that will allow me to assign RS-232 commands to a button. The button will execute the send command. Is there any example code available for this?

Something to look at: einhugur

You can use the standard serial controls.
BUT, you need to give the port(s) adequate permissions - otherwise nothing will happen, so check the result of opening the Serial Port.


My open source site is the current host of a serial port example project by Robert Rau that has been floated around the community as a serial port starter project for year. It’s called “Serial Port Test” and can be downloaded from the following URL:

I hope this helps.


Tim, how are permissions given to serial ports? returns true, is that all I need to verify? I have a scale continuously streaming data, and the code shows the data on a PC, but not on my RPi3. The RPI3 port is named /dev/ttyAMAO, and gpio readall shows that both TX and RX are setup with mode IN. This seems to imply that I need to do more to setup the port, XOJO does not seem to be doing it for me.

Hi David.
Yes, on Linux you need to give it permissions. Also on the RPi you need to do the conversion in the RPi Setup to switch the ports since the standard COM port is not very good, since it is created in software. I’ll go through my notes and find the info you need.


Hope this helps:

10/10/16 - UART 
1 - Change 'enable_uart=0'  TO enable_uart=1
1a - sudo nano /boot/config.txt   near the bottom of the file

2 - sudo systemctl stop serial-getty@ttyAMA0.service
3 - sudo systemctl disable serial-getty@ttyAMA0.service

4 - sudo systemctl stop serial-getty@ttyS0.service
5 - sudo systemctl disable serial-getty@ttyS0.service

6 - sudo nano /boot/cmdline.txt
dwc_otg.lpm_enable=0 console=serial0,115200 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline rootwait

6b - remove the line: console=serial0,115200 and save and reboot for changes to take effect.
dwc_otg.lpm_enable=0 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline rootwait

7 - sudo nano /boot/config.txt
7a- Add   dtoverlay=pi3-miniuart-bt
**not use - for reference only**  dtoverlay=pi3-disable-bt

8 - check to be sure it "took"
ls -l /dev

lrwxrwxrwx 1 root root           7 Oct 13 16:21 serial0 -> ttyAMA0       <<====
lrwxrwxrwx 1 root root           5 Oct 13 16:21 serial1 -> ttyS0		  <<====

sudo nano /boot/cmdline.txt  
wc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline rootwait quiet splash plymouth.ignore-serial-consoles

Another set of notes for UART-

One reference:


Finally this got work for my pi3 (os: debian jessie)

please follow these 6 steps carefully.

Step 1 - Install Raspbian Jessie onto a SD card and boot the Pi when connected to a network Login via terminal or desktop and shell Configure the system with:

sudo raspi-config

Expand filesystem and enable serial on advanced page, exit and reboot.

Step 2 -this won't necessary if you have jessie new release Update the system with:

sudo apt-get update

sudo apt-get upgrade

Step 3 - Device Tree settings as below:

Add device tree to /boot/config.txt to disable the Raspberry Pi 3 bluetooth.

sudo nano /boot/config.txt

Add at the end of the file

*if you want to change the blutooth to miniuart port(bad)


*if you want to disable the blutooth(good)


Exit the editor saving your changes.

Step 4 - reboot the pi

sudo reboot

step 5 -

a)to disable the Serial Console edit the file using

sudo nano /boot/cmdline.txt

remove the word phase "console=serial0,115200" or "console=ttyAMA0,115200"

Exit and save your changes

b)to Enable the Serial Console edit the file using

sudo nano /boot/cmdline.txt

Change the file to the following:

dwc_otg.lpm_enable=0 console=tty1 console=serial0(or ttyAMA0),115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Exit and save your changes

Step 6 - reboot the pi

sudo reboot

UART/Serial Port Permissions-

sudo nano /etc/udev/rules.d/50-myusb.rules



** Add these lines **

Also look at

sudo edit /etc/udev/rules.d/50-myusb.rules

Those are my notes, they may be a bit convoluted, but I know that they do work. You can/should check the references too. BTW, these worked with Jesse from 2016/2017. I try not to upgrade/update Linux as oftentime something breaks! In my case, these are not used by people that are surfing and in combination with other common sense precautions I have not experienced any issues with regard to security. The good news is that changing/replacing the uSD card provides a quick fix (other than shipping time!).

Hope that helps.

Chuck, this may be a bit late in the thread to ask, but are you making the connection to the scale via a USB port on the RPi, and a FDDI-equipped USB cable?

The reason I ask is that I’ve built many RPi solutions with UART/FDDI (to DB9, serial) solutions and never had any trouble (no issues with permissions or any special mods needed).

I haven’t absorbed Tim’s notes yet, but it sounds like William’s method using a USB port might be better. I was trying to use the RS232 (GPIO14 and 15), and had not tried USB, as the DB9-serial converters need to have drivers installed when I used them on Windows and Macs, and I was not sure how to install them on a Pi. So I tried a variety of USB-serial adapters connected to my scale, w/o installing any drivers, and my Xojo program does identify the converters as serial port /dev/TTY/USB0, but still doesn’t see any incoming data. I tried FTDI and Prolific converters. I had already made mods that got the GPIO RS232 port working with something called PuTTY, so maybe I need to figure out how to undo those mods, which were:
sudo raspi-config ’ enable serial protocol
sudo systemctl disable serial-getty@ttyso.service 'disable server
sudo nano /boot/cmdline.text ’ remove console=serial0,115200 from text

I have an army of Pi’s that are using (3) serial connections each (two RS-232, the other RS-485). I even have a cool trick (that I’m sure I got off this forum) for auto-connecting to the right port and not making the end-user select the ports at startup. I’ll paste all that stuff in another post to this later today when I get in front of my notes.

When working with USB/TTY/UART/SERIAL stuff, I start by seeing if the RPi/Raspian OS can ‘see’ it by default.

With the FDDI (cable) disconnected, use the terminal command lsusb , and make a note of the list of devices it returns (see output sample below). Then plug your serial device into the USB on the RPi and run lsusb again, and see if your device now shows up. If it shows up in the list then you’re over the most difficult hurdle.

I have a list of my steps taken after this to create my serial object in code (I like making a module for encapsulating my serial-device specific code), and doing a cool trick with Symbolic Links, which let me programmatically find and open serial comms with attached devices, no matter if they jump around on their bus location. Just tell me if I should post all that stuff in your thread - I don’t want to hijack it! :slight_smile:

Sample Output from lsusb
Bus 001 Device 006: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Bus 001 Device 005: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 001 Device 004: ID 0403:6015 Future Technology Devices International, Ltd Bridge(I2C/SPI/UART/FIFO)
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Thanks, I do see the my converters with lsusg, as I can with my Xojo code. I’m ready for the rest! This thread started as a question about how to assign RS232 commands to a button. I think we are still on topic.

Here’s what I’m currently doing…

Back to my previous example, I use the following to identify my device, and then note the attributes, aka ‘ATTRS’, for adding to a .rules file for creating a symbolic link.

Step #1: Use terminal, run the lsusb command, and find the attributes for both ATTRS{idVendor} and ATTRS{idProduct}.

pi@raspberrypi:~ $ lsusb Bus 001 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port

…in this case my attributes are the following…


Step #2: Create a rules file, and create the symbolic link whenever the RPi starts up.

Used cmd: ‘sudo nano /etc/udev/rules.d/99-usb-serial.rules’
Note: the file name is arbitrary, but starts with ’99’ to be at the end of the sequential read-in of all .rules files as is part of the OS behavior.

Added this lines:

ATTRS{idVendor}==“067b”, ATTRS{idProduct}==“2303”, SYMLINK+=“ttyUSB-Anybus”

Note: if edited in TextEdit the “ quotes will be improperly formatted - be certain to replace quotes in the edit tool “nano” in terminal.
Note: had to preface the name of the name of the SYMLINK with “ttyUSB-…” before it started to show up properly.
Note: to ‘see’ the execution of .rules use this command: ‘udevadm test /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.5’

Step #3: Done! Now xojo can see the port named “ttyUSB-Anybus”.

Below is sample code for how I automatically attach to and open the serial device in xojo. It works really well, and accounts for having multiple devices attached and the unpredictable way that the OS assigns Bus# and Device#'s at startup.

[code]Public Function autoConnectUSBPort() as Boolean

Dim i, count As Integer
count = System.SerialPortCount

For i = 0 To count - 1

if System.SerialPort( i ).Name = "/dev/ttyUSB-Anybus" then
  AnyBusRS232.ABSerialPort.SerialPort = System.SerialPort(i)
  if Not AnyBusRS232.ABSerialPort.Open then
    Return False
  end if 
  Return True
end if 

End Function[/code]

I should have noted, this whole concept falls apart if you have two or more devices with identical ATTRS. My work-around for this is to use different FDDI-USB cables for each serial device, so each has a unique fingerprint.

I’ve used USB to Serial cables (FTDI chipset based) in many of projects. I find that this is a great way to have multiple serial devices connected to the Raspberry Pi simultaneously.

When using multiple USB to Serial cables which have the same IDs, I assign each serial device to a specific USB port on the Raspberry Pi and then reference the serial device by the path to the device:

objSerial.SerialPort = System.SerialPort("/dev/serial/by-path/platform-3f980000.usb-usb-0:1.3.4:1.0-port0")

Connecting by path lets you use two or more identical USB to Serial cables from the same manufacturer.

In my projects, the serial devices are always plugged into the same USB port and therefore are always found by their path. If for some reason, you need to move the serial devices to a different set of USB ports, just look in the /dev/serial/by-path/ to see their new path and update your code.

I know that this isn’t very dynamic, but in my case everything is wired up for the customer turn-key style and the physical configuration doesn’t change. If it needs to change, that’s my job not my customer’s.

FTDI USB is a great solution for many cases. In mine I needed on-board solutions…
The only problem with USB cables are that they are not all created equal, but as you stated the FTDI equipped devices are far and away superior!


William, thanks I’m learning a lot about Linux, but not enough yet.
Following your example, Xojo can see ttyUSB-Anybus, along with /dev/ttyAMA0, ttySO, ttyUSB0 and ttyUSB1 as available serial ports, but locks up if I select it.
When I run:
udevadm test /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.5’
The resulting text contains:
unable to open device ‘/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.5’

Why does that describe what I am interested in? I do notice that there is no device 1-1.5 in the directory that string is pointing to, just 1-1.1 to 1-1.3

Hi David - just to make sure I haven’t mislead anyone, let’s step thru it one more time. My solution differs from the one posted by Wes, in that mine is more elementary and less flexible :slight_smile:

Step #1: from a terminal prompt, type lsusb, and paste the results into a reply on this post. Then I’ll tell you what I would do (if it were me) in a 2nd, 3rd step.

p.s.- I only know a fraction of Linux, and often find myself accidentally trying to us MS-DOS and Unix commands.

Thanks, William. Given that I have the exact same Prolific adaptor as you, I just copied and pasted your code into the rule file:
ATTRS{idVendor}==“067b”, ATTRS{idProduct}==“2303”, SYMLINK+=“ttyUSB-Anybus”
I also modified it for the FTDI adaptor, and same lack of response. My scale is continuously sending weights, which I have verified by running the same code on a PC after swapping cables and selecting the relevant PC port. I see DataAvailable right away on the PC, but nothing on the Pi.
Here is my lsusb output:
Bus 001 Device 006: ID 093a:2510 Pixart Imaging, Inc. Optical Mouse
Bus 001 Device 007: ID 05ac:0204 Apple, Inc.
Bus 001 Device 004: ID 05ac:1002 Apple, Inc. Extended Keyboard Hub [Mitsumi]
Bus 001 Device 008: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 001 Device 005: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Bus 001 Device 009: ID 0424:7800 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 002: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Wes, I would like to try your method also, but how do you determine the path to your devices?