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