Communication over serial port using Bash - bash

I'm trying to get a check-scanner to transmit image files to my Mac (the company only provides Windows drivers). I have the technical manual that specifies all the commands that can be sent to the device, and I'm trying the most basic commands first (blink the LED, print serial number, etc.).
I have been able to successfully communicate with the device using "Serial Tools", but I'm trying to use bash for more control of the input/output and to eventually write an automated script.
After reading a few other posts and trying several things, so far I've tried this:
Opening the port with stty command and various flags, for example:
stty -f /dev/tty.usbserial-A5002TeW speed 9600 cs8 cread clocal
stty -f /dev/tty.usbserial-A5002TeW raw speed 9600 -cstopb -parity -echo
Trying to send commands over echo:
echo $'LE 100\cM\cJ' > /dev/tty.usbserial-A5002TeW
--Note that the serial device is connected through a USB adaptor, but responds (at least in Serial Tools) as a regular serial device after I installed the right drivers for it.
--I'm trying to use the \cM\cJ characters because as the device was intended for use by Windows boxes I figured I should use their control codes, I've tried multiple permutations of the code.
The Problem:
No matter what happens, the device never responds (no blinky LED lights, no output, etc) and my bash shell hangs until I stop it (^C). I know I can probably use a Python library, and I might wind up doing that, but I'd like to at least see some basic functionality with the bash shell.

Another option would be to try to get it working under a VM (VirtualBox, Fusion, Parallels, etc.) by installing Windows in the VM on your Mac.
Most cumbersome solution, because you'd have to reboot all the time, dual boot your Mac into windows.
Finally, I have an app on my iPhone from my banking institution that simply uses the camera in the phone to photograph the front and back of a check, and they'll accept it that way. This might be an option.

Related

Simulate serial port device using stdin and stdout

Currently, I have a desktop app that I used to read and write data to a proprietary hardware device via a serial port interface. The app starts by listing the serial COM devices connected to the computer, and once a device is selected, one can interact with the app to communicate with the corresponding device. I no longer have such a device at hand, but I would still like to use the app nonetheless.
I have already developed a command-line utility that, given a binary message in stdin, emits the corresponding binary reply (the same reply that the proprietary hardware device would send via serial port) to stdout. Let's call this utility a simulator.
How do I set up a virtual serial port such that the app can detect it, and that whenever the app sends a message using the serial port protocol, such message is forwarded to the simulator, and the simulator's reply is returned back to the desktop app?
I'm on Mac OS 12 with an M1 CPU. I'm also open to solutions on Windows 10 (with less priority).
I have looked at previous questions on StackOverflow that might be similar to this one, but they were either incomplete or slightly different, with no obvious way to infer the solution for my actual problem.
Serial ports are unlike other channels. That's why separate system calls exist for them.
Normally, serial ports are created by physical devices (an old-fashioned serial port or a newer USB-based one) and the associated driver. Since writing a driver is quite difficult, a pragmatic and possibly unexpected approach would be to use hardware, specifically two USB-to-serial adapters. That way, a serial port is created by the drivers of the USB-to-serial adapter.
The two USB-to-serial adapters are wired to each other (RX to TX and vice versa). The will appear as two serial ports on macOS (/dev/cu.usb...).
The legacy application then connects to one of the serial ports. And your command line utility (acting as a device emulation) connects to the other serial port. All data send by the command line utility will go to the legacy application, and vice versa.
The remaining issue is how to connect your command line utility to the serial port. If you are lucky, you can use the screen command. But more likely, you will need to modify it to read and write from the serial port (instead of stdin and stdout).

WSL and serial port

I am using Win10 and Linux Ubuntu on WSL 2.0.
For testing purposes of some programs, I would like to use the serial port of my PC in "loopback" with Linux running through WSL.
Basically I would like a process on Linux/WSL to be able to send/receive data from a Windows process or vice versa, through serial port, but without any hardware hack.
Of course I have already tried to set the Windows process serial as "COM1" (as indicated by the Windows resource manager) and the Linux port on WSL as "/dev/ttyS1", but apparently it doesn't work.
Is there any way to do this?
Status update
According to other answers, it seems that the addition of product functions has made it possible to use USB devices with WSL2.
Especially in v5.10.93.2, it seems that drivers for two types of USB serial interface chips are built in.
linux-msft-wsl-5.10.93.2
Enable CH341 and CP210X USB Serial drivers
linux-msft-wsl-5.10.60.1
Enable USB over IP support
Enable USB kernel configuration options for interacting with an Arduino over USB
The following is outdated information.
WSL 2.0 does not support serial ports.
Exceptions for using WSL 1 rather than WSL 2
WSL 2 does not include support for accessing serial ports. Learn more in the FAQs or in WSL GitHub repo issue on serial support.
The following options are possible.
Revert to WSL 1.0 and use
Use third-party products
For example, there seems to be something like this.
Windows USB Server
VirtualHere USB Server
Although it is a Japanese article, there is such a trial article.
WSL2でUSBデバイスを認識させる
Also, if you want to communicate between serial ports even if WSL2 cannot recognize USB serial, this method is also available.
Connecting to serial port (com port) over network
And as you can see from the above explanation, if you want to communicate between the processes of each OS, you can simply use a TCP/IP socket instead of the above mechanism.
Run this in PowerShell or CMD to see if you use Version 1:
C:\>wsl -l -v
NAME STATE VERSION
* Ubuntu Running 1
If yes, here's some tested working code to read COM14 from linux command prompt:
#!perl -w
use strict;
$|=1; # autoflush
use Device::SerialPort;
my $port = Device::SerialPort->new("/dev/ttyS14");
$port->baudrate(115200); # Configure below to match your device
$port->databits(8);
$port->parity("none");
$port->stopbits(0);
$port->debug(1);
$port->read_char_time(0); # don't wait for each character
$port->read_const_time(1); # 0.001 second per unfulfilled "read" call
while (1) {
my ($count_in, $str_in) = $port->read(255); # Supposedly must be 255 always
if($count_in) {
print $str_in;
}
}
If you are running WSL2, you can backup, convert or copy your distribution to WSL1 via wsl --export, wsl --import, and wsl --set-version. See this question among others for details.

Play audio file stored in SD Card memory on Zedboard

Is it possible to have a named pipe on my PS side of the Zedboard; that leads to a FIFO in the the PL side (using DMA,AXI,I2S etc) that I then revert to the audio out port and play songs from my PS side and listen from the audio out port on the PL side?
If yes then what steps are to be followed on the PS Side?
I'm guessing at mapping of user space into kernel space.
Yes, it turns out that ANALOG DEVICES has just the stuff you need.
There is a different kernel that Analog Device's maintains, which
includes both ALSA drivers
for the audio chip (ADAU1761) and the HDMI output (ADV7511).
https://github.com/analogdevicesinc/linux
there are a few zynq branches in there. Normally Xilinx pulls drivers
from there for their kernels,
but anyone can do the same.
The build instructions (if that's the sort of thing you want to do) is
at:
http://wiki.analog.com/resources/eval/user-guides/ad-fmcomms2-ebz/software/linux/zynq_2014r2
Or, alternatively you could just download the ready-made image for your particular board from this dropbox link:
https://www.dropbox.com/sh/yfbpj63pcenqatr/AAAt0s3xFXs47I7q5pNopheHa?dl=0
After you download the file; uncompress it with this command:
unxz -d sdimage-8G-zedboard.direct.xz
Find out the name of your SD Card with this command:
dmesg|tail
And then write the resulting image to your 8GB SD Card with this command:
sudo dd if=sdimage-8G-zedboard.direct of=/dev/sdX
where sdX is your particular SDCard which you noted from command dmesg|tail
This command will erase all the pre-existing data on the sd card so make sure you have a backup if that data is important to you.
WARNING: Please be VERY careful while using the dd command. Writing the image to the wrong /dev/sdX location could possibly lead to corruption of OS and/or the hardware also and is extremely risky.
After you burn the image; you're good to go! A full-blown graphical linux environment will turn up ( You need to connect an HDMI display; and use USB OTG port to use the mouse and keyboard)
NOTE: You can also choose between what path you want your sound to play;
whether from the headphone jack or through the HDMI cable.

How to send modem AT Commands to a board from from OSX

Does anyone out there who has developed on a dev board attached to a Mac OSX machine's USB port know how to send AT tty modem commands to the board? Arduino folks? Anyone?
TL;DR
I purchased a development STEVAL-SPBT2ATV2 "USB Dongle for the Bluetooth class 2 SPBT2532C2.AT module", which, according to the docs and spec has "downloaded FW, enabling the user to create a Bluetooth link with simple AT commands."
Which would be great, except all the documentation is only for old MS Windows, and doesn't give any hints on how to program this device from OSX or Linux.
Do I need to install a driver of some sort? Everything I've tried is like talking to a brick wall: I send commands, but nothing comes back from the board.
Things I've tried:
/dev/tty.Bluetooth-Modem already existed. Didn't seem to do anything. I think that's the built-in bluetooth device.
/dev/tty.AmpedUp-AMP-SPP and /dev/cu.AmpedUp-AMP-SPP showed up when I went to "Network -> set up bluetooth device" - which is good (correct device name) but strange, why under network?
Then under Bluetooth I can add a "serial port used to connect to this computer" and get the choice of Modem or RS-232. I guessed Modem.
I really hope I don't have to mess with the /etc/tty files
Nothing. the device never seems to react, or send any data back. I've tried several ways to send data to the device. Silence.
echo "AT+AB GPIOConfig 2 O^M" > /dev/cu.AmpedUp-AMP-SPP # ctrl-v ctrl-m for the ^M
screen /dev/tty.AmpedUp-AMP-SPP
cat /dev/tty.AmpedUp-AMP-SPP
minicom # via brew
chat /dev/cu.AmpedUp-AMP-SPP
From the docs:
Each dongle has the following factory default:
UART: 115200 baud, no parity,1 stop bit,8 data bits
Local name: “Amp'ed UP!’
Class of device: Misc Device
Profile: SPP (serial port profile)
Service name: “AMP-SPP”
Deep sleep: disabled
Page and inquiry scan: 1.28s interval, 11 ms duration
Security: disabled
Bonding PIN: “1234”
Bonding allowed: always enabled
I figured out where I was wayyy off-base with some help from a friend.
I thought that I was trying to establish a OSX->Terminal->Serial (Over USB)->Chip communication channel to program the chip.
When in reality, I was trying to establish a OSX->Terminal->Serial (over Bluetooth->Bluetooth) -> Chip channel to talk to the chip using the existing firmware, which includes enough defaults to have 4 GPIO/LED outputs. The USB only comes in later if I want to change the firmware.
Which means plugging the board into my laptop's USB port isn't necessary and only complicates the issue - better is plugging it into a spare USB charger next to my laptop.
... Which let me narrow down the problem to 2 things:
OSX isn't holding an open bluetooth communication channel to the BT device, so of course I won't be able to talk to it, even if the serial port is set up right
Even if I could talk to it, Apple iOS is so locked down that I won't be able to connect to it from an iPhone/iPad. :(

Configuring 2 XBee Modules on OSX for wireless connection

I am trying find out how and with which program for OSX (10.5.8) I can configure serial ports? I am trying to establish a wireless connection between two Xbee´s (RF modules) and cannot figure out how to use ZTerm nor screen under Terminal. The setup I am using is: an Arduino+Xbeeshield+Xbee with external power supply, and an xbee on the xbee explorer connected to the Computer via USB.
I am trying to gather information on this through various forums, but most of them cover the configuration issue for PC using X-CTU (which I tried with CrossOver but it doesn´t recognize ny of my ports). According to one source, using screen under Terminal should show me all my serial ports, particularly /dev/tty.KeySerial1 - but it doesn´t show, even though I´ve plugged in both my arduino+xbee shield and the xbee on the explorer.
/dev/tty.KeySerial1 is incorrect.
First step is to get the FTDI USB driver installed if it has not yet been installed. The fastest way to determine if it is installed or not is to connect the XBee Explorer board. Then go look in /dev for a device named tty.usbserial-XXXXXXXX (Xs will be a unique hex ID). If you see multiple devices like this, then you probably have the Arduino plugged in too and you will need to disconnect it to determine what the device name is for the XBee Explorer board.
Once you know the device name, all you need to do is the command "screen /dev/tty.usbserial-XXXXXXXXX 9600". That should do it for you.
You can configure the tty device itself using stty. Be sure to redirect input from the terminal you want to configure, as stty operates on it's input. For instance, to set a serial port to 9600, no parity, 8 data bits, and 1 stop bit, aka "9600N81" in Windows parlance, try:
stty 9600 cs8 -cstopb -parenb < /dev/tty.usbserial-xxxxxxxx
Programmatically, you do this by opening the serial port and using the termios(4) ioctls on the device. See the termios(4) man page for more assistance.

Resources