-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialusb.tcl
More file actions
102 lines (76 loc) · 2.4 KB
/
Copy pathserialusb.tcl
File metadata and controls
102 lines (76 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#serial I/O with line oriented device
namespace eval serialusb {
variable timeError 0
variable devices {}
variable usbpaths {}
}
proc serialusb::send { chan command } {
puts $chan $command
return
}
proc serialusb::receive {chan endstring {timeout 10} } {
variable timeError
set lastResults ""
set polltimer [expr $timeout/10]
#poll for early end...
for {set i 0} {$i<10} {incr i} {
if { [ eof $chan ] } {
catch { close $chan }
return "End of file encountered."
}
append lastResults [read $chan]
if { [regexp $endstring $lastResults] } { break }
after $polltimer
}
#Not necessarily an error, but set flag for a timeout without
#seeing the expected termination string
if { $i == 10 } {
set timeError 1
} else {
set timeError 0
}
return $lastResults
}
##proc serialusb::connect { {port "/dev/ttyUSB0"} {mode "115200,n,8,1"} } {
proc serialusb::connect { {port "/dev/ttyUSB0"} {mode "9600,n,8,1"} } {
set chan [open $port r+]
fconfigure $chan -mode $mode -translation crlf -buffering line -blocking 0
return $chan
}
#create a list of all the usb devices which MAY be mesytec modules
#(the serial interface chip is common, and there is not a way to distinguish
# a mesytec module from any other device).
#
#use an exec to find to get a list of all the symbolic links created by
#udev for the ttyUSB devices. Exclude the symbolic links for
#the tty devices that they use
proc serialusb::connectionlist { } {
variable devices {}
variable usbpaths {}
# set pathlist [split [exec find /sys/devices -name \*ttyUSB\* | grep -v "/tty/" | sort ] "\n"]
if { [ catch \
{ exec find /sys/devices -name \*ttyUSB\* | grep -v "/tty/" | sort } \
rawpaths ] } {
puts "No appropriate devices found..."
return "None"
}
set pathlist [split $rawpaths "\n"]
foreach path $pathlist {
set parts [file split $path]
set obfuscatedpath [lindex $parts end-1 ]
set devicename "/dev/[lindex $parts end ]"
#in the path, the part up to the ":" is bus-port.port.port..."
set busports [ split $obfuscatedpath ":" ]
set busports [ split [ lindex $busports 0 ] "-." ]
set partname "bus"
set ourlabel ""
foreach port $busports {
set ourlabel "$ourlabel$partname$port"
set partname "port"
}
# puts "$ourlabel $devicename"
lappend devices $devicename
lappend usbpaths $ourlabel
}
return $devices
}