Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/Windows-COM-port-communication-with-Expect.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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?

Please...no 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.

Code:
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
Code:
spawn -open $channel


HTH

Bruce



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.

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