Lepsi tester seriovky
[mirrors/Programs.git] / bash / serial_loopback_test.sh
CommitLineData
4eb6c240
TM
1#!/bin/sh
2#Serial line tester, should work in busybox ash
3#(c) Tomas Mudrunka 2022-2024
55d43182 4
4eb6c240
TM
5trylimit=0
6seconds=0
7baudrate=115200
8workaround_rs485=false
9
10while 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
24done
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; }
55d43182 29
55d43182
TM
30
31testdata="${RANDOM}d82b2ae45432e7c80699852ab557b279c42180a379711aae85487bda0cc58602f65c0ab5af0d510d6ac1606c52f887f3332052f67c45212dbbf2730${RANDOM}"
32
4eb6c240
TM
33echo "Running $baudrate baud loopback test on $port_tx -> $port_rx"
34echo "Limiting to $trylimit iterations and $seconds seconds (0 = no limit)"
55d43182 35
4eb6c240
TM
36stty -F "$port_tx" raw -echo "$baudrate"
37stty -F "$port_rx" raw -echo "$baudrate"
55d43182
TM
38
39exec 5>"$port_tx"
40exec 6<"$port_rx"
41
4eb6c240
TM
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
55d43182 48errcnt=0
4eb6c240
TM
49trycnt=0
50datestart="$(date +%s)"
51dateend="$(($datestart+$seconds))"
55d43182
TM
52while true; do
53 echo "$testdata" >&5
54 unset line
55 read -rst 1 -u 6 line
56 #echo "LINE: $line"
57 [ "$line" != "$testdata" ] && {
55d43182 58 echo -e "$(date +'%D %T')\t$errcnt\tError receiving data $port_tx -> $port_rx"
4eb6c240
TM
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
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.172229 seconds and 4 git commands to generate.