4

I want to read the single status lines (txd, dtr, ...) of a PL2303 USB2RS232 converter with statserial. I already attached my PL2303 to the ttyUSB0 port. But to read the data, I need the command statserial, wich just accepts ttyS* (if I try to use it with ttyUSB*, "permission denied" apperars). How can you change the ttyUSB* to a ttyS* "adress"??

lsusb:

gernot@gernot-Lenovo-G505s:~$ lsusb
Bus 002 Device 002: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 004: ID 174f:147b Syntek 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 004: ID 0cf3:3004 Atheros Communications, Inc. 
Bus 003 Device 002: ID 062a:4101 Creative Labs 
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 005 Device 002: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

dmesg: 

...

[   53.186869] ISO 9660 Extensions: Microsoft Joliet Level 3
[   54.986337] ISOFS: changing to secondary root
[   76.485704] usb 5-1: new full-speed USB device number 2 using xhci_hcd
[   76.522777] usb 5-1: New USB device found, idVendor=067b, idProduct=2303
[   76.522786] usb 5-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[   76.522791] usb 5-1: Product: USB-Serial Controller
[   76.522795] usb 5-1: Manufacturer: Prolific Technology Inc.
[   76.557641] usbcore: registered new interface driver usbserial
[   76.557658] usbcore: registered new interface driver usbserial_generic
[   76.557668] usbserial: USB Serial support registered for generic
[   76.559702] usbcore: registered new interface driver pl2303
[   76.559719] usbserial: USB Serial support registered for pl2303
[   76.560192] pl2303 5-1:1.0: pl2303 converter detected
[   76.588044] usb 5-1: pl2303 converter now attached to ttyUSB0

...

Thanks for the help!

Update: It seems that I was not a member of the dialout group. I added my user to that group using sudo usermod -a -G dialout <username>

After that I had permission to access the USB and Serial ports. Now terminal-programs like moserial work. But I still can't access the status lines using statserial. Instead I get that message:

gernot@gernot-Lenovo-G505s:~$ statserial ttyUSB0
statserial: can't open device `ttyUSB0': No such file or directory

Update 2: I managed to make statserial work (the sense of this thread will collapse I guess): First you have to reset the port and then establish a hard link to a ttyS*-port with a higher number than the number of standard ports are:

sudo rm /dev/ttyS40
sudo ln /dev/ttyUSB0 /dev/ttyS40 

But how can I copy the data displayed in terminal into any object i can read with selfmade code? And why is the statserial-tool reacting so slowly to changed staus lines?

4

Your first issue (device permissions) was fixed by adding your user to the correct group.

Your second issue (device path) seems to be that you are not using the full device path. From man statserial:

The optional device-name parameter is the full name of the device file for the serial port in question. If not specified, the default is taken from the environment variable MODEM if set, otherwise /dev/cua1.

So you need to do statserial /dev/ttyUSB0

(I checked the source to verify this is correct, it literally does fd = open(device,...) where device is your argv[x])

For generic information about programming the serial port see Serial Programming HOWTO. You can look at the source of statserial with apt-get source statserial (it is a very simple program compiled from a single file statserial.c which is 195 lines long).

The update is slow because statserial only polls the serial port every second, the main loop is:

  for (;;) {

    /* get modem status info */ 
    status = ioctl(fd, TIOCMGET, &arg);
    if (status != 0) {
      perror("statserial: TIOCMGET failed");
      exit(1);
    }

...

    printw("Device: %s\n\n", device);
    printw("Signal  Pin  Pin  Direction  Status  Full\n");
    printw("Name    (25) (9)  (computer)         Name\n");
    printw("-----   ---  ---  ---------  ------  -----\n");
    printw("FG       1    -      -           -   Frame Ground\n");
    printw("TxD      2    3      out         -   Transmit Data\n");
    printw("RxD      3    2      in          -   Receive  Data\n");
    printw("RTS      4    7      out         %1d   Request To Send\n", !!(arg & TIOCM_RTS));
    printw("CTS      5    8      in          %1d   Clear To Send\n", !!(arg & TIOCM_CTS));
    printw("DSR      6    6      in          %1d   Data Set Ready\n", !!(arg & TIOCM_DSR));
    printw("GND      7    5      -           -   Signal Ground\n");
    printw("DCD      8    1      in          %1d   Data Carrier Detect\n", !!(arg & TIOCM_CAR));
    printw("DTR     20    4      out         %1d   Data Terminal Ready\n", !!(arg & TIOCM_DTR));
    printw("RI      22    9      in          %1d   Ring Indicator\n", !!(arg & TIOCM_RNG));
    refresh();

    /* break out if -n option was used */
    if (n_option)
      exit(0);

    /* delay 1 second between loops */
    sleep(1);
  }

If you are doing any serious work with serial ports, I would highly recommend that you get a real serial port card (PCI card with RS232 controller on it). The USB adaptors are not reliable enough, especially if you are linking to old hardware, you will find that they do not reproduce the signal timings accurately, which will cause problems with some devices. Another common problem is that the USB serial ports don't implement (or badly implement) hardware RTS/CTS handshaking, and thus end up being incompatible with old serial port devices. Having said that, the PL2303 is one of the better chips, and should work with most devices.

  • 1
    Thank for the answer, that helped a lot! I am new to Ubuntu, so it's more of a Try'n'Fail-procedure. I study physics and need that tools for a little project for triggering two output lines depending on two input lines with different time delay. Usually I'd use a PIC-controller for that job, but my programmer crashed one day ago. The serial port is great, if you just need some lines. So the question is, if there is any possibility to change the statserial-tool to make it run faster. How can you rewrite the statserial app? – user285079 May 25 '14 at 19:05
  • Remove sleep(1) (or replace it with usleep or nanosleep) – bain May 25 '14 at 19:07
  • @user285079, Could you accept bain's answer, if it worked for you. Please make a look on askubuntu.com/help/someone-answers – user.dz Jun 8 '16 at 3:25
1

I don't know about statserial (and I know this is an old question), but if you want to have a custom device name, you can drop a file in /etc/udev/rules.d/ like this one (mine is named 98-devicename.rules):

SUBSYSTEM=="tty" ATTRS{idVendor}=="067b" ATTRS{idProduct}=="2303" SYMLINK+="devicename%n" MODE:="0666"

This will match the USB ID/vendor and allow all users to access without being in dialout group (MODE). It will also create a symlink /dev/devicename0 -> /dev/ttyUSB0 assuming it is the first of both. If you had a different device with a different USB chipset (e.g. a Cypress one) already plugged in, it might be the first of this chipset, so /dev/devicename0 -> /dev/ttyUSB1. By having udev handle the symlinks, you don't need to worry about it being plugged into a different port, etc - devicename0 should always be the first device that uses that chipset.

To find more things that you can match, see udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0).

0

As for how to connect custom code to serial ports, since I commonly use Python, I would recommend checking out the pySerial library https://wiki.python.org/moin/PySerial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.