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