Print TTY Stream to Terminal - linux

I have a usb device. The driver for said device (a generic USB to UART driver for that chipset) sets up a ttyUSB0 stream for interfacing with the device. I am trying to understand how the data from this device is formatted. Is it possible to display that stream, live, on my terminal (something like piping to stdout)?
This device is running on a limited embedded system. The system uses a Linux 2.6 kernel and busybox shell, so I can't simply install applications like a serial terminal, and many cli programs which are default on most distros (such as screen) aren't available on my system.

Use cat.
cat /dev/ttyUSB0
If it's not setup correctly, do it with stty. For example to set baud rate:
stty -F /dev/ttyUSB0 115200

Related

how to know if a USB device is plugged into the embedded Linux system

I'm new to the embedded Linux world. I'm using Nvidia Jetson Nano as my embedded SOC.
Now I need my application to do some specific thing, for example, show an icon, as long as a USB disk is plugged into the embedded board. How can I know if such a USB disk is plugged in?
I need to know the approach for knowing the as long as a USB disk is plugged in
You can check all USB devices that are plugged in with the commands :
$ lsusb
Or with the command :
$ ls /dev/ttyUSB
You could then check if one of the listed devices (/dev/ttyUSB0 for example) is in the list and do your action.
What programming language are you using for your application ? There must be libraries that can help you check easily if a USB is connected/disconnected

How can I detect a USB port being used for charging in Linux?

I'm using a USB port on the Raspberry Pi 3 Model B to charge an accessory via a USB magnetic charging cable. I want to detect when the cable is plugged in and removed from the Pi.
I've been plugging in and removing this cable and looking for any traces of this being detected on Linux. So far, I've tried:
monitoring udev events by udevadm monitor.
tailing the system logs in /var/log etc.
running an inotify watch on the /dev directory for new devices
looking around in /sys/bus/usb/devices for any clues
Is it possible to detect my simple charging cable being inserted/removed? By intuition I would think no (since the cable is purely for charging, and doesn't have drivers), however I observe that the RPI flashes its builtin LED red when I insert the cable.
Without significant effort (hardware or hacks), No.
The red LED flash you observe is the power monitor for the Pi indicating a sudden voltage drop as current begins to flow to your accessory. This LED indicates voltage (electrical energy) has briefly dropped below stable levels, and system instability may result.
As the connection is purely charging (no USB data exchanged with the Pi) the operating system has no idea the device is connected. You could build an external current-measuring circuit to detect the peripheral drawing power, but such a question is better off in the electronics stack exchange.
I think that you cand do it with the lsusb tools:
lsusb -v
Or Filtering with:
lsusb -v |grep 'Bus\|MaxPower'
With this command I can see how much power each USB is usign and which device is connected.

How can I prevent linux from initializing a USB HID device

I have a USB HID device that can work in two different modes. The selection of modes is based on the sequence of USB enumeration/initialization packets sent to it.
I am using a Raspberry Pi 3 running Raspbian, however I also see the same issue if I compile my code for my desktop Ubuntu distro.
The issue I have is that linux is recognizing the USB device as a HID device and then sending the sequence of commands that it deems necessary to start the device, and this works correctly and starts the device in "Mode 1".
However I need to start the device in "Mode 2" and to do this I need to send a slightly different set of enumeration/initialization commands.
I am new to linux but very experienced with LibUSB and LibUSBDotNet under windows and can get the behavior I desire under windows.
Windows has similar behaviour to linux in that it will enumerate, recognise the device as a USB HID device and then initialise it as it deems fit resulting in the device going into "Mode 1". To prevent windows doing this I can create a LibUSB filter driver for the device, which then replaces the default driver, so windows will now do the initial enumeration, realise that the VID and PID of the device are managed by the LibUSB filter driver (rather than the windows HID driver) and then stop enumeration/initialization - this allows my code to take over and complete the initialization into "Mode 2".
How can I stop Linux from fully enumerating/initializing this device (as I do with windows). Perhaps I need to do something with udev rules or something, but I would have no idea what as I am new to linux.
Any help greatly appreciated
you have right, you have to play with the udev rules.
First of all you have to identify your device. Find the idProduct and the idVendor of your device. You can use:
lsusb
Then in the rules.d folder (/etc/udev/rules.d) create a new file with the name:
10-my-usb.rules
In this file add this line
SUBSYSTEM=="usb",ATTRS{idVendor}=="XXXX", ATTRS{idProduct}=="XXXX", MODE="666", GROUP+="plugdev"
Replace the XXXX with the value you get before
Then restart your udev rules:
sudo udevadm trigger
Then unplug and replug normally you can use it

Analyzing Serial data over USB on Linux

I have a USB to RS232 adapter which I'm currently using to communicate with a legacy device. I want to analyze the data going to and from the legacy device from my Ubuntu machine over USB. The adapter is getting detected automatically without installing any driver as shown in lsusb output below:
Bus 006 Device 002: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
I tried using the USB packet capture in Wireshark, but it shows a bunch of URB_CONTROL, URB_BULK packets which is not showing anything meaningful. So if there are any better utilities available which can capture the USB2Serial data for a Linux system, please let me know. TIA!
After trying various methods, I finally got what I needed using strace. The following command helped me capture all the termios settings for the serial port and also all the IOCTL calls after doing some greps on the file handle of the serial port:
sudo strace -p <pid> -t -v -o dump.txt
This will dump all the system calls that your application makes on Linux into the file dump.txt.

How do I intercept messages from a USB device on Linux?

I have a popular drawing tablet that I connect to my PC with USB. Once connected, the tablet detects hand movements and manipulates the pointer accordingly. Somewhere, the tablet is transmitting this data to my computer.
My goal is to intercept these transmissions and manipulate the mouse after I process the data. The buzzwords I have found are: device drivers and HID, but I haven't been able to piece together much more than that.
Assuming this is possible, I have a few questions:
How is this done if the data format is known?
How is this done if the data format is unknown/proprietary?
My computer is running Ubuntu (but answers related to any form of a Linux OS are greatly appreciated!).
Note: This question is similar but intended for Windows.
Actually you missed a buzzword "USB sniffing". ;-)
Here are a few tutorials on USB sniffing on Linux, to get you started.
Official Wireshark wiki for USB monitoring
biot.com/blog/usb-sniffing-on-linux (InternetArchive)
tjworld.net/wiki/Linux/Ubuntu/USBmonitoring
Essentially you use the usbmon Linux kernel module to capture the USB-packets and Wireshark as a front-end to display and analyse the captured USB stream.
To add another useful resource: Kernel manual for usbmon
You can use the following commands on Debian Linux to view debug log for usbmon in text format using usbmon Kernel's built in usb monitoring:
$sudo -i to use root
#modprobe usbmon
#ls /sys/kernel/debug/usb/usbmon to view bus sockets
#cat /sys/kernel/debug/usb/devices to view devices at each bus socket
#cat /sys/kernel/debug/usb/usbmon/<bus socket> to view or you can route stdout to a file using >

Resources