Sunday, April 25, 2021

Remote Windows PC Uart Access

Last August I posted using Python for Remote Serial Port Access. In fact, there is many other way to do the same, without the need to run a script. The widely used is opening pseudo-ttys via SSH connection, as mentioned https://github.com/npat-efault/picocom:

$ ssh -t user@termbox picocom -b 115200 /dev/ttyS0 

Picocom is a terminal emulator, similar like minicom, PuTTy and Teraterm, with very small footprint. One problem is that it is tty based, and Windows does not expose Serial Port/UART as TTY devices. Cygwin does not provide picocom package. Even we can compile picocom from source for Cygwin, it is worthless as it cannot open Windows native serial port device.

The solution is WSL. With WSL, it is possible to access native device from the Linux environment running as Windows Sub-System. Nowadays, model PC/Laptop does not have RS-232 port, mostly, external device with UART output will be connected to PC with a Uart-over-USB (FTDI chip). Unfortuenately, as of today, WSL2 still does not support USB device. As I mentioned here, may need to stick with WSL1 for this feature at this moment.

Two things need to be done on WSL: install picocom and setup SSH server.

Install picocom

Most Linux distribution provide picocom install package, such as Ubuntu/Debian, user can run sudo apt install picocom to have it installed in seconds. However there is one issue: may run into error as 'FATAL: failed to add port: Cannot get the device attributes: Inappropriate ioctl for device'. This is a known issue with custom baud-rate support, and as mentioned in the ticket the solution is either build the binary with CPPFLAGS=-DNO_CUSTOM_BAUD make or run picocom with env setting: NO_CUSTOM_BAUD=1. If taking the env setting way, need to make sure the setting is taking effect for SSH session. So better use the build option. 
To build it, download the picocom source from https://github.com/npat-efault/picocom, install gcc and make for WSL if you haven't done so:
sudo apt install gcc make
If runs into issue, likely you will need to do: sudo apt update --fix-missing
Then build the binary as mentioned above: CPPFLAGS=-DNO_CUSTOM_BAUD make
Once build is done, copy the executable to a executable path such as /usr/local/bin.

Setup SSH server

May refer this for how to setup SSH on WSL. Before try the SSH access, make sure on SSH server side, has port 22 opened from Windows Firewall.
Install and setup SSH server just like on a Linux setup: sudo apt install openssh-server
By default, it will have PasswordAuthentication off and PubkeyAuthentication on. And for pubkey, it may be using ECDSA but not RSA. So if running into SSH permission denied (publickey) issue, may need to play with sshd config setting: sudo nano /etc/ssh/sshd_config
May try ssh -vvv username@the_wsl_host to triage the error. The '-vvv' option should give out lot of useful information of the error. Once sshd_config is updated, run sudo service ssh --full-restart to restart the sshd (systemctl restart sshd for old version Linux).
 

Open UART remotely

ssh -t user@wsl_host picocom -b 115200 /dev/ttyS10

Above will try to open the COM10 on the wsl_host pc remotely. Note picocom is a terminal emulator, just like minicom, there is no GUI interface. To quit, press C-a, then C-x. Looking for help? Press C-a, then C-h.

0 Comments:

Post a Comment