Install serial port

From xPL
Jump to: navigation, search

Many devices are controlled over an RS-232 serial port.

Before all, make sure you have installed an xPL hub.

CPAN serial port module

The xPL serial port device bases on the Perl CPAN Device::SerialPort module.

Open a terminal and login as root.

Update CPAN:

su
perl -MCPAN -e shell
  > install LWP
  > reload LWP
  > install CPAN
  > reload CPAN
  > install YAML
  > q

Check if the serial port driver is already installed:

perl -MFile::Find=find -MFile::Spec::Functions -Tlw -e 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC' 2> /dev/null | grep -i serial

Install the serial port driver:

perl -MCPAN -e "install 'Device::SerialPort'"

Alternatively, on Ubuntu:

apt-get install libdevice-serialport-perl

Install xPL serial port device

Download the serial port xPL device script.

Move the script to xPL scripts directory:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
mv xpl-serial_port.pl $SCRIPTS_BASE_DIR
chown control:users $SCRIPTS_BASE_DIR/*.pl
chmod 775 $SCRIPTS_BASE_DIR/*.pl

Test xPL serial port device

If not already done, add user control to the dialout group:

su
cat /etc/group | grep control
usermod -aG dialout control

On some devices, the serial port is only owned by root. Check it:

ls -l /dev/tty*
cat /etc/udev/rules.d/*.rules

You might want to add a line in a rule file such as:

KERNEL=="ttymxc*",  MODE="0660", GROUP="dialout"

Test it on a BeagleBone:

su control
SCRIPTS_BASE_DIR=/home/control/Documents/Controls
ll /dev/ttyO*
$SCRIPTS_BASE_DIR/xpl-serial_port.pl -h
$SCRIPTS_BASE_DIR/xpl-serial_port.pl -v -n test /dev/ttyO1 baudrate 9600 databits 8 parity none stopbits 1 handshake none

Open a 2nd terminal window (on any machine with xPL installed) and monitor the xPL protocol:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-monitor.pl -vf

The monitor should echo the serial port incoming messages.

Open another terminal and send a message to the serial port:

su control
SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-send.pl -v -t cmnd -d dspc-serPort.test -c serPort.basic command='hello world'

Launch the xPL serial port device at startup

The xPL serial port device is to be launched after the xPL hub.

If the serial port equipment needs an initialisation command, the xPL device controlling the equipment trough the xPL serial port needs to be launched after the xPL serial port.

The following example shows the installation of an xPL serial port device connected to a Tapko EIB/KNX to RS232 converter.

Ubuntu

On Ubuntu, the xPL serial port device starts-up once xpl-hub_started has been emitted. Once launched, it emits xpl-eibSerialPort_started for the next xPL device to control the serial port equipment.

Edit /etc/init/xpl-eibSerialPort.conf:

################################################################################
# xPL serial port for Tapko EIB/KNX to RS232 bridge
#
description "xPL EIB bridge serial port"
version     "1.0"
author      "Francois Corthay"

#-------------------------------------------------------------------------------
# Configuration variables
#
env SCRIPTS_DIR='/home/control/Documents/Controls'
env SCRIPT_NAME='xpl-serial_port.pl'
env PARAMETERS='-n eib -e \\x0A\\x0D /dev/ttyO1 baudrate 9600 databits 8 parity none stopbits 1 handshake none'

#-------------------------------------------------------------------------------
# Start and stop conditions
#
start on xpl-hub_started
stop on shutdown
respawn
setuid control
setgid dialout

#-------------------------------------------------------------------------------
# Startup signalling
#
post-start script
  sudo initctl emit xpl-eibSerialPort_started
end script

#-------------------------------------------------------------------------------
# Start daemon
#
exec $SCRIPTS_DIR/$SCRIPT_NAME $PARAMETERS

Start the device and test it:

su root
service xpl-eibSerialPort start
service xpl-eibSerialPort status
initctl list | grep xpl

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-send.pl -v -t cmnd -d dspc-serPort.eib -c serPort.basic command='gci'
$SCRIPTS_BASE_DIR/xpl-send.pl -v -t cmnd -d dspc-serPort.eib -c serPort.basic command='dts \$15'
$SCRIPTS_BASE_DIR/xpl-send.pl -v -t cmnd -d dspc-serPort.eib -c serPort.basic command='tds (1/1/1) 1'

Debian with SystemD

On Debian with systemd (such as Raspbian), the scripts can be defined as services.

Edit /lib/systemd/system/xpl-screenSerialPort.service:

[Unit]
Description=serial port for Panasonic screen
After=network.target

[Service]
Type=simple
ExecStart=/home/control/Documents/Controls/xpl-serial_port.pl -n loungeScreen -b \\x02 -e \\x03 -w 2 /dev/ttyUSB0 baudrate 9600 databits 8 parity none stopbits 1 handshake none
Restart=always

[Install]
WantedBy=multi-user.target

Activate the service:

su
systemctl enable xpl-screenSerialPort.service

Reboot and check:

ps ax | grep -v grep | grep -i xpl
ps aux | grep -i xpl | grep -v grep | sed 's/.*\/Controls\///' | sort
service xpl-screenSerialPort status

Mac OS

On Mac OS, the xPL serial port device starts-up later than the hub.

Edit /Library/LaunchDaemons/xpl-eibSerialPort.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>xPL EIB bridge serial port</string>
    <key>OnDemand</key>
    <false/>
    <key>UserName</key>
    <string>control</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/control/Documents/Controls/xpl-serial_port.pl</string>
      <string>-n</string>
      <string>eib</string>
      <string>-w</string>
      <string>25</string>
      <string>-e</string>
      <string>\x0A\x0D</string>
      <string>/dev/tty.PL2303-0020131D</string>
      <string>baudrate</string>
      <string>9600</string>
      <string>databits</string>
      <string>8</string>
      <string>parity</string>
      <string>none</string>
      <string>stopbits</string>
      <string>1</string>
      <string>handshake</string>
      <string>none</string>
    </array>
  </dict>
</plist>

Start the device and test it:

su root
launchctl load /Library/LaunchDaemons/xpl-eibSerialPort.plist

SCRIPTS_BASE_DIR=/Users/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-send.pl -v -t cmnd -d dspc-serPort.eib -c serPort.basic command='gci'
$SCRIPTS_BASE_DIR/xpl-send.pl -v -t cmnd -d dspc-serPort.eib -c serPort.basic command='dts \$15'
$SCRIPTS_BASE_DIR/xpl-send.pl -v -t cmnd -d dspc-serPort.eib -c serPort.basic command='tds (1/1/1) 1'