[NEWBIE] fileevent problem?
From: Tim C (cannedmeat_at_crone.us)
Date: 10/28/03
- Next message: Bob Techentin: "Re: moniter a folder?"
- Previous message: Mads Linden: "Re: fatal error on winxp/w2k - threads ? - 2 procs ?"
- Next in thread: Derk Gwen: "Re: [NEWBIE] fileevent problem?"
- Reply: Derk Gwen: "Re: [NEWBIE] fileevent problem?"
- Reply: Rolf Schroedter: "Re: [NEWBIE] fileevent problem?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 28 Oct 2003 12:34:54 -0600
I've started writing in TCL, after a long stint in the C world (among
others). To do what I want to do, I needed to open a serial port
for both read and write.
I'm using the latest TCL 8.4.4, compiled within the past week on a
hacked install of RedHat 7.1.
I tried to open the serial port as RDWR, and was able to write but not
read. (Perhaps the open pointer retains the characteristics of the
first operation?)
Anyway, I worked around that by creating two pointers, a serial out
and a serial in. I was then able to write and read, respectively.
Not willing to leave well enough alone, I wanted to use fileevent to
drive the data collection. I looked through everything I could
find on the web or usenet, and sadly turned up nothing that worked.
(I did, however, paste several scripts that contained fileevents
into VI, and was not able to successfully get them to read.)
Code is attached. (Linebreaks are not in the actual code,
incidentally.) Thanks in advance for any tips you can offer!
-TC
---------------
#!/usr/bin/tcl
##############
# Rudimentary communication with serial port.
# Sends the string "show inventory" out.
# Returns the resultant values to the user.
##############
proc readSerial {cliIn} {
global cliDataIn
set cliDataChunkIn [read $cliIn]
append cliDataIn $cliDataChunkIn
}
# Default 10/28/03 is 9600 8n1n, serial port 1.
set serialPort /dev/ttyS0
set serialBaud 9600
set serialDataBits 8
set serialParity n
set serialStopBits 1
set serialHandshake none
# fconfigure sets the serial communication
set cliOut [open $serialPort WRONLY]
set cliIn [open $serialPort RDONLY]
if [catch [fconfigure $cliOut -mode \
$serialBaud,$serialParity,$serialDataBits,$serialStopBits \
-handshake $serialHandshake -blocking false -buffering line]] {
puts "Cannot configure serial port $serialPort for output."
close $cliOut
exit 1
}
if [catch [fconfigure $cliIn -mode \
$serialBaud,$serialParity,$serialDataBits,$serialStopBits \
-handshake $serialHandshake -blocking false -buffering line]] {
puts "Cannot configure serial port $serialPort for input."
close $cliIn
exit 1
}
fileevent $cliIn readable [readSerial $cliIn]
# Send out data
puts $cliOut "show inventory"
puts "Sent to the cli."
## I expect the fileevents _here_, so that there is nothing in the read
## buffer for serialReturn.
# fconfigure $cli -blocking true
after 5000
set serialReturn [read $cliIn]
puts "was $serialReturn"
# fconfigure $cli -blocking false
puts "The return was:\n$cliDataIn"
close $cliOut
close $cliIn
exit 0
-- swap tim01301 and cannedmeat to reply
- Next message: Bob Techentin: "Re: moniter a folder?"
- Previous message: Mads Linden: "Re: fatal error on winxp/w2k - threads ? - 2 procs ?"
- Next in thread: Derk Gwen: "Re: [NEWBIE] fileevent problem?"
- Reply: Derk Gwen: "Re: [NEWBIE] fileevent problem?"
- Reply: Rolf Schroedter: "Re: [NEWBIE] fileevent problem?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]