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