subIT
Posts: 7
Joined: Fri Oct 11, 2019 5:56 pm
Location: Germany

USB-Serial reading of PI

Fri Oct 11, 2019 6:20 pm

Hi, I am trying to read a USB-Serial on the Raspi from a wifi usb adapter.
When I read the lsusb I see the correct USB but when I am using dmesg I can´t find which is the correct Serial to use.
Cause I want to read it with a python script like

Code: Select all

serial.Serial('/dev/ttyAMA0', 19200, timeout=5)
so that whenever I get a value from it I can handle it (like a DB entry of the return).

To read the USB-Serial I did some things:
1) sudo apt-get install python-serial python3-serial
2) sudo raspi-config --> Serial --> login Shell (disabled)

This is my current Python file to read the data and handle it

Code: Select all

#!/usr/bin/python

import serial, string
import sys
import Adafruit_DHT
import time
import requests
import json
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode

output = " "

ser = serial.Serial(
    port='/dev/ttyAMA0',
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)
counter = 0

print("----")
while 1:
    output = ser.readline()
    print(output)

    if output == "" or len(output) < 5:
        print("Fail-Read")
    else:
        try:
            conn = mysql.connector.connect(host='', database='', user='', password='')
            cursor = conn.cursor()

            sql1 = "INSERT INTO tb_artikel (code, test) VALUES (%s, %s)"
            val1 = (output, 1)
            result = cursor.execute(sql1, val1)

            conn.commit()
            print("success!")

        except mysql.connector.Error as error:
            conn.rollback()
            print("Insertingerror".format(error))

        finally:
            if (conn.is_connected()):
                cursor.close()
                conn.close()
                print("Connection has been closed!")

    output = " "

This is what I get at the dmesg: Image

The USB-Device is on Bus 001 Device 007: ID #####

dgordon42
Posts: 764
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: USB-Serial reading of PI

Sat Oct 12, 2019 3:47 pm

If you are trying to read (or write) serial data to a USB attached device, you need to know it's filename in the '/dev' directory.
It won't be '/dev/ttyAMA0' as this is one of the Pi's UARTs.

Have a look in the '/dev' directory. A plugged in USB Serial device would normally appear as '/dev/ttyUSB0'.
If it's there, replace all instances of "/dev/ttyAMA0" with "/dev/ttyUSB0" in your Python code and try again.

Hope this helps,
Dave.

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: USB-Serial reading of PI

Sat Oct 12, 2019 4:46 pm

subIT wrote:
Fri Oct 11, 2019 6:20 pm
Hi, I am trying to read a USB-Serial on the Raspi from a wifi usb adapter.

You've lost me already.

What role does your WiFi adapter have to play in reading serial data?

subIT
Posts: 7
Joined: Fri Oct 11, 2019 5:56 pm
Location: Germany

Re: USB-Serial reading of PI

Mon Oct 14, 2019 9:34 am

dgordon42 wrote:
Sat Oct 12, 2019 3:47 pm
If you are trying to read (or write) serial data to a USB attached device, you need to know it's filename in the '/dev' directory.
It won't be '/dev/ttyAMA0' as this is one of the Pi's UARTs.

Have a look in the '/dev' directory. A plugged in USB Serial device would normally appear as '/dev/ttyUSB0'.
If it's there, replace all instances of "/dev/ttyAMA0" with "/dev/ttyUSB0" in your Python code and try again.

Hope this helps,
Dave.
Hi, that is exactly what I normally expected but with dmesg I get input,hidraw1: USB HID v1.10 Dev ice and in my Folder /dev I have no tty with a USB and I tryed different USB-Devices but nothing showed up in the folder

Folder: Image

subIT
Posts: 7
Joined: Fri Oct 11, 2019 5:56 pm
Location: Germany

Re: USB-Serial reading of PI

Mon Oct 14, 2019 9:35 am

B.Goode wrote:
Sat Oct 12, 2019 4:46 pm
subIT wrote:
Fri Oct 11, 2019 6:20 pm
Hi, I am trying to read a USB-Serial on the Raspi from a wifi usb adapter.

You've lost me already.

What role does your WiFi adapter have to play in reading serial data?
It´s a device which runs on batteries and sending data via WiFi to a USB-Serial receiver which is in the RasperryPi USB-Hub

User avatar
scruss
Posts: 2617
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB-Serial reading of PI

Mon Oct 14, 2019 3:01 pm

Your device, if it's recognized, wasn't listed in the output you gave. Here's how I find my serial ports. You'll need to open a terminal.

· Look at the output from lsusb:

Code: Select all

Bus 001 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. LAN9500 Ethernet 10/100 Adapter / SMSC9512/9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
It's fairly clear that the serial port is the device Bus 001 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port. Yours may be different: typical manufacturers include FTDI, QinHeng, Prolific and Silicon Laboratories/SiLabs, but the important bit is the USB ID (067b:2303 for me, likely different for you) that uniquely identifies a device and sets up its driver.

· Now look at the kernel messages using dmesg | less. You can scroll through this with space and the cursor keys. You can also search using /string and then n to find the next match. Here I'm looking for the first part of my USB ID, 067b:

Code: Select all

[    3.301420] usb 1-1.3: New USB device found, idVendor=067b, idProduct=2303
(you could also use grep for this: dmesg | grep 067b )
The useful part of that output is the usb 1-1.3: it flags output in the log that's coming from this device. We can find all of the output using dmesg | grep 'usb 1-1.3':

Code: Select all

[    3.159137] usb 1-1.3: new full-speed USB device number 4 using dwc_otg
[    3.301420] usb 1-1.3: New USB device found, idVendor=067b, idProduct=2303
[    3.310441] usb 1-1.3: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[   12.743646] usb 1-1.3: pl2303 converter now attached to ttyUSB0
The last line tells you the device name: ttyUSB0, or the full device node you need to use from your program /dev/ttyUSB0. It's likely to be either a ttyUSB name or a ttyACM name for a USB serial port.

Hope this gets you going.

(if you end up using multiple serial adapters, it's sometimes useful to know about the alternative device paths that live in /dev/serial. For me, these devices are the same port as /dev/ttyUSB0:

Code: Select all

/dev/serial/by-path/platform-20980000.usb-usb-0:1.3:1.0-port0
/dev/serial/by-id/usb-067b_2303-if00-port0
These aliases can help choose the actual hardware port you're connecting to, as the ttyUSB[0123…] numbers don't always get assigned to the same port.)
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: USB-Serial reading of PI

Mon Oct 14, 2019 4:07 pm

subIT wrote:
Mon Oct 14, 2019 9:35 am
B.Goode wrote:
Sat Oct 12, 2019 4:46 pm
subIT wrote:
Fri Oct 11, 2019 6:20 pm
Hi, I am trying to read a USB-Serial on the Raspi from a wifi usb adapter.

You've lost me already.

What role does your WiFi adapter have to play in reading serial data?
It´s a device which runs on batteries and sending data via WiFi to a USB-Serial receiver which is in the RasperryPi USB-Hub



There is some usb troubleshooting advice from Raspberry Pi towards the end of this documentation - https://www.raspberrypi.org/documentati ... /README.md


It seems possible that the device is not being detected by the Raspbian Operating System as a serial (UART) device.

Do you have a web link to the specification of this 'dongle' you are trying to use?

Are you connecting it via an external usb hub? Does it make any difference if you connect it directly to the RPi board itself?

What Model of RPi board are you using, and what Operating System is it running?

subIT
Posts: 7
Joined: Fri Oct 11, 2019 5:56 pm
Location: Germany

Re: USB-Serial reading of PI

Mon Oct 14, 2019 6:11 pm

scruss wrote:
Mon Oct 14, 2019 3:01 pm
Your device, if it's recognized, wasn't listed in the output you gave. Here's how I find my serial ports. You'll need to open a terminal.

· Look at the output from lsusb:

Code: Select all

Bus 001 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. LAN9500 Ethernet 10/100 Adapter / SMSC9512/9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
It's fairly clear that the serial port is the device Bus 001 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port. Yours may be different: typical manufacturers include FTDI, QinHeng, Prolific and Silicon Laboratories/SiLabs, but the important bit is the USB ID (067b:2303 for me, likely different for you) that uniquely identifies a device and sets up its driver.

· Now look at the kernel messages using dmesg | less. You can scroll through this with space and the cursor keys. You can also search using /string and then n to find the next match. Here I'm looking for the first part of my USB ID, 067b:

Code: Select all

[    3.301420] usb 1-1.3: New USB device found, idVendor=067b, idProduct=2303
(you could also use grep for this: dmesg | grep 067b )
The useful part of that output is the usb 1-1.3: it flags output in the log that's coming from this device. We can find all of the output using dmesg | grep 'usb 1-1.3':

Code: Select all

[    3.159137] usb 1-1.3: new full-speed USB device number 4 using dwc_otg
[    3.301420] usb 1-1.3: New USB device found, idVendor=067b, idProduct=2303
[    3.310441] usb 1-1.3: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[   12.743646] usb 1-1.3: pl2303 converter now attached to ttyUSB0
The last line tells you the device name: ttyUSB0, or the full device node you need to use from your program /dev/ttyUSB0. It's likely to be either a ttyUSB name or a ttyACM name for a USB serial port.

Hope this gets you going.

(if you end up using multiple serial adapters, it's sometimes useful to know about the alternative device paths that live in /dev/serial. For me, these devices are the same port as /dev/ttyUSB0:

Code: Select all

/dev/serial/by-path/platform-20980000.usb-usb-0:1.3:1.0-port0
/dev/serial/by-id/usb-067b_2303-if00-port0
These aliases can help choose the actual hardware port you're connecting to, as the ttyUSB[0123…] numbers don't always get assigned to the same port.)
When I grep the USB by vedor I don´t get an attached /dev name by the console output --> so what should I do?

Image

When I try a general data USB I get the same data as above + the Productname, it´s maufactor and it´s serial number but NOT a tty...
Last edited by subIT on Mon Oct 14, 2019 6:32 pm, edited 1 time in total.

subIT
Posts: 7
Joined: Fri Oct 11, 2019 5:56 pm
Location: Germany

Re: USB-Serial reading of PI

Mon Oct 14, 2019 6:21 pm

B.Goode wrote:
Mon Oct 14, 2019 4:07 pm
subIT wrote:
Mon Oct 14, 2019 9:35 am
B.Goode wrote:
Sat Oct 12, 2019 4:46 pm



You've lost me already.

What role does your WiFi adapter have to play in reading serial data?
It´s a device which runs on batteries and sending data via WiFi to a USB-Serial receiver which is in the RasperryPi USB-Hub



There is some usb troubleshooting advice from Raspberry Pi towards the end of this documentation - https://www.raspberrypi.org/documentati ... /README.md


It seems possible that the device is not being detected by the Raspbian Operating System as a serial (UART) device.

Do you have a web link to the specification of this 'dongle' you are trying to use?

Are you connecting it via an external usb hub? Does it make any difference if you connect it directly to the RPi board itself?

What Model of RPi board are you using, and what Operating System is it running?

The troubleshooting link was´t very effective cause none of the listed issues exist for me
--> only the driver problem of keyboards.. but I would be surprized if a very standard keyboard would make a problem ++ I wouldn´know where I could get a seperated driver for it

Model: Pi 3 Modell B+ 1GBram
Dongle: at the moment it´s a simple keyboard for testing while I am waiting for a barcode Scanner(which simulates a keyboard)
Connection: actual Keybord directly via cable to the PI .... later Scanner sending it to USB-receiver --> directly in the PI

The Programm later has to interact with the scanned data by a BarcodeScanner and a website (hosted on the PI) to add to a list / register a new article with it´s name (inserted on the websitePopUp)

jj_0
Posts: 108
Joined: Wed Jul 11, 2012 7:07 am

Re: USB-Serial reading of PI

Mon Oct 14, 2019 7:05 pm

subIT wrote:
Mon Oct 14, 2019 6:21 pm
B.Goode wrote:
Mon Oct 14, 2019 4:07 pm
subIT wrote:
Mon Oct 14, 2019 9:35 am


It´s a device which runs on batteries and sending data via WiFi to a USB-Serial receiver which is in the RasperryPi USB-Hub
There is some usb troubleshooting advice from Raspberry Pi towards the end of this documentation - https://www.raspberrypi.org/documentati ... /README.md


It seems possible that the device is not being detected by the Raspbian Operating System as a serial (UART) device.

Do you have a web link to the specification of this 'dongle' you are trying to use?

Are you connecting it via an external usb hub? Does it make any difference if you connect it directly to the RPi board itself?

What Model of RPi board are you using, and what Operating System is it running?

The troubleshooting link was´t very effective cause none of the listed issues exist for me
--> only the driver problem of keyboards.. but I would be surprized if a very standard keyboard would make a problem ++ I wouldn´know where I could get a seperated driver for it

Model: Pi 3 Modell B+ 1GBram
Dongle: at the moment it´s a simple keyboard for testing while I am waiting for a barcode Scanner(which simulates a keyboard)
Connection: actual Keybord directly via cable to the PI .... later Scanner sending it to USB-receiver --> directly in the PI

The Programm later has to interact with the scanned data by a BarcodeScanner and a website (hosted on the PI) to add to a list / register a new article with it´s name (inserted on the websitePopUp)
If you are currently using a keyboard to test (device 046d:c31c is a K120 keyboard) and not connecting the USB serial device then there will be no /dev/ttyUSBx device showing up. So you can only test by reading keyboard input.

subIT
Posts: 7
Joined: Fri Oct 11, 2019 5:56 pm
Location: Germany

Re: USB-Serial reading of PI

Mon Oct 14, 2019 9:00 pm

jj_0 wrote:
Mon Oct 14, 2019 7:05 pm
subIT wrote:
Mon Oct 14, 2019 6:21 pm
B.Goode wrote:
Mon Oct 14, 2019 4:07 pm

There is some usb troubleshooting advice from Raspberry Pi towards the end of this documentation - https://www.raspberrypi.org/documentati ... /README.md


It seems possible that the device is not being detected by the Raspbian Operating System as a serial (UART) device.

Do you have a web link to the specification of this 'dongle' you are trying to use?

Are you connecting it via an external usb hub? Does it make any difference if you connect it directly to the RPi board itself?

What Model of RPi board are you using, and what Operating System is it running?

The troubleshooting link was´t very effective cause none of the listed issues exist for me
--> only the driver problem of keyboards.. but I would be surprized if a very standard keyboard would make a problem ++ I wouldn´know where I could get a seperated driver for it

Model: Pi 3 Modell B+ 1GBram
Dongle: at the moment it´s a simple keyboard for testing while I am waiting for a barcode Scanner(which simulates a keyboard)
Connection: actual Keybord directly via cable to the PI .... later Scanner sending it to USB-receiver --> directly in the PI

The Programm later has to interact with the scanned data by a BarcodeScanner and a website (hosted on the PI) to add to a list / register a new article with it´s name (inserted on the websitePopUp)
If you are currently using a keyboard to test (device 046d:c31c is a K120 keyboard) and not connecting the USB serial device then there will be no /dev/ttyUSBx device showing up. So you can only test by reading keyboard input.
Ok but when I use for example a Arduino Mega --> which can be receive and send data - I don´t get any ttyUSB.. like in the example before

jj_0
Posts: 108
Joined: Wed Jul 11, 2012 7:07 am

Re: USB-Serial reading of PI

Mon Oct 14, 2019 9:08 pm

subIT wrote:
Mon Oct 14, 2019 9:00 pm
jj_0 wrote:
Mon Oct 14, 2019 7:05 pm
subIT wrote:
Mon Oct 14, 2019 6:21 pm



The troubleshooting link was´t very effective cause none of the listed issues exist for me
--> only the driver problem of keyboards.. but I would be surprized if a very standard keyboard would make a problem ++ I wouldn´know where I could get a seperated driver for it

Model: Pi 3 Modell B+ 1GBram
Dongle: at the moment it´s a simple keyboard for testing while I am waiting for a barcode Scanner(which simulates a keyboard)
Connection: actual Keybord directly via cable to the PI .... later Scanner sending it to USB-receiver --> directly in the PI

The Programm later has to interact with the scanned data by a BarcodeScanner and a website (hosted on the PI) to add to a list / register a new article with it´s name (inserted on the websitePopUp)
If you are currently using a keyboard to test (device 046d:c31c is a K120 keyboard) and not connecting the USB serial device then there will be no /dev/ttyUSBx device showing up. So you can only test by reading keyboard input.
Ok but when I use for example a Arduino Mega --> which can be receive and send data - I don´t get any ttyUSB.. like in the example before
Well, it's the same principle, it depends on how you are connecting said Arduino Mega. Directly to the GPIO uart pins? Then there's no ttyUSBx. Via a USB2Serial adapter? Then there should be a ttyUSBx and 'lsusb' should show that the USB2Serial adapter is attached/recognised.

subIT
Posts: 7
Joined: Fri Oct 11, 2019 5:56 pm
Location: Germany

Re: USB-Serial reading of PI

Mon Oct 14, 2019 9:31 pm

jj_0 wrote:
Mon Oct 14, 2019 9:08 pm
subIT wrote:
Mon Oct 14, 2019 9:00 pm
jj_0 wrote:
Mon Oct 14, 2019 7:05 pm


If you are currently using a keyboard to test (device 046d:c31c is a K120 keyboard) and not connecting the USB serial device then there will be no /dev/ttyUSBx device showing up. So you can only test by reading keyboard input.
Ok but when I use for example a Arduino Mega --> which can be receive and send data - I don´t get any ttyUSB.. like in the example before
Well, it's the same principle, it depends on how you are connecting said Arduino Mega. Directly to the GPIO uart pins? Then there's no ttyUSBx. Via a USB2Serial adapter? Then there should be a ttyUSBx and 'lsusb' should show that the USB2Serial adapter is attached/recognised.
Well I connected it with a USB directly and the lsusb showed me 2341:0042 Arduino SA Mega 2560 R3 (CDC ACM) but not ttyUSB exists

BUT what I recognized is that the Arduino creates instead of a ttyUSB an ttyACM0

User avatar
scruss
Posts: 2617
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB-Serial reading of PI

Tue Oct 15, 2019 12:06 am

subIT wrote:
Mon Oct 14, 2019 9:31 pm
BUT what I recognized is that the Arduino creates instead of a ttyUSB an ttyACM0
Which I said was a possibility in my post: ttyACM0

It would've been a lot easier if you'd said Arduino Mega in the first place: they all create ttyACM0 devices.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

Return to “Troubleshooting”