-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserialport-server.sh
executable file
·190 lines (144 loc) · 3.37 KB
/
serialport-server.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
#
# Author: Andrey Shmakov
#
# https://github.com/akshmakov/serialport-server
#
# Simple serial port server using socat
#
PROGNAME=$(basename $0)
INVOKE_TS=$(date +"%s")
function error_exit
{
#----------------------------------------------------------------
# Function for exit due to fatal program error
# Accepts 1 argument:
# string containing descriptive error message
#----------------------------------------------------------------
echo "ERROR: ${PROGNAME} - ${1:-"Unknown Error"}" 1>&2
usage
exit 1
}
function usage
{
cat <<EOF
Usage: $PROGNAME [OPTIONS] device
options:
-p/--port=<PORT> : exposed TCP Port (default=2000)
-b/--tty-br=<BAUDRATE> : baud rate of underlying device (default=9600)
-l/--logfile=<FNAME> : save output to file
-h/--help : print this usage
-d/--daemon : daemonize (background)
-v/--verbose : more (debug)
device: local socket or device (e.g. /dev/ttyUSB0)
The following Environment Variables can be used in lieu of args
PORT - TCP Port
BAUDRATE - Baudrate
DEVICE - device
EOF
exit 0
}
## Global VARS
PORT=${PORT-2000}
BAUDRATE=${BAUDRATE-9600}
#VERBOSE
#DAEMON
#LOGFILE
#DEVICE
## Following Parses the -abc/--do-thing-xyz options
## Most options modify one of the service variables above
## A few set Application Flags
## Command Processing Happens After
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-p|--port)
PORT=$2
shift
shift
;;
-b|--tty-br)
BAUDRATE=$2
shift
shift
;;
-h|--help)
usage
error_exit "Should Not Get Here"
;;
-l|--logfile)
LOGFILE=$2
shift
shift
;;
-d|--daemon)
DAEMON=1
shift
;;
-v|--verbose)
VERBOSE=1
shift
;;
-*|--*)
# unknown option
error_exit "Unknown Option $1"
;;
*)
break
;;
esac
done
## All options have "-" or "--"
## First string after options is our command
## Command Arguments can be added later
if [[ -n $1 ]]; then
DEVICE=$1
elif [[ -z $DEVICE ]]; then
error_exit "You need to specify a device"
fi
echo "Device: $DEVICE"
### setup verbose mode
# Nifty Trick found on SO
# You can write your own verbose only
# log string by repacing your
# echo "text string here"
# with
# echo "text string here" >&3
# for stdout (verbose)
# and >&4 for stderr (verbose)
###
if [ ! -z $VERBOSE ]; then
exec 4>&2 3>&1
else
exec 4>/dev/null 3>/dev/null
fi
cat <<-EOF >&3
External Port: $PORT
Host Device: $DEVICE
BAUD: $BAUDRATE
Log File : ${LOGFILE-NONE}
Daemon : `if [ -z $DAEMON ]; then echo "no"; else echo "yes"; fi`
EOF
## Socat
#socat -d -d -v tcp4-listen:2000,reuseaddr,fork file:/dev/ttyUSB0,raw,nonblock,echo=0,waitlock=/var/run/tty
SOCAT_BIN=socat
SOCAT_OPTS="-v"
SOCAT_TCP_PRE=tcp4-listen
SOCAT_TCP_POST="reuseaddr,fork,ignoreeof"
SOCAT_FILE_PRE=file
SOCAT_FILE_POST="raw,nonblock,echo=0"
if [ -n $VERBOSE ]; then
SOCAT_OPTS="-d -d -v"
fi
SOCAT_INVOCATION="$SOCAT_BIN $SOCAT_OPTS \
$SOCAT_TCP_PRE:$PORT,$SOCAT_TCP_POST \
$SOCAT_FILE_PRE:$DEVICE,$SOCAT_FILE_POST"
echo $SOCAT_INVOCATION >&3
stty -F $DEVICE $BAUDRATE
if [ -z $LOGFILE]; then
$SOCAT_INVOCATION
else
$SOCAT_INVOCATION 2>&1 > $LOGFILE
fi
#socat -d -d -v PTY,link=/dev/ttytun,raw,echo=0 FILE:/dev/ttyUSB0,raw,nonblock,echo=0