comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: GNAT.Serial_Communication and Streams
Date: Sun, 22 Nov 2015 21:52:02 +0000
Date: 2015-11-22T21:52:02+00:00	[thread overview]
Message-ID: <lyoaelg0sd.fsf@pushface.org> (raw)
In-Reply-To: d6318f11-df2c-49bd-928c-2abb766f9c9a@googlegroups.com

rrr.eee.27@gmail.com writes:

> I want to send text strings via a serial line (actually, it is a USB
> device that presents itself as a serial CDC device).
>
> For sending text snippets I had to declare the following routine:
>
>    procedure Send_Command (Cmd : String)
>    is
>       Output_Max_Length : constant Ada.Streams.Stream_Element_Offset := 50;
>       Output : Ada.Streams.Stream_Element_Array (1 .. Output_Max_Length);
>    begin
>       for I in 1 .. Cmd'Length loop
>          Output (Ada.Streams.Stream_Element_Offset(I)) := Character'Pos(Cmd (Cmd'First + I - 1));
>       end loop;
>       Output (Cmd'Length+1) := Character'Pos(ASCII.LF);
>       GNAT.Serial_Communication.Write (P, Output(1..Cmd'Length+1));
>    end Send_Command;
>
>
> That works but feels completely strange to me. I'm sure that I'm
> missing something here. There must be an easier way to fill the output
> buffer with a standard string.

The clue is that

   type Serial_Port is new Ada.Streams.Root_Stream_Type with private;

which means that you aren't supposed to use the local Read/Write; they
are for the implementation of the 'Read/'Write type attributes (ARM
13.3.2),

   String'Write (P'Access, Cmd & ASCII.LF);

BTW, you should probably have declared

   Output : Ada.Streams.Stream_Element_Array (1 .. Cmd'Length + 1);

avoiding the need for Output_Max_Length.

  reply	other threads:[~2015-11-22 21:52 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-22 21:40 GNAT.Serial_Communication and Streams rrr.eee.27
2015-11-22 21:52 ` Simon Wright [this message]
2015-11-22 21:54 ` Jeffrey R. Carter
2015-11-24  1:29   ` Randy Brukardt
2015-11-24 16:09     ` Jeffrey R. Carter
2015-11-24  8:28   ` Dmitry A. Kazakov
2015-11-24 10:28     ` Simon Wright
2015-11-24 10:45       ` Dmitry A. Kazakov
2015-11-25  6:24         ` Shark8
2015-11-25  8:07           ` Simon Wright
2015-11-25  8:52           ` Dmitry A. Kazakov
2015-11-25 12:45             ` Shark8
2015-11-25 17:43               ` Dmitry A. Kazakov
2015-11-29  8:45                 ` Shark8
2015-11-29  9:33                   ` Dmitry A. Kazakov
2015-11-29 11:34                     ` Simon Wright
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox