From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id F35CD7F75C for ; Wed, 13 Aug 2014 09:23:11 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.01,855,1400018400"; d="scan'208";a="89168742" Received: from sympa.inria.fr ([193.51.193.213]) by mail2-relais-roc.national.inria.fr with ESMTP; 13 Aug 2014 09:23:11 +0200 Received: by sympa.inria.fr (Postfix, from userid 20132) id E51627F79D; Wed, 13 Aug 2014 09:23:11 +0200 (CEST) Date: Wed, 13 Aug 2014 09:23:09 +0200 To: caml-list@inria.fr Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7BIT From: "Edouard Evangelisti" X-Mailer: Sympa 6.1.17 Subject: [Caml-list] Unix library and serial port 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.