Commit 65d1e5b405d73fa3cfeef1069c20046f4dbc8a8b
1 parent
df29ceea
Exists in
dev
New reousrces in Tcl for tests.
Showing
1 changed file
with
124 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,124 @@ |
1 | +# source C:/d/rapido/rapido2/actions_incitatives_2019/orientalmotor/test3.tcl | |
2 | + | |
3 | +# source C:/d/rapido/rapido2/actions_incitatives_2019/orientalmotor/test3.tcl | |
4 | +# set f [az_open] | |
5 | +# set cmd ":e1" | |
6 | +# puts -nonewline $f "${cmd}\r" | |
7 | +# flush $f | |
8 | + | |
9 | +# ==================================== | |
10 | +# === Tools : Open/close communication | |
11 | +# ==================================== | |
12 | + | |
13 | +proc az_close { } { | |
14 | + global audace | |
15 | + if {[info exists audace(orientalmotor,fid)]==0} { | |
16 | + error "Communication not previously opened" | |
17 | + } | |
18 | + set f $audace(orientalmotor,fid) | |
19 | + close $f | |
20 | + unset audace(orientalmotor,fid) | |
21 | +} | |
22 | + | |
23 | +proc az_open { {port //./COM6} } { | |
24 | + global audace | |
25 | + if {[info exists audace(orientalmotor,fid)]==1} { | |
26 | + error "Port $port ever opened" | |
27 | + } | |
28 | + set err [catch { | |
29 | + set f [open $port w+] | |
30 | + fconfigure $f -mode 115200,n,8,1 -buffering none -blocking 0 | |
31 | + after 2000 | |
32 | + } msg] | |
33 | + if {$err==1} { | |
34 | + error $msg | |
35 | + } | |
36 | + set audace(orientalmotor,fid) $f | |
37 | + az_delay 200 | |
38 | + return $f | |
39 | +} | |
40 | + | |
41 | +proc az_open_if_needed { } { | |
42 | + global audace | |
43 | + if {[info exists audace(orientalmotor,fid)]==0} { | |
44 | + az_open | |
45 | + } | |
46 | +} | |
47 | + | |
48 | +# ==================================== | |
49 | +# === Tools : Put/Get messages | |
50 | +# ==================================== | |
51 | + | |
52 | +proc az_delay { {delay ""} } { | |
53 | + global audace | |
54 | + if {$delay==""} { | |
55 | + set delay $audace(orientalmotor,delay) | |
56 | + } else { | |
57 | + set audace(orientalmotor,delay) $delay | |
58 | + } | |
59 | + after $delay | |
60 | +} | |
61 | + | |
62 | +proc az_put { cmd } { | |
63 | + global audace | |
64 | + az_open_if_needed | |
65 | + set f $audace(orientalmotor,fid) | |
66 | + puts -nonewline $f "${cmd}\r" | |
67 | + flush $f | |
68 | + az_delay | |
69 | +} | |
70 | + | |
71 | +proc az_read { } { | |
72 | + global audace | |
73 | + az_open_if_needed | |
74 | + set f $audace(orientalmotor,fid) | |
75 | + set result "" | |
76 | + set t0 [clock milliseconds] | |
77 | + set sortie 0 | |
78 | + while {$sortie==0} { | |
79 | + set res [read -nonewline $f] | |
80 | + if {$res==""} { | |
81 | + set sortie 1 | |
82 | + break | |
83 | + } | |
84 | + append result $res | |
85 | + set dt [expr [clock milliseconds]-$t0] | |
86 | + if {$dt>1000} { | |
87 | + set sortie 2 | |
88 | + break | |
89 | + } | |
90 | + after 100 | |
91 | + } | |
92 | + return $result | |
93 | +} | |
94 | + | |
95 | +proc az_putread { cmd } { | |
96 | + global audace | |
97 | + az_read | |
98 | + az_put $cmd | |
99 | + set res [az_read] | |
100 | + #return $res | |
101 | + # --- search for the echo of the command name | |
102 | + set k [string last ${cmd} $res] | |
103 | + if {$k<0} { | |
104 | + error "No response" | |
105 | + } | |
106 | + set res [string range $res $k end] | |
107 | + # --- search for the = symbol to isolate the value | |
108 | + set k [string first = $res] | |
109 | + if {$k>=0} { | |
110 | + set res [string range $res [expr 1+$k] end] | |
111 | + # --- take only the first element of the response as value | |
112 | + set res [split $res \n] | |
113 | + set value [lindex $res 0] | |
114 | + } else { | |
115 | + # --- take only the second element of the response as value | |
116 | + set res [split $res \n] | |
117 | + set value [lindex $res 1] | |
118 | + } | |
119 | + return $value | |
120 | +} | |
121 | + | |
122 | +# ==================================== | |
123 | +# === Tools : high level functions | |
124 | +# ==================================== | ... | ... |