gforth
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [gforth] Debugging serial I/O to USB RS232 port


From: Bernd Paysan
Subject: Re: [gforth] Debugging serial I/O to USB RS232 port
Date: Sat, 01 Nov 2014 01:57:31 +0100
User-agent: KMail/4.11.5 (Linux/3.11.10-21-desktop; KDE/4.11.5; x86_64; ; )

Am Freitag, 31. Oktober 2014, 17:21:24 schrieb Jerry DeLisle:
> I have found a curious problem.
> 
> I have two different USB to RS-232 devices I am testing with on Linux based
> system.
> 
> With one of the devices the following works without problem, the other
> hangs.
> 
> 0 Value port
> 
> : open-port  ( addr u -- )  r/w open-file throw to port ;
> 
> s" /dev/ttyUSB0" open-port
> 
> Using the device that hangs requires a Ctrl-C at the gforth command terminal
> to bring back the "ok" prompt.
> 
> The following C code works for both devices.
> 
> int
> open_port (void)
> {
>    int fd; /* File descriptor for the port */
> 
>    fd = open ("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
>    if (fd == -1) /* Could not open the port.  */
>      perror("open_port: Unable to open /dev/ttyUSB0 ");
>    else
>      fcntl (fd, F_SETFL, 0);
> 
>    return (fd);
> }
> 
> To attempt to emulate the working C code I tried the following:
> 
> 256  CONSTANT noctty
> 2048 CONSTANT ndelay
> 0 Value port
> 
> : open-port  ( addr u -- )  r/w noctty ndelay + +
> 
>      open-file throw to port ;
> 
> s" /dev/ttyUSB0" open-port
> 
> This also hangs with the one device.
> 
> Looking through the gforth engine code, support.c (guessing this is the
> spot)
> 
> I see where I think the wfam is being truncated, at line 113:
> 
> fd = open(s, flags|ufileattr[wfam & 0x0F], (wfam >> 4) ^ 0666);
>                               ^^^^^^^^^^^

It's not truncated, wfam is simply not the Unix constant for open.  Actually, 
there are only three different possibilities: r/o, r/w, and w/o (with bin 
changing the lsb, but having no effect).

> I would prefer to not add additional interface C code to gforth.
> 
> Am I understanding this correctly?
> 
> Any suggestions on how to pass those flags to an open?
> 
> Is there another primitive I should be using instead?

No, if you want to have direct access to open, use a special C interface.  I 
think I should add open and fdopen (to get an fd) to the serial.fs code, so 
that you can use open directly, and try different flags.  We also found that 
on some serial ports, you don't even want to use fdopen, because it sets some 
things which the serial port driver can't handle... this means, you also need 
read/write to access the port.

-- 
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://bernd-paysan.de/

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]