Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to Open COM1 Port as File Under Windows/Tk?

362 views
Skip to first unread message

ray...@my-deja.com

unread,
Oct 4, 2000, 3:00:00 AM10/4/00
to
Hello. My appliation requires that I open the COM1 port on my PC as
a file using Tk. The idea is to setup a frame widget that operates
similar to the HyperTerm application under Windows. The application
I'm trying to control is a simple "dumb term", serial I/O (console
display, keyboard response) type. Once I have the frame set up with
the correct display, I plan to control the keyboard input through
buttons, command menus, etc. OK??

If you could give some specific examples or refer me to a good
reference, I would appreciate it. P.S.- I have read most of the
Ousterhout book but haven't found any description so far.


Sent via Deja.com http://www.deja.com/
Before you buy.

Jeffrey Hobbs

unread,
Oct 4, 2000, 3:00:00 AM10/4/00
to ray...@my-deja.com
ray...@my-deja.com wrote:
> Hello. My appliation requires that I open the COM1 port on my PC as
> a file using Tk. The idea is to setup a frame widget that operates

You have to do something like:
set serial [open "COM1:" r+]

and then $serial is a channel to the com port.

> reference, I would appreciate it. P.S.- I have read most of the
> Ousterhout book but haven't found any description so far.

The Ousterhout predates serial device support, along with many other
features. I highly recommend getting the latest (3rd) edition of
Brent Welch's Practical Programming in Tcl and Tk.

--
Jeffrey Hobbs The Tcl Guy
hobbs at ajubasolutions.com Ajuba Solutions (née Scriptics)

Rolf Schroedter

unread,
Oct 5, 2000, 3:00:00 AM10/5/00
to
Maybe the appended simple terminal application helps.

Its opens and configure the serial port
[open ...] [fconfigure ...]
A event handler is defined for incoming characters from the serial
port
[fileevent $chan readable receiver].
And a keyboard event is specified for typed characters to be sent
to the serial port
[bind ...].

Regards,
Rolf

ray...@my-deja.com wrote:
>
> Hello. My appliation requires that I open the COM1 port on my PC as
> a file using Tk. The idea is to setup a frame widget that operates

> similar to the HyperTerm application under Windows. The application
> I'm trying to control is a simple "dumb term", serial I/O (console
> display, keyboard response) type. Once I have the frame set up with
> the correct display, I plan to control the keyboard input through
> buttons, command menus, etc. OK??
>
> If you could give some specific examples or refer me to a good

> reference, I would appreciate it. P.S.- I have read most of the
> Ousterhout book but haven't found any description so far.
>

> Sent via Deja.com http://www.deja.com/
> Before you buy.

---------------------------------------------------------------------
German Aerospace Center Rolf Schroedter
Inst. of Planetary Exploration Tel/Fax: +49 (30) 67055-416/384
Rudower Chaussee 5, D-12489 Berlin Internet: Rolf.Sc...@dlr.de

SimpleTerm.tcl

ray...@my-deja.com

unread,
Oct 7, 2000, 3:00:00 AM10/7/00
to
In article <39DB6B4E...@ajubasolutions.com>,

Jeffrey Hobbs <jeffre...@ajubasolutions.com> wrote:
> The Ousterhout predates serial device support, along with many other
> features. I highly recommend getting the latest (3rd) edition of
> Brent Welch's Practical Programming in Tcl and Tk.
>
> --
> Jeffrey Hobbs The Tcl Guy
> hobbs at ajubasolutions.com Ajuba Solutions (née Scriptics)
>
Excellent reference, thank you. This books seems to have the informa-
tion I need.

ray...@my-deja.com

unread,
Oct 7, 2000, 3:00:00 AM10/7/00
to
In article <39DC1EC4...@dlr.de>,
Rolf Schroedter <Rolf.Sc...@dlr.de> wrote:
> This is a multi-part message in MIME format.
>
> [Code sample appended here.]

>
> ---------------------------------------------------------------------
> German Aerospace Center Rolf Schroedter
> Inst. of Planetary Exploration Tel/Fax: +49 (30) 67055-416/384
> Rudower Chaussee 5, D-12489 Berlin Internet: Rolf.Sc...@dlr.de
>
Thanks very much for your kind sample. I will try it ASAP and let
you know how it works on my system. Aufwiedersein...

ray...@my-deja.com

unread,
Oct 7, 2000, 3:00:00 AM10/7/00
to
I tried Rolf's code and the system returns the following message when
I try to source the TCL file:

"couldn't open "com1", permission denied"

I also tried the following:

set chan [open "COM1:" r+] (vs. set chan [open com1 r+])
but still get the same error msg.

I am using Windows NT system and have no terminal windows (e.g.,
HyperTerm) open. Also, tried on Win98 system with same results.

Any ideas/suggestions?

Rolf Schroedter

unread,
Oct 9, 2000, 2:01:14 AM10/9/00
to

Are you sure, that there are no other processes using com1: ?

You may also have an unclosed wish pending.
Typically I get those things with:

console show
wm withdraw .


set chan [open com1 r+]

# if you now don't close the console-window with [exit] or "File-Exit"
# but wth a click onto [x] you'll have a dead wish83 hanging.

Rolf.

--

ray...@my-deja.com

unread,
Oct 11, 2000, 10:28:16 PM10/11/00
to
In article <39E15F2A...@dlr.de>,

Rolf Schroedter <Rolf.Sc...@dlr.de> wrote:
>
> Are you sure, that there are no other processes using com1: ?
>
OK, I got past the "fail to open com1" problem, now I get the following:

In proc term_in, there is a failure to process line 11 when I start
receiving characters. The exact command WISH83 complains about is
"$txt see end".

Also, I could not get the frame to display at all until I broke up
the command:

set Term(Text) [scrolled_text .t -width 80 -height 25 -font Courier ] \
pack .t -side top -fill both -expand true

Had to break it into two seperate lines (just like above w/o the \
delimiter).

Any ideas/suggestions what to try next? (Thanks!)

Rolf Schroedter

unread,
Oct 12, 2000, 1:46:48 AM10/12/00
to ray...@my-deja.com

I can't understand what happened. For me the program works (Tcl822).
"$txt see end" works fine for me.
Also I don't have merged lines as you had.
Did the mail-program mangle the file ?
Here is the source again, today not as an attachment,
but with Copy&Paste.

file SimpleTerm.tcl:

###############################################################################
# Term for a simple terminal interface
# !!! fileevent funktioniert NICHT mit Term unter Win95 !!!
# --> Polling
###############################################################################
# The terminal bindings are implemented by defining a new bindtag 'Term'
###############################################################################

set Term(Text) {}

##################### Terminal In/Out events ############################
proc term_out { chan key } {
switch -regexp -- $key {
[\x07-\x08] -
\x0D -
[\x20-\x7E] { puts -nonewline $chan $key; return -code break }
[\x01-\x06] -
[\x09-\x0C] -
[\x0E-\x1F] -
\x7F { return }
default { return }
} ;# switch
}

proc term_in { ch } {
upvar #0 Term(Text) txt

switch -regexp -- $ch {
\x07 { bell }
\x0A { # ignore }
\x0D { $txt insert end "\n" }
default { $txt insert end $ch }
}
$txt see end
}

proc receiver {chan} {
foreach ch [ split [read $chan] {}] {
term_in $ch
}
}

##################### Windows ############################
proc scrolled_text { f args } {
frame $f
eval {text $f.text \
-xscrollcommand [list $f.xscroll set] \
-yscrollcommand [list $f.yscroll set]} $args
scrollbar $f.xscroll -orient horizontal \
-command [list $f.text xview]
scrollbar $f.yscroll -orient vertical \
-command [list $f.text yview]
grid $f.text $f.yscroll -sticky news
grid $f.xscroll -sticky news
grid rowconfigure $f 0 -weight 1
grid columnconfigure $f 0 -weight 1
return $f.text
}

##### main #######


set chan [open com1 r+]

fconfigure $chan -mode "19200,n,8,1" -translation binary -buffering none -blocking 0
fileevent $chan readable [list receiver $chan]

set Term(Text) [scrolled_text .t -width 80 -height 25 -font Courier ]
pack .t -side top -fill both -expand true

bind $Term(Text) <Any-Key> [list term_out $chan %A]

console hide

ray...@my-deja.com

unread,
Oct 12, 2000, 3:00:00 AM10/12/00
to
In article <39E55047...@dlr.de>,
Rolf Schroedter <Rolf.Sc...@dlr.de> wrote:

> I can't understand what happened. For me the program works (Tcl822).
> "$txt see end" works fine for me.
> Also I don't have merged lines as you had.
> Did the mail-program mangle the file ?

> Here is the source again...

Got it working, thanks to Rolf for a great little script.

I had a couple of little problems, as follows:

1) On my system, for some reason you have to startup a HyperTerm
window first and establish your connection. Then just exit HyperTerm
and start the script. Else I get "fail to open com1" as described
previously. Why, I dunno...

2) My shell doesn't like like the pack statement on the same line with
set or the fileevent statement on the same line with fileconfig at
the end of the script so just seperated them.

3) Changed from 19200 to 9600 baud for my target system (but this
is no-brainer..)

3) Pasted below is my final working version with above changes FYI:

########################################################################


#
# Term for a simple terminal interface
# !!! fileevent funktioniert NICHT mit Term unter Win95 !!!
# --> Polling
########################################################################
#

# The terminal bindings are implemented by defining a new bindtag 'Term'
########################################################################
#

set Term(Text) {}

return $f.text
}

fconfigure $chan -mode "9600,n,8,1" -translation binary -buffering none


-blocking 0
fileevent $chan readable [list receiver $chan]

set Term(Text) [scrolled_text .t -width 80 -height 25 -font Courier ]
pack .t -side top -fill both -expand true

bind $Term(Text) <Any-Key> [list term_out $chan %A]

console hide


0 new messages