From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: GNAT.Serial_Communication and Streams Date: Sun, 22 Nov 2015 21:52:02 +0000 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="fa1ef3748c46c80a9a627d7896b0bfd1"; logging-data="12890"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+kDrKygWLuODvkjGuvMLPwJeoHdDx32pU=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (darwin) Cancel-Lock: sha1:D88S+jgDeCq4pq11G9psdluea00= sha1:VrXTxBSl2h9ocOHmmklJhnHKnxA= Xref: news.eternal-september.org comp.lang.ada:28496 Date: 2015-11-22T21:52:02+00:00 List-Id: 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.