Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening a serial port with different in and out baudrate 1

Status
Not open for further replies.

Froody

Technical User
Jul 22, 2003
6
0
0
AU
I have a data logger script which reads from a serial port at 38400 baud. I want to output to the same port a dummy message once a second at a variable rate of 1200 to 9600 baud. Is there an easy way to do this (ie using fconfigure) or do I have to get in the weeds ( ie in the C code )
 
Well, Tcl doesn't let you set different baud rates for input and output on a serial channel. You'll have to switch the baud rate yourself as needed. The fconfigure -mode option allows you to do this. If you don't supply a value, the -mode option returns the current serial port settings, in "baud,parity,data,stop" format. You can change the settings by providing new values in the same format. For example (untested):

Code:
set chan [open "COM2:" r+]

# Read the current channel settings. Let's assume that
# we'll retain all the settings except for baud rate,
# which we'll want to change for sending data on the
# serial port.

set input [fconfigure $chan -mode]

# Split the values into separate variables.

foreach {baud parity data stop} $input {break}

# Assemble our channel settings for sending data.

set output "1200,$parity,$data,$stop"

# Change the channel mode to send some data.

fconfigure $chan -mode $output

puts $chan "My data"

# Go back to the original setting to read some data.

fconfigure $chan -mode $input

set num [gets $chan line]

You might want to go to the Tcl'ers Wiki ( and read some of the pages there regarding the fconfigure command and serial port I/O. Here are some starting points: "fconfigure," "Serial Port," "More Serial Port Samples,"
- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top