NI Linux Real-Time Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

Setting Serial Port Configuration on Startup

Solved!
Go to solution

Hello All!

I just posted this question as a comment on this tutorial page, but I feel like that may get overlooked, so I am replicating it here.  Sorry for the double post!

I am running into a conundrum and I was wondering if anyone had some insight.  For background, my app requires remote access to the serial ports on my cRIO from the host, so I have configured ser2net (successfully) to forward them over TCP/IP.  Using the tutorial above as a guide, I was able to get ser2net to start on boot, which is fantastic. 

The only issue I am having is trying to configure the serial port settings at boot.  This is a slightly different use case from the above, since I don't want to start a daemon, but simply run a few commands.  I use stty to set my serial configurations, and have incorporated it into the startup script as shown below.  (important piece in bold)

#!/bin/bash

NAME="Ser2Net Daemon"

DAEMON=/usr/sbin/ser2net

ARGS=" -n"

USER=admin

PIDFILE=/var/run/ser2net.pid

do_start() {

    /sbin/start-stop-daemon --start --pidfile $PIDFILE \

        --make-pidfile --background \

        --chuid $USER --exec $DAEMON -- $ARGS

}

do_stop() {

/sbin/start-stop-daemon --stop --pidfile $PIDFILE --verbose

}

case "$1" in

  start)

echo "Starting $NAME"

/bin/stty -F /dev/ttyNIRIOslot2port0 115200 ixon

/bin/stty -F /dev/ttyNIRIOslot2port1 115200 ixon

do_start

;;

  stop)

echo "Stopping $NAME"

do_stop

;;

  restart)

echo "Restarting $NAME"

do_stop

do_start

;;

  *)

echo "Usage: $0 {start|stop|restart}"

exit 1

;;

esac

exit 0

Now, if I test this after installing the script (by entering "/etc/init.d/ser2net.startup start" in the shell), this works fine.  However, if I reboot, the commands seem to do nothing, and the port config stays default.  To me, this means the syntax, etc. must be right, but somehow doing it at startup is causing issues.

Does anyone have any ideas why these commands would fail during startup but not during normal operation?  Is there a way to debug what is happening on startup?

Thanks in advance!

Drew

Drew T.
Camber Ridge, LLC.
Message 1 of 7
(8,043 Views)

If I'm understanding your post correctly, the script might not be failing -- it might not be running at all. You may just need to add a missing symlink. Putting a script in init.d doesn't tell the system when to run it. To do that, you need a link to it in /etc/rc<level>.d/. The link name includes a prefix S (for start -- or K for kill, run on shutdown) and a sequence number. An example for your case might be /etc/rc5.d/S50ser2net.startup -> /etc/init.d/ser2net.startup. The update-rc.d tool can do all this for you, see http://manpages.ubuntu.com/manpages/hardy/man8/update-rc.d.8.html

0 Kudos
Message 2 of 7
(7,242 Views)

Scot,

Thanks for the quick reply.  I realize I wasn't very clear about this part, but I know the script runs because ser2net comes up as a daemon.  I can communicate with the COM ports remotely and see the process using ps | grep ser2net.

Any other thoughts?

Thanks!

Drew

Drew T.
Camber Ridge, LLC.
0 Kudos
Message 3 of 7
(7,242 Views)

For clarity, I ran update-rc.d, and see K20ser2net.startup in /etc/init.d/rc1.d.

Drew T.
Camber Ridge, LLC.
0 Kudos
Message 4 of 7
(7,242 Views)
Solution
Accepted by Drew_T

One reason could be that the script runs before the hardware is up.

The symlinks created with update-rc.d that start with K are called to stop a daemon (e.g. at shutdown). Can you list where the 'S*ser2net' symlinks are? Or list the update-rc.d command you are using?

Really this sort of hardware configuration belongs in a udev rule instead. You can create a udev rule that matches /dev/ttyNIRIO* in /etc/udev/rules.d. Make the rule run on the "add" action and invoke the required stty command from there. This will guarantee that the stty configuration is applied after the hardware is up.

0 Kudos
Message 5 of 7
(7,242 Views)

Excellent, this is exactly the feedback I was looking for!  Being a Linux newcomer, I don't know the "right" ways to do things yet.

My symlinks are below:

admin@cRIO-9035-A:~# ls /etc/rc*/*ser2net*

/etc/rc0.d/K20ser2net.startup  /etc/rc1.d/K20ser2net.startup  /etc/rc2.d/S20ser2net.startup /etc/rc3.d/S20ser2net.startup  /etc/rc4.d/S20ser2net.startup  /etc/rc5.d/S20ser2net.startup /etc/rc6.d/K20ser2net.startup

I am just using the standard update-rc.d command below:

/usr/sbin/update-rc.d -f ser2net.startup defaults

I will look into the udev rule, as this seems more like the correct way to go.

Thanks for the help!!

Drew

Drew T.
Camber Ridge, LLC.
0 Kudos
Message 6 of 7
(7,242 Views)

Ding, ding, ding! We have a winner!

I added the file below to /etc/udev/rules.d and the ports now configure on boot.  Thanks for the awesome help Gratian!  This community rocks!

KERNEL=="ttyNIRIOslot[0-9]*port[0-9]*",        ACTION=="add", \

        RUN+="/bin/stty -F /dev/$name 115200 ixon"

Drew T.
Camber Ridge, LLC.
0 Kudos
Message 7 of 7
(7,242 Views)