Reading/Writing Serial Ports in Forte
The current application that I am working on has a need to communicate
from a UNIX box to some old software running on a PS/2. The
communication must be done via the serial ports. In late August we
proved that this could be done from Forte using some C wrappering.
However, for a variety of reasons, we would like to avoid C wrapopering
where possible. So we are now trying to find out whether we can
read/write to the UNIX serial ports using the Forte File class; after
all, a serial port in UNIX is just another device that appears in the
directory listing.
I was able to do some testing today with using the File class to access
the serial ports and found that it works beautifully for WRITING to the
ports. I can write code like the following:
toSend : TextData = 'TEST SEND\n';
serialPort : File = new();
serialPort.setPortableName('/dev/tty8');
serialPort.open(SP_AM_WRITE, FALSE);
serialPort.write(toSend.Value, toSend.LengthToEnd());
serialPort.close();
and the message will show up just fine on the other end.
Reading the port, on the other hand, does not seem to work. The
following:
serialPort : File = new();
serialPort.setPortableName('/dev/tty9');
serialPort.open(SP_AM_READ, FALSE);
toReceive : TextData = new();
rc : integer = serialPort.readText(toReceive, -1);
serialPort.close();
always returns -1 as the return code from readText(). (I am able to
confirm that there realy IS something on the port to be read by reading
it via a UNIX script.)
Has anyone out there done this sort of thing before? If you, can you
tell me what I'm doing wrong? I have a call in Forte Tech Support, as
well.
Thanks in advance for any help,
Dale
Dale V. Georg
Indus Consultancy Services
dgeorg@indcon.com
[1955 byte] By [
] at [2007-11-25 5:01:09]

Thanks to everyone who gave me feedback on this issue. It turns out
(as a few people pointed out to me) that I was making the call to
ReadText() incorrectly. I was specifying the number of bytes to read
as -1. What I was trying to do was tell it to read to the end of the
file, which means I should have specified 0. However, this wasn't the
entire answer. After making that change, the read on the port hung and
never returned. I was able to figure out, though, that because a
serial port is not a real file, the read was never going to find an EOF
in order to know when to stop reading. So, I started using the
ReadLine() method instead, and ensured that the program on the other
end of the serial connection was sending a newline character at the end
of each transaction. This worked beautifully! We still have a few
minor configuration issues to deal with on the serial ports themselves,
but at least we got the basic communication from Forte working.
Again, thanks for all the feedback.
Dale
at 2007-6-29 9:20:21 >

Hi George,
First up all you make sure the port works that can be tested by on one
terminal send and and on the other terminal you receive and one more thing
you make sure both are initialized for the same baud rate, parity, data
width and stop bits. If any of these parameters are wrong you will not
receive. If you still have problem, you send me I can clarify your doubts.
regards
ashok segu
Dale V. Georg wrote:
> The current application that I am working on has a need to communicate
> from a UNIX box to some old software running on a PS/2. The
> communication must be done via the serial ports. In late August we
> proved that this could be done from Forte using some C wrappering.
> However, for a variety of reasons, we would like to avoid C wrapopering
> where possible. So we are now trying to find out whether we can
> read/write to the UNIX serial ports using the Forte File class; after
> all, a serial port in UNIX is just another device that appears in the
> directory listing.
>
> I was able to do some testing today with using the File class to access
>
> the serial ports and found that it works beautifully for WRITING to the
>
> ports. I can write code like the following:
>
> toSend : TextData = 'TEST SEND\n';
> serialPort : File = new();
> serialPort.setPortableName('/dev/tty8');
> serialPort.open(SP_AM_WRITE, FALSE);
> serialPort.write(toSend.Value, toSend.LengthToEnd());
> serialPort.close();
>
> and the message will show up just fine on the other end.
>
> Reading the port, on the other hand, does not seem to work. The
> following:
>
> serialPort : File = new();
> serialPort.setPortableName('/dev/tty9');
> serialPort.open(SP_AM_READ, FALSE);
> toReceive : TextData = new();
> rc : integer = serialPort.readText(toReceive, -1);
> serialPort.close();
>
> always returns -1 as the return code from readText(). (I am able to
> confirm that there realy IS something on the port to be read by reading
>
> it via a UNIX script.)
>
> Has anyone out there done this sort of thing before? If you, can you
> tell me what I'm doing wrong? I have a call in Forte Tech Support, as
> well.
>
> Thanks in advance for any help,
> Dale
>
> Dale V. Georg
> Indus Consultancy Services
> dgeorg@indcon.com
at 2007-6-29 9:20:21 >

George,
I was puzzled by your use of -1 as the length parameter to ReadText.
Is this a typo? Otherwise, note that ReadText also returns -1 if
you try to read past end of stream.
My experience with asynch serial ports was always that they were very
finicky about the number of characters read and not "underflowing"
the buffer, etc.
Could you try specifying a length of 1 (in ReadText) in a while loop
that exits on ReadText returns -1?
Doug Welch
>> Reading the port, on the other hand, does not seem to work. The
>> following:
>>
>> serialPort : File = new();
>> serialPort.setPortableName('/dev/tty9');
>> serialPort.open(SP_AM_READ, FALSE);
>> toReceive : TextData = new();
>> rc : integer = serialPort.readText(toReceive, -1);
>> serialPort.close();
>>
>> always returns -1 as the return code from readText(). (I am able to
>> confirm that there realy IS something on the port to be read by reading
>Date: Tue, 09 Dec 1997 09:01:38 -0500
>From: ashok segu <ashoksegu@ibcweb.com>
>X-Mailer: Mozilla 4.03 [en] (Win95; I)
>Mime-Version: 1.0
>To: "Dale V. Georg" <dvgeorg@ibm.net>
>Cc: Forte Users <kamranamin@yahoo.com>,
>"Gardner," <steve_gardner@macktrucks.com>
>Subject: Re: Reading/Writing Serial Ports in Forte
>References: <199712090250.CAA24726@out5.ibm.net>
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit
>Sender: owner-forte-users@SageIT.com
>Precedence: bulk
>Reply-To: ashok segu <ashoksegu@ibcweb.com>
>Status: RO
>
>Hi George,
>First up all you make sure the port works that can be tested by on one
>terminal send and and on the other terminal you receive and one more thing
>you make sure both are initialized for the same baud rate, parity, data
>width and stop bits. If any of these parameters are wrong you will not
>receive. If you still have problem, you send me I can clarify your doubts.
>
>regards
>ashok segu
>
>Dale V. Georg wrote:
>
>> The current application that I am working on has a need to communicate
>> from a UNIX box to some old software running on a PS/2. The
>> communication must be done via the serial ports. In late August we
>> proved that this could be done from Forte using some C wrappering.
>> However, for a variety of reasons, we would like to avoid C wrapopering
>> where possible. So we are now trying to find out whether we can
>> read/write to the UNIX serial ports using the Forte File class; after
>> all, a serial port in UNIX is just another device that appears in the
>> directory listing.
>>
>> I was able to do some testing today with using the File class to access
>>
>> the serial ports and found that it works beautifully for WRITING to the
>>
>> ports. I can write code like the following:
>>
>> toSend : TextData = 'TEST SEND\n';
>> serialPort : File = new();
>> serialPort.setPortableName('/dev/tty8');
>> serialPort.open(SP_AM_WRITE, FALSE);
>> serialPort.write(toSend.Value, toSend.LengthToEnd());
>> serialPort.close();
>>
>> and the message will show up just fine on the other end.
>>
>> Reading the port, on the other hand, does not seem to work. The
>> following:
>>
>> serialPort : File = new();
>> serialPort.setPortableName('/dev/tty9');
>> serialPort.open(SP_AM_READ, FALSE);
>> toReceive : TextData = new();
>> rc : integer = serialPort.readText(toReceive, -1);
>> serialPort.close();
>>
>> always returns -1 as the return code from readText(). (I am able to
>> confirm that there realy IS something on the port to be read by reading
>>
>> it via a UNIX script.)
>>
>> Has anyone out there done this sort of thing before? If you, can you
>> tell me what I'm doing wrong? I have a call in Forte Tech Support, as
>> well.
>>
>> Thanks in advance for any help,
>> Dale
>>
>> Dale V. Georg
>> Indus Consultancy Services
>> dgeorg@indcon.com
>
>
>
at 2007-6-29 9:20:21 >

