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:
  1. Extract it
    • tar xf tty0tty-1.2.tgz
  2. Build the kernel module from the provided source
    1. cd tty0tty-1.2/module
    • make
  3. Copy the new kernel module into the kernel modules directory
    • sudo cp tty0tty.ko /lib/modules/$(uname -r)/kernel/drivers/misc/
  4. Load the module
  5. sudo depmod
  6. sudo modprobe tty0tty
    • You should see new serial ports in /dev/ (ls /dev/tnt*)
  7. Give appropriate permissions to the new serial ports
  8. 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 ports

Part 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



  sudo socat -d -d pty,link=/dev/tnt0,raw,echo=0 pty,link=/dev/tnt1,raw,echo=0   

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

Popular posts from this blog

IOT Greenhouse project part 1

How to fix FirebaseApp with name [DEFAULT] doesn't exist issue with flutter

Let's Learn NodeMCU(EPS8266MOD) very popular IoT component