Using TK to read/write to serial port, allowing stdout display and TK GUI control



Hello,

I'm using a TK GUI to write data to and read data from a serial port.
One of the button widgets needs to send a specific command to the
serial port, which programs a microprocessor to essentially run in a
loop until interrupted. To interrupt the microprocessor, I am using the
same TK GUI (another button widget) to send the appropriate interrupt
character to the serial port.

To try to implement this, I send the initial microprocessor programming
command to the serial port, and it's received correctly. The
microprocessor then sends the text strings back to the TK GUI. What I
want to do is read the data from the serial port, and display it to
stdout, but still allow the TK GUI to send a character down the serial
port when the button widget is pressed, causing the microprocessor loop
to be terminated. When terminated, the microprocessor sends a '>'
character, which would be easy to parse from a read command.

I'm new to TK, so I'm not sure how to achieve the above. I tried using
the code snipped below. The first chunk sends the 'M' character to the
serial port, which programs it. I then read from the serial port byte
by byte, until I receive the acknowledgment command "Waiting for
trigger". This works. It's the second chunk that fails miserably! The
TK GUI hangs in a loop, because I can never invoke the TK button widget
to send the break command.

If this is a bit complicated to do, rather than write to stdout, I
could use message windows to relay info (e.g. to essentially echo back
the commands from the microprocessor), as long as the TK GUI button
widget can still send the interrupt command.

If anyone has any suggestion for how I can get this to work, I'd really
appreciate it.

Thanks!

****

set i 0
fileevent $comm_port writable [flush $comm_port; puts $comm_port
"M"; flush $comm_port]
fileevent $comm_port readable [set read_str [read $comm_port 1]]
while { ! [regexp {Waiting for trigger} $read_str match] } {
fileevent $comm_port readable [append read_str [read $comm_port
1]]
incr i
if {$i > 100000} {
puts "Error entering stim mode \"M\", exiting TCL from routine
WriteScript ..."
puts "read_str: $read_str"
exit
}
}

fileevent $comm_port readable [set read_str [read $comm_port 1]]
while { ! [regexp {> } $read_str match] } {
fileevent $comm_port readable [set read_str [read $comm_port 1]]
puts "$read_str"
}

****

.