RS-232 APP

  1. ‹ Older
  2. 5 months ago

    David G

    May 10 Pre-Release Testers, Xojo Pro Phoenix, AZ

    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

  3. William R

    May 10 Pre-Release Testers, Xojo Pro Irvine, California USA

    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.

  4. William R

    May 10 Pre-Release Testers, Xojo Pro Irvine, California USA

    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! :-)

    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

  5. David G

    May 10 Pre-Release Testers, Xojo Pro Phoenix, AZ

    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.

  6. William R

    May 10 Pre-Release Testers, Xojo Pro Irvine, California USA

    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...

    ATTRS{idVendor}=="067b"
    ATTRS{idProduct}=="2303"

    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.

    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 
        
      Next
    End Function
  7. William R

    May 10 Pre-Release Testers, Xojo Pro Irvine, California USA

    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.

  8. Wes W

    May 10 Pre-Release Testers, Xojo Pro MD, Columbia USA

    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.

  9. Tim S

    May 10 Pre-Release Testers, Xojo Pro Phoenix Arizona USA

    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!

    Tim

  10. 4 months ago

    David G

    May 15 Pre-Release Testers, Xojo Pro Phoenix, AZ

    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

  11. William R

    May 15 Pre-Release Testers, Xojo Pro Irvine, California USA

    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 :-)

    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.

  12. David G

    May 15 Pre-Release Testers, Xojo Pro Phoenix, AZ

    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

  13. David G

    May 15 Pre-Release Testers, Xojo Pro Phoenix, AZ

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

  14. William R

    May 16 Pre-Release Testers, Xojo Pro Irvine, California USA

    Try opening/running the sample file (shipped with xojo) named "Serial Port Bar Code Reader Example", and see if your device, named "ttyUSB-Anybus" appears in the drop-down menu.

    If it appears in the drop-down then it's on to the next step, if not then we go back to examining the .rule file...

    :: here's where the template is on my drive, in case you need some breadcrumbs ::
    /Applications/Xojo 2018 Release 1/Example Projects/Communication/Serial/Serial Port Bar Code Reader Example.xojo_binary_project

  15. David G

    May 16 Pre-Release Testers, Xojo Pro Phoenix, AZ

    Sorry if I wasn't clear, but "ttyUSB-Anybus" does appear in the drop-down menu in my code, the same as in code you suggested. With nothing connected to the USB ports, the pull down options are:
    /dev/ttyAMA0
    /dev/ttyS0
    With the FDTI converter attached two additional options appear:
    /dev/ttyUSB-Anybus
    /dev/USB0
    None show the expected incoming data on the Pi, but the same programs do show it arriving on Comm1 when code is run on PC.

  16. Wes W

    May 16 Pre-Release Testers, Xojo Pro MD, Columbia USA

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

    Look in the /dev/serial/by-path/ folder to see the paths to your available serial devices.

    -Wes

  17. William R

    May 16 Pre-Release Testers, Xojo Pro Irvine, California USA

    Hi David,

    What happens when you call the .Open method against that port (sample code below), does it return true?

    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 
        
      Next
    End Function
  18. David G

    May 16 Pre-Release Testers, Xojo Pro Phoenix, AZ

    I just realized I had not modified Xojo's example which was waiting for specific characters before displaying incoming data. I had removed that restriction to compile on my PC, but had not uploaded the corresponding build to the Pi. When I did so, the Pi did start receiving the serial data. Now I need to figure out why code crashes when I select the port. I don't expect that will be too hard to figure out. Thanks for your help, I've been quite lost until now!

  19. 3 months ago

    David G

    Jun 17 Pre-Release Testers, Xojo Pro Phoenix, AZ

    This serial port is really wearing me out! My first Pi was working for a while, but eventually stopped, and it took a while to figure out that it was not reacting to baud rate changes, it was stuck 57k baud, and I needed 9600. A new Pi worked right away, but within a few minutes stopped. The Pi itself seems to be working fine, but I can no longer get the serial port to send anything, despite it opening fine. The scale I am trying to communicate with is continuously sending readings toggling between 0 and 12V, connected to the Pi via a FDTI serial-USB converter. Could this voltage be a problem for the Pi?

  20. Derk J

    Jun 18 Pre-Release Testers, Xojo Pro

    @David G This serial port is really wearing me out! My first Pi was working for a while, but eventually stopped, and it took a while to figure out that it was not reacting to baud rate changes, it was stuck 57k baud, and I needed 9600. A new Pi worked right away, but within a few minutes stopped. The Pi itself seems to be working fine, but I can no longer get the serial port to send anything, despite it opening fine. The scale I am trying to communicate with is continuously sending readings toggling between 0 and 12V, connected to the Pi via a FDTI serial-USB converter. Could this voltage be a problem for the Pi?

    Never use any higher voltage than 5V over USB.
    RS232 = 12v but USB/Serial is NOT.

  21. David G

    Jun 22 Pre-Release Testers, Xojo Pro Phoenix, AZ

    It wasn't a voltage problem, the USB-serial converter is taking care of the voltages. I was setting baud with serial1.baud=9600 instead of serial1.baud=8. What threw me off was that the first setting worked under Windows, and only very occasionally under Linux, so I thought the problem was with the Pi.

or Sign Up to reply!