LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Expect spawn_id closes unexpectedly (https://www.linuxquestions.org/questions/programming-9/expect-spawn_id-closes-unexpectedly-4175493848/)

eliasson 02-05-2014 03:10 PM

Expect spawn_id closes unexpectedly
 
Hello,

I'm having a problem getting an expect script to work with a serial port. In the past, I've had success with expect using telnet & ssh, but I left scripting for some time. Now I'm back to it & working on automating some operations taking place over a serial port. So I have a simple script that opens the serial port, expects a set of 3 strings to come across, & lets me know they arrived. This is a simple test but I can't get it to consistently work. Most of the time, the script fails with; 'expect: spawn_id exp10 not open'. I'm too dense to pull out of the book on expect any reason why this happens & am hoping someone here may help. I'll try to post enough script details for a possible answer while not overdoing it ...

proc openSerial {baud_rate ser_dev} {
set portId [open $ser_dev RDWR]
if {[catch {chan configure $portId -mode "$baud_rate,n,8,1" } error]} {
exp_send_user "could not configure &ser_dev parms\n"
return 0
} else {
spawn -open $portId -noecho
return $spawn_id
}
return 0
}

Main body of script ...

Opening the serial port:
set baud 115200
set serDev /dev/ttyUSB0
set comId00 [openSerial $baud $serDev]
sleep 5
puts "serial spawn id is $comId00\n"

At this point I see exp10 output as the spawn ID ... so I do the following:

expect {
"<first string>"
{ puts "got it ...\n"
expect "<next string>"
puts "got string 2 ...\n"
expect "<last string>"
puts "got final string ...\n" }
timeout { do timeout stuff ... }
}

When I run this, I see string 1 & string 2 as expected. Then I see the 'spawn id not open' message & the script fails. I guess I don't really understand what's happening with spawn & how that relates to the serial port? I need to continue working with this port & can't even get started so far. Any help will be appreciated.

ACE

eliasson 02-21-2014 01:45 PM

Solved this ...
 
So, after much pulling of hair & gnashing of teeth, I got past this problem. Turns out the script was ok, it was my interaction with the serial port after power up, or power cycle, of the host. The 'putty' utility was not having any problem accessing the port after power up, so I compared what 'putty' was doing to initialize the port with the port settings directly after power up. I now 'exec' a short C program to set the port attributes based on those 'putty' uses, before running 'opening' the port in the script, & it's working.

ACE


All times are GMT -5. The time now is 08:17 AM.