How to read data from serial-to-usb device? - bash

I have a sensor device connected to my Ubuntu 20 machine using a serial to usb cable. I understand that to stream data from it I need to run the following commands:
Enter root with sudo -I.
Setup a virtual COM port with stty -F /dev/ttyACM0 9600 cs8 -cstopb -parenb.
Send a command to the device to initiate data streaming with echo -e "some_string" > /dev/ttyACM0.
Stream data with cat < /dev/ttyACM0.
However, I cannot be sure that my device is connected as /dev/ttyACM0. How can I set this up? Currently it has the path /dev/bus/usb/001/005, although the last number changes every time the device is plugged in.
In response to the comment below, here is the output I get when I disconnect the device and run dmesg | tail (I've edited some real values):
[89092.291151] usb 1-1: USB disconnect, device number 11
[89100.310893] usb 1-1: new full-speed USB device number 12 using xhci_hcd
[89100.467562] usb 1-1: New USB device found, idVendor=SOME_ID, idProduct=SOME_PRODUCT, bcdDevice=SOME_BCD_DEVICE
[89100.467565] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[89100.467567] usb 1-1: Product: SOME_PRODUCT
[89100.467569] usb 1-1: Manufacturer: SOME_MANUFACTURER
EDIT: I have just found that I can use sudo modprobe usbserial vendor=SOME_ID product=SOME_PRODUCT to make ttyUSB0 available. However, plugging this into the above commands and running screen /dev/ttyUSB0 only returns lots of ``` characters. Note that this stops when the device is unplugged.

I managed to get this working. The commands were:
sudo -i
rmmod ftdi_sio
rmmod usbserial
modprobe ftdi-sio
echo -n VENDOR_ID PRODUCT_ID | sudo tee /sys/bus/usb-serial/drivers/ftdi_sio/new_id
stty -F /dev/ttyUSB0 9600 cs8 -cstopb -parenb
echo -e "uud1\r" > /dev/ttyUSB0
screen /dev/ttyUSB0 9600

Related

redirect serial port output as bash command with Xbee and RPI

My setup is complicated and I think I have a clear way ahead, but please let me know if you see a better way to accomplish my end state of using a terminal window over Xbee. My use case is that RPI #1 has internet connectivity, but RPI #2 does not, and I want to fully control and access RPI #2 via RPI #1 over Xbee.
I have x2 Raspberry Pi 3B+ and am using x2 Xbee Pro S3B modules to communicate between the RPIs over Xbee USB Development Shields. The Xbees show on the RPIs as /dev/ttyUSB0. I want to use the Xbees as a transportation layer to the RPIs, much like 802.11/15 or plain old ethernet would be used in a headless situation with bash. The Xbees are running at 115200 baud rate, and are named and setup via the X-CTU tool. I have no illusions of high speed data, but want to "see" RPI #2 terminal on RPI #1, the same as when SSH is accomplished with traditional transport layers.
I am able to use the Xbees in Transparent Mode, and send plain text with Screen, Minicom, "echo "text here" > /dev/ttyUSB0", and "cat < /dev/ttyUSB0". Despite the ability to pass messages, I want to use these plain text messages as bash input. For example, when I pass the command ls via any of the three methods listed from RPI 1, I want to have bash exectue "ls" on RPI 2, not just see it listed on the screen for RPI 2.
I've found several tools for Xbee, but don't want to wire up the GPIO pins and go that method; I want to use the Xbees as simple transport, nothing more. How do I pass text from /dev/ttyUSB0 to bash as a command, and see the results? Short of a more direct route, I'm considering using crontabs and an executable file that is erased and re-written to accomplish this task, but feel that is a last, very ineffective, method.
Is there some tool I am missing that does this already? Can I "screen" over a serial port as command line and serial I/O simultanously?
I found pyserial, which could allow for a TCP binding to the /dev/ttyUSB0 port, but am not sure if that is the right way to go or not. As of now, my code is as simple as
RPI #1:
echo "ls" > /dev/ttyUSB0
RPI #2:
cat < /dev/ttyUSB0
I was able to send and recieve commands from command line of a local (although remoted) XBee host to a remote (secondary, off net) Xbee host. I found the answer when I started looking at how serial devices could open a login terminal, and arrived at the getty tool. Here are my setup instructions for Transparent Mode use, I am still trying to get python-xbee and other tools to work to allow for the same concept, but via API mode. Note that the below instructions are a 95% solution, but should get the common user to a solid way ahead. I am not the original author of the steps below in their format, but found each step and combined them through various other Q&A forums to arrive at a solution:
First, acquire Digi Xbee X-CTU software (does not install on ARM devices such as Raspberry or Odroid):
XCTU:
Install from the following Digi.com link, but navigate to the corrresponding software FTP link:
https://www.digi.com/support/productdetail?pid=3352&type=drivers
Linux 64 Bit: ftp://ftp1.digi.com/support/utilities/40002881_R.run
Linux 32 Bit: ftp://ftp1.digi.com/support/utilities/40002880_R.run
Windows: ftp://ftp1.digi.com/support/utilities/40003026_T.exe
Mac: ftp://ftp1.digi.com/support/utilities/40003026_T.exe
Install X-CTU Via:
sudo wget ftp://ftp1.digi.com/support/utilities/40002880_R.run
sudo mv 40002881_R.run xctu_64bit.run
sudo chmod +x xctu_64bit.run
sudo ./xctu_64bit.run
Find X-Bee Device:
make sure Xbee is not plugged into a hub, power will be too little, recognizable via the below error, YMMV:
dmesg | grep ttyUSB0
and returning error: [ 228.800021] ftdi_sio ttyUSB0: failed to get modem status: -32
lsusb -v -d 0403:6001
sudo nano /boot/cmdline.txt
change the console tty device from AMA to USB, then for the kgdboc, which allows the remote end to watch the boot process, add/make match as appropriate
console=ttyUSB0,115200 kgdboc=ttyUSB0,115200
sudo nano /etc/inittab
make sure to uncomment (remove #) if present, change tty from AMA to USB
T0:23:respawn:/sbin/agetty -L ttyUSB0 115200 vt100
On Ubuntu x86 system, use X-CTU via
sudo ./XCTU.desktop
update firmware to the latest version
currently 8075 for the Pro S3B, then set baud rate to 115200 on each device
other xbees on in the vicinity can be updated by using a local xbee via X-CTU, then setting the api mode to “api mode with escapes”. Note that Transparent Mode should be used unless you have an indepth knowlege to make API mode work. I started with Transparent Mode to demonstrate the below works, but have since moved to API mode to gain the enhanced send-recieve control capabilities (pending a working version as of this writing).
do the same steps for all the devices that will be used on the network; once the local device is complete, other remote devices can be updated if visible (close enough).
Close out X-CTU and add the current user to the dialout group via:
sudo usermod -a -G dialout root
reboot then:
Setup Minicom Via:
sudo aptitude install minicom
minicom -s
serial port setup
a, set to /dev/ttyUSB0, then hit enter
e, set baud rate to 115200, then hit enter
hit enter again to close the window and arrive at the configuration page of minicom
select to save as dfl, followed by enter
then move to exit, and hit return
test connection to a locally connected device via
three plus symbols without hitting return
if it replies “ok” in a few seconds or less, all is well
OR Screen:
screen /dev/ttyUSB0
again, if you see a login prompt, you are connected. Note that screen is probably the best choice for most users; it has the inherent quality of ease of use (when compared to Minicom), handles low bandwidth connections with ease, and stays alive despite a disconnect from remote host. Ctl+a and then k will disconnect.
Install Coreutils to add more options than Minicom (Screen is also advisable):
sudo aptitude install coreutils && screen
stty -F /dev/ttyUSB0 -a
this will check serial port capabilities and settings
Communicate with your devices:
Note you interact with your network on a local machine with an X-Bee plugged in, or on a remote device you SSH over the internet, as long as it has an X-Bee attached. Also, note that the below settings to rc.local weren't keeping my settings after a reboot; this is a work in progress. I was setting them manually until I got automation worked out.
Also, I added rc.local to the RPI manually, the how-to for that is out there somewhere:
sudo systemctl stop serial-getty#ttyAMA0.service
sudo systemctl disable serial-getty#ttyAMA0.service
sudo systemctl enable serial-getty#ttyUSB0.service
sudo nano /etc/rc.local
add the below before exit 0
The stty line is twice because it has been noted that the first instance changes the reported baud rate to 9600, the second to 115200. If you are doing this manually, do a “stop” then re-do the start command to receive the prompt. This could be automated; I will update this post with a process monitor.
stty -F /dev/ttyUSB0 speed 115200 cs8 -cstopb -parenb raw
stty -F /dev/ttyUSB0 speed 115200 cs8 -cstopb -parenb raw
sudo systemctl start serial-getty#ttyUSB0.service
Then, use Minicom, Screen, or "cat" and "watch" to view messages sent. When using Minicom you will receive a login prompt via the above directions. As previously stated, I am still trying to get this working smoothly for API mode, but at least I know I have connectivity and can do basic command & control via the command line remotely with Transparent Mode, including running command line programs and commands. Transparent Mode does not offer any enhanced RF propagation correction techniques, hence my desire to get API mode working; RSSI values and error correction would be nice.

How to remove only some network services on Mac?

I noticed that on my Network I have thousands of useless services and I want to remove them, but I want to remove only these starting with "SAMSUNG".
When I run:
% networksetup -listallnetworkservices
I get:
SAMSUNG Modem 1
...
SAMSUNG Modem 1392
Wi-Fi
LGE Android Phone 2
iPhone
Bluetooth PAN
Thunderbolt Bridge
As you can see I have more than on thousand of services starting with "SAMSUNG" and I want to remove only them.
When I trey to remove one of them:
% networksetup -removenetworkservice "SAMSUNG Modem 4"
I just can't because of this error:
You cannot remove SAMSUNG Modem 4 because there aren't any other network services on SAMSUNG Modem.
** Error: The parameters were not valid.
How can I remove this?
I had this problem recently and the solution is very simple
this file contains all network adapters /Library/Preferences/SystemConfiguration/preferences.plist
this command list all samsung modems: networksetup -listallnetworkservices | grep "SAMSUNG Modem"
this command remove one samsung modem (where X is a modem number, ex: "SAMSUNG Modem 155"): networksetup -deletepppoeservice "SAMSUNG Modem X"
To remove all Samsung Modems do it on terminal:
for service in $(/usr/sbin/networksetup -listallnetworkservices | grep "SAMSUNG Modem" ); do
/usr/sbin/networksetup -deletepppoeservice "${service}"
done
Solved by executing the following bash script:
/usr/sbin/networksetup -listallnetworkservices | grep "SAMSUNG Modem" | while read -r line
do
/usr/sbin/networksetup -deletepppoeservice "${line}"
printf 'Deleted %s\n' "${line}"
done
Decided not to touch /Library/Preferences/SystemConfiguration/preferences.plist, not sure if the connection should not be deleted elsewhere.
wish there was a more easy way...but nothing...
i stumbled upon your same issue...
Anyway...you can solve this issue cleaning this file
/Library/Preferences/SystemConfiguration/preferences.plist
And by cleaning i mean: "remove the networkservice you don't need"
UPDATE
I just emptied that file (ofc i did a backup before), opened network in setting and was totally empty (ofc). so i just added a new Position and named "Automatic" and now all works like a charm without the thousand of network bogus services
Have a good day
Antonio

Shell command for getting mac address in OS X

I have been handicapped by the GUI and always seem to ask of help when it comes to the command line.
On Mac OS X only I need a command line to get the mac address of the wifi currently in use.
Help!
ifconfig en1 gets the interface details for wifi, the mac is on a line starting with ether, and is the second word on that line so:
ifconfig en1 | awk '/ether/{print $2}'
I think the best and easiest way to get the information is using this command:
networksetup -listallhardwareports
It will return a nice list of devices like this:
Hardware Port: USB 10/100/1000 LAN
Device: en6
Ethernet Address: 00:e0:4c:...
Hardware Port: Wi-Fi
Device: en0
Ethernet Address: 80:e6:50:...
Hardware Port: Bluetooth PAN
Device: en3
Ethernet Address: 80:e6:50:...
Hardware Port: Thunderbolt 1
Device: en1
Ethernet Address: 72:00:05:...
Hardware Port: Thunderbolt 2
Device: en2
Ethernet Address: 72:00:05:...
Hardware Port: Thunderbolt Bridge
Device: bridge0
Ethernet Address: 72:00:05:...
VLAN Configurations
===================
networksetup -getmacaddress <interface>
Wifi mac address is normally can be found in en0. So you may try this command on Terminal
ifconfig en0 | awk '/ether/{print $2}'
ifconfig should do the trick, it'll display a bunch of info including your MAC address. Alternatively it'll be in your network settings under system preferences.
EDIT
On a computer with just a wireless connection, en0 will have your wifi settings. The tag labeled with ether will most likely be your MAC address.
If you have both a wired and wireless connection, it'll be under ether in the en1 tag
Source: http://m.iclarified.com/entry/index.php?enid=30929
This will easily give you the specific Mac Address for your Wifi Interface
networksetup -listallhardwareports | grep Wi-Fi -A 3 | awk '/Ethernet Address:/{print $3}'

Cant connect Android Wear Device via bluetooth

I have USB debugging on and I am showing that the Target is connected I however am unable to connect the host.
When I run the below command on my PC I get output indicating that the command is not complete.
adb forward tcp:4444 localabstract:/adb-hub; adb connect localhost:4444
Output:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Android Studio>adb forward
tcp: 4444 localabstract:/adb-hub;adb connect localhost:4444
Android Debug Bridge version 1.0.31
-a - directs adb to listen on all interfaces for a c
onnection
-d - directs command to the only connected USB devic
e
returns an error if more than one USB device is
present.
-e - directs command to the only running emulator.
returns an error if more than one emulator is r
unning.
-s <specific device> - directs command to the device or emulator with
the given
serial number or qualifier. Overrides ANDROID_S
ERIAL
environment variable.
-p <product name or path> - simple product name like 'sooner', or
a relative/absolute path to a product
out directory like 'out/target/product/sooner'.
If -p is not specified, the ANDROID_PRODUCT_OUT
environment variable is used, which must
be an absolute path.
-H - Name of adb server host (default: localhost)
-P - Port of adb server (default: 5037)
devices [-l] - list all connected devices
('-l' will also list device qualifiers)
connect <host>[:<port>] - connect to a device via TCP/IP
Port 5555 is used by default if no port number
is specified.
disconnect [<host>[:<port>]] - disconnect from a TCP/IP device.
Port 5555 is used by default if no port number
is specified.
Using this command with no additional arguments
will disconnect from all connected TCP/IP devic
es.
device commands:
adb push <local> <remote> - copy file/dir to device
adb pull <remote> [<local>] - copy file/dir from device
adb sync [ <directory> ] - copy host->device only if changed
(-l means list but don't copy)
(see 'adb help all')
adb shell - run remote shell interactively
adb shell <command> - run remote shell command
adb emu <command> - run emulator console command
adb logcat [ <filter-spec> ] - View device log
adb forward --list - list all forward socket connections.
the format is a list of lines with the followin
g format:
<serial> " " <local> " " <remote> "\n"
adb forward <local> <remote> - forward socket connections
forward specs are one of:
tcp:<port>
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
dev:<character device name>
jdwp:<process pid> (remote only)
adb forward --no-rebind <local> <remote>
- same as 'adb forward <local> <remote>' but fail
s
if <local> is already forwarded
adb forward --remove <local> - remove a specific forward socket connection
adb forward --remove-all - remove all forward socket connections
adb jdwp - list PIDs of processes hosting a JDWP transport
adb install [-l] [-r] [-s] [--algo <algorithm name> --key <hex-encoded key> --
iv <hex-encoded iv>] <file>
- push this package file to the device and instal
l it
('-l' means forward-lock the app)
('-r' means reinstall the app, keeping its data
)
('-s' means install on SD card instead of inter
nal storage)
('--algo', '--key', and '--iv' mean the file is
encrypted already)
adb uninstall [-k] <package> - remove this app package from the device
('-k' means keep the data and cache directories
)
adb bugreport - return all information from the device
that should be included in a bug report.
adb backup [-f <file>] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all]
[-system|-nosystem] [<packages...>]
- write an archive of the device's data to <file>
.
If no -f option is supplied then the data is wr
itten
to "backup.ab" in the current directory.
(-apk|-noapk enable/disable backup of the .apks
themselves
in the archive; the default is noapk.)
(-obb|-noobb enable/disable backup of any insta
lled apk expansion
(aka .obb) files associated with each applic
ation; the default
is noobb.)
(-shared|-noshared enable/disable backup of the
device's
shared storage / SD card contents; the defau
lt is noshared.)
(-all means to back up all installed applicatio
ns)
(-system|-nosystem toggles whether -all automat
ically includes
system applications; the default is to inclu
de system apps)
(<packages...> is the list of applications to b
e backed up. If
the -all or -shared flags are passed, then t
he package
list is optional. Applications explicitly g
iven on the
command line will be included even if -nosys
tem would
ordinarily cause them to be omitted.)
adb restore <file> - restore device contents from the <file> backup
archive
adb help - show this help message
adb version - show version num
scripting:
adb wait-for-device - block until device is online
adb start-server - ensure that there is a server running
adb kill-server - kill the server if it is running
adb get-state - prints: offline | bootloader | device
adb get-serialno - prints: <serial-number>
adb get-devpath - prints: <device-path>
adb status-window - continuously print device status for a specifie
d device
adb remount - remounts the /system partition on the device re
ad-write
adb reboot [bootloader|recovery] - reboots the device, optionally into the boo
tloader or recovery program
adb reboot-bootloader - reboots the device into the bootloader
adb root - restarts the adbd daemon with root permissions
adb usb - restarts the adbd daemon listening on USB
adb tcpip <port> - restarts the adbd daemon listening on TCP on th
e specified port
networking:
adb ppp <tty> [parameters] - Run PPP over USB.
Note: you should not automatically start a PPP connection.
<tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
[parameters] - Eg. defaultroute debug dump local notty usepeerdns
adb sync notes: adb sync [ <directory> ]
<localdir> can be interpreted in several ways:
- If <directory> is not specified, both /system and /data partitions will be u
pdated.
- If it is "system" or "data", only the corresponding partition
is updated.
environmental variables:
ADB_TRACE - Print debug information. A comma separated list
of the following values
1 or all, adb, sockets, packets, rwx, usb, sync
, sysdeps, transport, jdwp
ANDROID_SERIAL - The serial number to connect to. -s takes prior
ity over this if given.
ANDROID_LOG_TAGS - When used with the logcat option, only these de
bug tags are printed.
For your information, I finally solved the same problem by replacing port 4444 by 5601.
The 2 calls are:
adb forward tcp:5601 localabstract:/adb-hub
and
adb connect localhost:5601
(My Android Wear device is a LG G Watch and my Android tablet is an Asus Nexus 7)
This is easy to solve:
I had the same problem.
Just devide this into two calls.
First enter:
adb forward tcp:4444 localabstract:/adb-hub
hit Enter and after that:
adb connect localhost:4444
Enter and it should connect. If not try first to start the "adb start-server"
try Ip address instead of localhost when connecting
adb forward tcp:4444 localabstract:/adb-hub
adb connect 127.0.0.1:4444
I had this problem as well. After running the above commands in the Command Line, I solved it by activating the Developer Options on my Moto 360 (pressing the build number repeatedly in the settings like the phone). Enabling the ADB and Bluetooth Debugging on the watch and unpairing my watch with my phone.
After I did all these steps my computer finally sent the RSA permissions key for my Android Wear device through my phone.
The Complete solution for this issue..
1. Open Your-sdk-path in Explorer.
2. Open platform-tools.
3. Shift + Rightclick then select Open command window here.
4. adb forward tcp:4444 localabstract:/adb-hub
adb connect localhost:4444 (For Real Device).
5. adb -d forward tcp:5601 tcp:5601 (for EMulator).
Then after connect with emulator in Android wear companion app.
Before Doing step 4 please ensure that debugging over bluetooth is enabled in developer option.
Open Your-sdk-path in Explorer, and then "platform-tools", where it should be your adb.exe , in my case this is ( C:\Users\YourUser\AppData\Local\Android\sdk\platform-tools )
Shift + Rightclick then select Open command window here.
paste "adb forward tcp:4444 localabstract:/adb-hub"
And then paste "adb connect 127.0.0.1:4444" (do not put localhost it is giving problems).
you can check with "adb devices"
I also had issue adb debugging over bluethooth:
Debugging over Bluetooth was always Off for me. Apparently I had to choose a debugging wearable device here:
Android Wear -> Settings (gear icon) -> Debugging over Bluetooth -> Device to Debug
Then all other comments are helpful too.
Tried all of the above, nothing helped.
The one thing that helped me was to restart the phone...

usb detail via udev

I have written a udev rule that call a script to send email when a usb drive is attached/removed. so far its working fine. but i want to know if there is any way i can include detail of usb drive in my email? how can i get it using udev? like usb size, label, mounted on, time. following is my udev rule and send email script:
SUBSYSTEM=="usb", ACTION=="add", RUN+="/u/usb_added add %b"
usb_added script is:
sendEmail -f root#IP -t abc#live.com -s smtp.live.com:587 -m "USB Attached" -o username=xyz#live.com
I found the way by using udevadm monitor --environment. This gives complete details when a USB device is attached or removed.

Resources