Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Serial Port RS232

Posted by Yvanou (Yvanou), 11 May 2007
Hello,

I have to develop a software in order to communicate  with a presure measurment device linked to a PC (Windows 95) with a RS232 serial port. I have to use tcl/tk language.

I can open the port "com2" with:

set com2 [open com2: r+]

I configure with fconfigure:

fconfigure $com2 -mode 9600,8,n,1 -buffering none -blocking 0

I try to send message and receive response...:

puts $com2 instruction
set resp [read $com2]
puts $resp

... but the device send me a blank line...

What can I do?  

I'm working on it for 3 days long.

Thanks

Yvanou

Posted by admin (Graham Ellis), 11 May 2007
Because you've set blocking off, your read won't wait for andy input and you'll get an empty string back.

Posted by iamxiaop (iamxiaop), 16 July 2007
fconfigure命令可以打开串口,比如:
set tty [open /dev/ttyS0]
fconfigure $tty -mode 9600 0 8 2
上面的是在Linux里面的方式,-mode后面分别表示波特率/控制位/数据位/停止位
如果是windows,则控制台使用com1,比如:
set tty [open /dev/com1]

我按上述方法实现了打开串口,由于交换机要输入用户名和密码登录,我发现通过向串口写数据无法登录成功。
puts $iChannel "$username\r"; puts $iChannel "$password\r"
puts $iChannel "enable"

目前我正在写自动化测试程序,使用主控结构,由main.tcl与各个测试例文件组成。
main.tcl主要实现函数库的加载、各个变量与设备初始化、调用各个测试例运行测试,各个测试例实现具体的测试功能。

注:串口可以当作文件设备使用,示例代码如下:
#-------------------------------------------------------------------------------#
# 初始化串口
#-------------------------------------------------------------------------------#
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
  update
  after 2000
  set cap [read -nonewline $iChannel]
  return "$cap"  
}
#-------------------------------------------------------------------------------#
# 向串口中写数据
#-------------------------------------------------------------------------------#
proc SendCmd {channel command} {
  global output debug

  set letter_delay 10  
  set commandlen [string length $command]
  for {set i 0} {$i < $commandlen} {incr i} {  
     set letter [string index $command $i]
     after $letter_delay
     puts -nonewline $channel $letter
     if {$debug(dutConfig) == 1} {puts -nonewline $output "$letter"}
  }
  after $letter_delay  
  puts -nonewline $channel "\n"
  if {$debug(dutConfig) == 1} {puts $output ""}
  after 500
  flush $channel
}
&#23545;&#35774;&#22791;&#30340;&#37197;&#32622;&#20063;&#21487;&#20197;&#36890;&#36807;telnet&#30340;&#26041;&#24335;&#36827;&#34892;&#12290;
&#31034;&#20363;&#65306;
  package require Expect
  set hostname 192.168.1.1
  set password admin

  exp_spawn telnet $hostname
  expect "*Password:" {exp_send "$password\n"}
  expect "*>"         {exp_send "en\n"}
  expect "Password:"  {exp_send "$password\n" }
  # Do More ...


Posted by iamxiaop (iamxiaop), 16 July 2007
http://www.wellho.net/cgi-bin/opentalk/YaBB.pl?board=tcl;action=display;num=1138163407;start=0



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho