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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id D087F7F75C for ; Wed, 13 Aug 2014 12:40:36 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of p.donadeo@gmail.com) identity=pra; client-ip=209.85.218.45; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="p.donadeo@gmail.com"; x-sender="p.donadeo@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of p.donadeo@gmail.com designates 209.85.218.45 as permitted sender) identity=mailfrom; client-ip=209.85.218.45; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="p.donadeo@gmail.com"; x-sender="p.donadeo@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-oi0-f45.google.com) identity=helo; client-ip=209.85.218.45; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="p.donadeo@gmail.com"; x-sender="postmaster@mail-oi0-f45.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Am0BAOc/61PRVdotlGdsb2JhbABahDYEgnayap8OAYELCBYQAQEBAQcLCwkSK4QEAQEEEhEEGQEbHQEDDAYDAgs3AgIiAREBBQEcBhsaiAsBAxGUBpAhaospgXKDEIoXChknDWaEXREBBQ6PPgeCeYFTAQSPCoMOiiGTDBgphQ87Lw X-IPAS-Result: Am0BAOc/61PRVdotlGdsb2JhbABahDYEgnayap8OAYELCBYQAQEBAQcLCwkSK4QEAQEEEhEEGQEbHQEDDAYDAgs3AgIiAREBBQEcBhsaiAsBAxGUBpAhaospgXKDEIoXChknDWaEXREBBQ6PPgeCeYFTAQSPCoMOiiGTDBgphQ87Lw X-IronPort-AV: E=Sophos;i="5.01,856,1400018400"; d="scan'208";a="74696770" Received: from mail-oi0-f45.google.com ([209.85.218.45]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 13 Aug 2014 12:40:35 +0200 Received: by mail-oi0-f45.google.com with SMTP id e131so7546232oig.32 for ; Wed, 13 Aug 2014 03:40:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=KwiFsCE2Ublz+u9U8gHUAohT5+k6cSRo1ED5EdDS0NE=; b=U2nj40N+zXhXm0nevQA1dqls26yOE4qcTEPHxBCilhhalFIX34+kH3gZwU+HpsWV1u 916Qz5uFMzCgw5qvapMlWcZbd3C0JS3RkNGkClXC86J1rgrJipyToq7j55sQzE0vfm0t 42yXsypoSnDykRQjHJjK0X3VrPZt6PkQkp8NhGi86d0pdRdGG3G9i2JW9VTEKKibLoYh AxiiDMviJJZnwY5jnsszyBXJgku+3owM6ItMeZe3KuMsfif9DrJwNV6XgN5G/aMmq53R GXgx3Nmsl7X023p/b93gXOPxkjnNgspNsg2FQpAAl6C9HKe3bg9MgcykK6pFsDfG35sI NTlg== X-Received: by 10.182.129.163 with SMTP id nx3mr3721817obb.7.1407926434433; Wed, 13 Aug 2014 03:40:34 -0700 (PDT) MIME-Version: 1.0 Received: by 10.76.4.79 with HTTP; Wed, 13 Aug 2014 03:39:53 -0700 (PDT) In-Reply-To: References: From: Paolo Donadeo Date: Wed, 13 Aug 2014 12:39:53 +0200 Message-ID: To: Edouard Evangelisti Cc: OCaml mailing list Content-Type: multipart/alternative; boundary=089e0115ea2efe91a80500806a5e Subject: Re: [Caml-list] Unix library and serial port --089e0115ea2efe91a80500806a5e Content-Type: text/plain; charset=UTF-8 I have a similar task, read/write to a MODBUS fieldbus (RS485). I can successfully read with this function: *let rec really_read ?(timeout = 0.5) fd buffer start length =* * let open Unix in* * if length <= 0 then () else begin* * match Unix.select [ fd ] [] [] timeout with* * | [], [], [] -> raise (Timeout timeout)* * | fdl, _, _ -> begin* * match read fd buffer start length with* * | 0 -> raise End_of_file* * | r -> really_read fd buffer (start + r) (length - r)* * end* * end* The code in which I use *really_read* looks like this: * Unix.write ctx.descriptor packet 0 packet_len |> ignore;* * let buffer = String.make 2048 (Char.chr 0) in* * let timeout = 1.0 in* * let expected_bytes = 13 in* * really_read ~timeout ctx.descriptor buffer 0 expected_bytes;* Before reading/writing I set a bunch of terminal variables: *let modbus_connect device_name baud csize parity cstopb =* * let open Unix in* * let descriptor = openfile device_name [ O_RDWR; O_NOCTTY; O_NONBLOCK; O_EXCL; O_CLOEXEC; ] 0o660 in* * let old_terminal_settings = tcgetattr descriptor in* * let slave_id = None in* * let ctx = { device_name; baud; parity; csize; cstopb; descriptor; old_terminal_settings; slave_id; } in* * let term_settings = tcgetattr descriptor in* * term_settings.c_obaud <- baud;* * term_settings.c_ibaud <- baud;* * term_settings.c_cread <- true;* * term_settings.c_clocal <- true;* * term_settings.c_csize <- csize;* * term_settings.c_cstopb <- cstopb;* * let () = match parity with* * | Parity_None -> begin* * term_settings.c_inpck <- false;* * term_settings.c_parenb <- false;* * term_settings.c_parodd <- false;* * end* * | Parity_Even -> begin* * term_settings.c_inpck <- true;* * term_settings.c_parenb <- true;* * term_settings.c_parodd <- false;* * end* * | Parity_Odd -> begin* * term_settings.c_inpck <- true;* * term_settings.c_parenb <- true;* * term_settings.c_parodd <- true;* * end in* * term_settings.c_icanon <- false;* * term_settings.c_echo <- false;* * term_settings.c_echoe <- false;* * term_settings.c_echok <- false;* * term_settings.c_echonl <- false;* * term_settings.c_isig <- false;* * term_settings.c_ixon <- false;* * term_settings.c_ixoff <- false;* * term_settings.c_opost <- false;* * term_settings.c_vmin <- 0;* * term_settings.c_vtime <- 0;* * let () = tcsetattr descriptor TCSANOW term_settings in* * ctx* *ctx* is a type in which I keep the context for the connection. Hope this may help. -- *Paolo* --089e0115ea2efe91a80500806a5e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I have a similar task, read/write to a MODBUS fieldbu= s (RS485).

I can successfully read with this funct= ion:

let rec really_read ?(timeout =3D 0.5) fd buffer start length =3D
=C2=A0 let open Unix in
=C2=A0 if length &= lt;=3D 0 then () else begin
=C2=A0 =C2=A0 match Unix.select [ fd ] [] [] timeout with
=C2=A0 =C2=A0 | [], [], [] -&= gt; raise (Timeout timeout)
=C2=A0 =C2=A0 | fdl, _, _ -> begin
<= b>=C2=A0 =C2=A0 =C2=A0 =C2=A0 match r= ead fd buffer start length with
=C2=A0 =C2=A0 =C2=A0 =C2=A0 |= 0 -> raise End_of_file
=C2=A0 =C2=A0 =C2=A0 =C2=A0 | r -> really_read fd buffer (= start + r) (length - r)
=C2=A0 =C2=A0 =C2=A0 end
=C2=A0 end=

The code in which I use really_read looks like this:

=C2=A0 Un= ix.write ctx.descriptor packet 0 packet_len |> ignore;
<= div>=C2=A0 let buffer =3D String.m= ake 2048 (Char.chr 0) in
=C2=A0 let timeout =3D 1.0 in=
=C2=A0 let e= xpected_bytes =3D 13 in
=C2=A0 really_read ~timeout ctx.descriptor buffer 0 expected_byt= es;

Before reading/writing I set a bunch of terminal = variables:

let modbus_connect device_name baud csize parity cstopb =3D
=C2=A0 let open Unix in
=C2=A0 let descrip= tor =3D openfile device_name [ O_RDWR; O_NOCTTY; O_NONBLOCK; O_EXCL; O_CLOE= XEC; ] 0o660 in
=C2=A0 let old_terminal_setti= ngs =3D tcgetattr descriptor in
=C2=A0 let slave_id =3D None in
= =C2=A0 let ctx =3D { device_name; bau= d; parity; csize; cstopb; descriptor; old_terminal_settings; slave_id; } in=

= =C2=A0 let term_settings =3D tcgetatt= r descriptor in
=C2=A0 term_settings.c_obaud <- baud;
=C2=A0 term_settings.c_ibaud = <- baud;
= =C2=A0 term_settings.c_cread <- true;
=C2=A0 term_settings.c_clocal <- true;
=C2=A0 term_settings.c_csize = <- csize;
= =C2=A0 term_settings.c_cstopb <- cstopb;
=C2=A0 let () =3D match parity with
=C2=A0 =C2=A0 | Parity_None -= > begin
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 term_settings.c_inpck <- false;
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 term_settings.c_parenb <- false;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 t= erm_settings.c_parodd <- false;
=C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0 | Parity_Even -> begin
=C2=A0 =C2=A0 =C2=A0 =C2=A0 t= erm_settings.c_inpck <- true;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 term_settings.c_parenb <= - true;
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 term_settings.c_parodd <- false;
=C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0 | P= arity_Odd -> begin
=C2=A0 =C2=A0 =C2=A0 =C2=A0 term_settings.c_inpck <- true;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 t= erm_settings.c_parenb <- true;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 term_settings.c_parodd <= ;- true;
=C2= =A0 =C2=A0 =C2=A0 end in
=C2=A0 term_settings.c_icanon= <- false;
=C2=A0 term_settings.c_echo =C2=A0 <- false;
=C2=A0 term_settings.c_echoe =C2=A0<= - false;
=C2=A0 term_settings.c_echok = =C2=A0<- false;
=C2=A0 term_settings.c_echonl <- false;
=C2=A0 term_settings.c_isig =C2=A0 <= - false;
=C2=A0 term_settings.c_ixon = =C2=A0 <- false;
=C2=A0 term_settings.c_ixoff =C2=A0<- false;
=C2=A0 term_settings.c_opost =C2= =A0<- false;
=C2=A0 term_settings.c_vmin = =C2=A0 <- 0;
=C2=A0 term_settings.c_vtime =C2=A0<- 0;

=C2=A0 let (= ) =3D tcsetattr descriptor TCSANOW term_settings in
=C2=A0 ctx

ctx is a type in w= hich I keep the context for the connection.

Hope t= his may help.


--
Paolo
--089e0115ea2efe91a80500806a5e--