Forward serial data between virtual serial ports in Node.js using socat
I'm sharing this with you because I have faced some difficulties when I try to get data from virtual serial ports to my backend in the project. It's easy
Part 1. Installation:
Download the tty0tty package from one of these sources:
- http://sourceforge.net/projects/tty0tty/files/
- clone the repo https://github.com/freemed/tty0tty
git clone https://github.com/freemed/tty0tty
- Extract it
tar xf tty0tty-1.2.tgz
- Build the kernel module from the provided source
- cd tty0tty-1.2/module
make
- Copy the new kernel module into the kernel modules directory
sudo cp tty0tty.ko /lib/modules/$(uname -r)/kernel/drivers/misc/
- Load the module
- sudo depmod
- sudo modprobe tty0tty
- You should see new serial ports in /dev/ (
ls /dev/tnt*
) - Give appropriate permissions to the new serial ports
- sudo chmod 666 /dev/tnt*
You can now access the serial ports as
/dev/tnt0
(1,2,3,4 etc) Note that the consecutive ports are interconnected. For example, /dev/tnt0 and /dev/tnt1 are connected as if using a direct cable.
Persisting across boot:
edit the file /etc/modules (Debian) or /etc/modules.conf
nano /etc/modules
and add the following line:
tty0tty
Note that this method will not make the module persist over kernel updates so if you ever update your kernel, make sure you build tty0tty again repeat the process.
Ok now you can use your new virtual serial portsPart 2 - Forwarding ports for to Node.js
To forward data from /dev/tnt0 to /dev/tnt1 you have to type follw command in the terminal
sudo socat -d -d pty,link=/dev/tnt0,raw,echo=0 pty,link=/dev/tnt1,raw,echo=0
Now you can see some terminal output with success port forwarding
Part 3 - Automate this process each time machine booting
If you are running a project in your machine or Raspberry PI you have to run this code each time when you forwarding ports. This can be fixed easily using crontab.
First you have to create a bash script to do that
Create a bash file called port_forward.sh and copy below to it
Then give permission to the bash using
chmod +x port_forward.sh
Now bash has been created. You can manually run it to forward the ports but let's make this automate each time machine boots up.
crontab is the best way to automate your scripts inside the Linux environment
You can simply setup a crontab by using this command
sudo crontab -e
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
# old_way @reboot /usr/bin/python /root/Desktop/Logger/log/Main.py
#@reboot /home/your_username/port_forward.sh
Please change your username here crontab and save it by Ctrl + O and reboot the pc.
Now Cron job will start and port forwarding will happen each time your Pc booting
Comments
Post a Comment
Comment