caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Anders Fugmann <anders@fugmann.net>
To: Edouard Evangelisti <edouardevangelisti@gmail.com>,  caml-list@inria.fr
Subject: Re: [Caml-list] Unix library and serial port
Date: Wed, 13 Aug 2014 13:22:38 +0200	[thread overview]
Message-ID: <53EB4A7E.5080903@fugmann.net> (raw)
In-Reply-To: <sympa.1407913958.2059.874@inria.fr>

Hi Edouard,

Setting the nonblock flag on the socket specifies exactly that calls 
will never wait but raise EGAGIN when no data is available. That is - no 
operations on the socket will ever block.

I would advice you to not open using the O_NONBLOCK flag, and instead
read exactly the amount of bytes you expect. If, for example, the motor 
returns data ending with newlines, I would read one char at a time until 
the terminating char is encountered and then parse the message received.

Another solution is to poll for data and then sleep a bit when EAGAIN is 
raised. Or even use select.

/Anders

On 08/13/2014 09:23 AM, Edouard Evangelisti wrote:
> Dear all,
>
> I encountered a problem while trying to exchange informations with a ZStepper
> Motor connected to the computer through a serial/USB adapter. I can send
> commands and move the motor, but I cannot retrieve the informations which are
> sent by the motor directly after completion. For this I am using the Unix
> library as follows.
>
> First of all, I get a read/write file descriptor :
>
> let usb0 = "/dev/ttyUSB0"
> let flags = Unix.([O_RDWR; O_NOCTTY; O_NONBLOCK])
> let perm = 0o666
> let fd = Unix.openfile usb0 flags perm
>
> I then set the parameters using Unix.tcgetattr/Unix.tcsetattr (for instance
> baud and parity check). Most of the default values are fine. Then I am able to
> send commands :
>
> let write_serial () =
>   let cmd = "/2P1000R\r\n" in (* one command as  an example. *)
>   let len = String.length cmd in
>   let ret = Unix.write fd cmd 0 len in
>   assert (ret = len); (* not the real code here for error management *)
>   Unix.tcdrain fd
>
> The call to write_serial triggers ZStepper motion as expected. Then, the motor
> send back some data. I have tried to read them using :
>
> let read_serial () =
>     let len = 10 in
>     let buf = Buffer.create len in
>     let str = String.create len in
>     let rec loop () =
>     let n = Unix.read fd str 0 len in
>     if n > 0 then (
>       Buffer.add_substring buf str 0 n;
>       loop ()
>     ) else Buffer.output_buffer stdout buf in
>     try loop () with Unix.Unix_error (Unix.EAGAIN, _, _) -> ()

>
> I always got EAGAIN error and no data available. When using
> Unix.in_channel_of_descr, I can retrieve some data but asynchronously.
>
> I have tried to launch simultaneously my program and GtkTerm configured with
> the same settings and GtkTerm is able to retrieve the informations sent by the
> motor after completion. The source code of GtkTerm looks very similar to mine.
> I probably do something wrong but I cannot see where is the problem.
>
> Would you have an idea ?
>
> Many thanks in advance for your precious help.
>
>
>
>
>
>
>
>
>


      parent reply	other threads:[~2014-08-13 11:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-13  7:23 Edouard Evangelisti
2014-08-13 10:39 ` Paolo Donadeo
2014-08-13 11:22 ` Anders Fugmann [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=53EB4A7E.5080903@fugmann.net \
    --to=anders@fugmann.net \
    --cc=caml-list@inria.fr \
    --cc=edouardevangelisti@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).