caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Edouard Evangelisti" <edouardevangelisti@gmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] Unix library and serial port
Date: Wed, 13 Aug 2014 09:23:09 +0200	[thread overview]
Message-ID: <sympa.1407913958.2059.874@inria.fr> (raw)

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.









             reply	other threads:[~2014-08-13  7:23 UTC|newest]

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

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=sympa.1407913958.2059.874@inria.fr \
    --to=edouardevangelisti@gmail.com \
    --cc=caml-list@inria.fr \
    /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).