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
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
Windows COM port communication with Expect

Posted by lanceh (lanceh), 17 April 2008
Does anyone have an example of an Expect script for communicating with a device connected to a Windows XP workstation's COM port? theories about that it should be possible.  I'd rather know if some has successfully done this before I put more time into trying to get this to work.  I've tried and apparently, I'm overlooking something.

Posted by lanceh (lanceh), 8 October 2008
I'm surprised no one has wanted to use Expect to control a device serially through a PC Communication port, live a WTI switch connected serially to a network switch(unconfigured).

Below is something I found useful for communication over a TCP connection, but I haven't had time to get it to work with COM ports yet:

proc send_string {expectTimeout spawnid stringToSend expectedOutput} {
  global displayLevel
  if {![info exists displayLevel]} {set displayLevel 3}
  if {$displayLevel >= 7} {
     send_user "\n\nINFO: (send_string) received: ($expectTimeout)"
     send_user "\nINFO: (send_string) received: ($spawnid)"
     send_user "\nINFO: (send_string) received: ($stringToSend)"
     send_user "\nINFO: (send_string) received: ($expectedOutput)\n"
  set timeout $expectTimeout
  set spawn_id $spawnid
  expect "*"
  send -i $spawnid "$stringToSend\r"
  expect {
     "$expectedOutput" {set recBuffer $expect_out(buffer)}
     timeout {send_user "\nCRITICAL: Timeout(send_string) on spawnid($spawnid)!\n Sent=($stringToSend)\n Expected output=($expectedOutput)"
        set recBufferTimeout ""
        append recBufferTimeout $expect_out(buffer)
        set recBuffer ""
        append recBuffer $recBufferTimeout
        send_user "\n\nINFO: (send_string) The expected output after sending the string($stringToSend) was not found within ($expectTimeout) seconds.\n [timestamp]\n Content of receiver buffer=($recBufferTimeout)\n\n\n"

        #Grab stats via serial console.
           source "lib//lib_grab_stats_on_timeout.tcl"

        global flag_breakForLoop
        set flag_breakForLoop "yes"
  set timeout 30
  if {$displayLevel >= 9} {send_user "\nDEBUG: (send_string) returning: recBuffer=($recBuffer)\n\n\n"}
  return $recBuffer

Posted by Custard (Custard), 25 November 2008
I don't know if this helps, but this is an extract from a bit of code I wrote in 1996 which connects to windows (95!) com ports.

set SYS(serial_DOS) "com1:"
set channel [open $SYS(serial_DOS) RDWR]
fconfigure $channel -mode 9600,n,8,1 -blocking 0 -translation binary
puts $channel "Some Data"
flush $channel

close $channel

I believe you can 'spawn' to an opened file handle with
spawn -open $channel



This page is a thread posted to the opentalk forum at 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., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho