Unable to read PC serial port

Ned Konz ned at bike-nomad.com
Wed Aug 22 04:46:11 UTC 2001


On Tuesday 21 August 2001 08:53 pm, you wrote:
> Some novice questions:
> I need to take input from a PC serial port (Windows 2000, Alpha
> updates to 4261) and stream it to a scrollable editable window and
> vice versa for keyboard input, back to the port.
> I'm not making much progress so far. Would PluggableTextMorph be the
> one to use?

That's not a bad choice.

I'd consider doing this in at least two different threads (one you add as 
well as the UI thread) and using WorldState addDeferredUIMessage: to 
communicate the received bytes to your PluggableTextMorph. You can send the 
keyboard bytes out the port as you get them, or you could have another thread 
handling this (connected with a SharedQueue).

The advantage of addDeferredUIMessage: is that it can call methods on your 
Morphs directly without worrying about being in the wrong thread context (the 
things sent to addDeferredUIMessage: are evaluated in the UI thread).

> How does one go about getting the text from the port to flow into the
> window? Starting from Ned's recent tips, I've tried the code below and have
> managed to output to the port but reading doesn't seem to work.
>   --------------------
> p _ SerialPort new initialize;

You don't need to call initialize here; new already does.

> baudRate: 9600;
> dataBits: 8;
> inputFlowControlType: 2;
> outputFlowControlType: 2;

Are you _sure_ you have hardware flow control? Are you _sure_ it's wired 
right?

> p readInto: q startingAt: 1.

Remember that readInto:startingAt: won't block (we don't have the API for 
that). So you have to look at the return value from this call to see if you 
actually got any bytes. The returned value will be > 0 if you did (and you 
can then use that many bytes).

What you're seeing is your uninitialized string with 0 bytes having been read 
into it.

Look at readString and readByteArray which return empty strings and/or byte 
arrays if nothing is read.

Why don't you test this first with a loopback: connect pin 2 to 3 (assuming a 
DE-9 or DB-25 connector), don't use hardware handshaking (comment out the 
*flowControlType: commands), and then see if you get what you send. You have 
too many variables if you're also trying to talk to an external device before 
you know that you can communicate.

-- 
Ned Konz
currently: Stanwood, WA
email:     ned at bike-nomad.com
homepage:  http://bike-nomad.com




More information about the Squeak-dev mailing list