用tcl语言实现串口数据的读取

用脚本实现了从pc到设备之间的串口连接,现在的图形界面类似于secureCRT,可以进行login、查询和修改各种配置等。

proc ComSetup {ComPort ComRate} {
set iChannel [open $ComPort w+]
set rate $ComRate
fconfigure $iChannel -mode $ComRate,n,8,1
fconfigure $iChannel -blocking 0
fconfigure $iChannel -buffering none
fileevent $iChannel readable ""
return $iChannel
}
proc GetData {iChannel} {
global output
global timeout 
update
after $timeout
set cap [read -nonewline $iChannel]
return "$cap"
}

proc write32 {addr data } {
	global iChannel
	global str_write
	global timeout 

	puts [GetData $iChannel]
	puts -nonewline $iChannel "\r"
	puts -nonewline $iChannel "$str_write $addr $data \r"
	puts -nonewline $iChannel "\r"
}
proc read32 {addr } { 
	global iChannel
	global output
	global str_read
	global timeout 
	puts  $iChannel "\r"
	
	puts [GetData $iChannel]
	puts -nonewline $iChannel "\r"	
	puts -nonewline $iChannel "$str_read $addr \r"
	puts [GetData $iChannel]
	set output [GetData $iChannel]
	return [string range   $output  24 34]

}
set str_write "fpga_spi w"
set str_read  "fpga_spi r"
set timeout 100

set ComPort COM3
set ComRate 115200
set iChannel [ComSetup $ComPort $ComRate]


puts -nonewline $iChannel "\r"
after $timeout
puts [GetData $iChannel]
puts -nonewline $iChannel "root\r"
after $timeout
puts [GetData $iChannel]
puts -nonewline $iChannel "root\r"


write32 1 1

read32 2

read32 4
 
after $timeout

你可能感兴趣的:(tcl,串口,fpga开发,tcl,串口)