nyni funguje i v shellu s aktivnim set -u
[mirrors/Programs.git] / bash / serial_loopback_test.sh
1 #!/bin/sh
2 #
3 #Serial line tester v0.3
4 #(c) Tomas Mudrunka 2022-2024
5 #
6 #Tested in: busybox ash, bash
7
8 function serial_loopback_test() {
9
10 port_tx=
11 port_rx=
12 trylimit=0
13 seconds=0
14 baudrate=115200
15 workaround_rs485=false
16
17 while getopts ":t:r:b:l:s:w" OPT; do
18 #echo "$OPT ==> $OPTARG";
19 case $OPT in
20 t) port_tx=$OPTARG;;
21 r) port_rx=$OPTARG;;
22 b) baudrate=$OPTARG;;
23 l) trylimit=$OPTARG;;
24 s) seconds=$OPTARG;;
25 w) workaround_rs485=true;;
26
27 '?') echo "Usage: $0 -t /dev/ttyTX [-r /dev/ttyRX] [-b 9600] [-l 10] [-s 10] [-w]" >&2; return 250;;
28 ':') echo "Missing option argument for -$OPTARG" >&2; return 251;;
29 * ) echo "Unimplemented option: -$OPT" >&2; return 252;;
30 esac
31 done
32
33 [ -z "$port_tx" ] && port_tx="$port_rx"
34 [ -z "$port_rx" ] && port_rx="$port_tx"
35 [ -z "$port_rx" ] && { echo Port not specified; return 253; }
36
37
38 testdata="${RANDOM}d82b2ae45432e7c80699852ab557b279c42180a379711aae85487bda0cc58602f65c0ab5af0d510d6ac1606c52f887f3332052f67c45212dbbf2730${RANDOM}"
39
40 echo "Running $baudrate baud loopback test on $port_tx -> $port_rx"
41 echo "Limiting to $trylimit iterations and $seconds seconds (0 = no limit)"
42
43 stty -F "$port_tx" raw -echo "$baudrate"
44 stty -F "$port_rx" raw -echo "$baudrate"
45
46 exec 5>"$port_tx"
47 exec 6<"$port_rx"
48
49 $workaround_rs485 && {
50 echo "Deasserting RTS on buggy RS485 drivers by writing to RX port"
51 echo>"$port_rx"
52 read -rst 1 -u 6
53 }
54
55 errcnt=0
56 trycnt=0
57 datestart="$(date +%s)"
58 dateend="$(($datestart+$seconds))"
59 while true; do
60 echo "$testdata" >&5
61 unset line
62 read -rst 1 -u 6 line
63 #echo "LINE: $line"
64 [ "$line" != "$testdata" ] && errcnt=$((errcnt+1)) &&
65 echo -e "$(date +'%D %T')\t$errcnt\tError receiving data $port_tx -> $port_rx"
66 trycnt=$((trycnt+1))
67 #echo "try $trycnt err $errcnt lim $trylimit"
68 [ $trylimit -gt 0 -a $trycnt -gt $trylimit ] && break
69 [ $seconds -gt 0 ] && [ $(date +%s) -gt $dateend ] && break
70 done
71
72 exec 5>&-
73 exec 6<&-
74
75 echo TRANSMITTED: $trycnt, ERRORS: $errcnt, SECONDS: $(($(date +%s)-$datestart))
76 [ "$errcnt" = "0" -a "$trycnt" -gt 0 ] && return 0
77 return 255
78
79 }
80
81 serial_loopback_test "$@"
This page took 0.286485 seconds and 4 git commands to generate.