Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Serial port configuration

94 views
Skip to first unread message

hreba

unread,
Apr 6, 2014, 12:22:20 PM4/6/14
to
The problem
-----------
I am trying to communicate with an Arduino using
GNAT.Serial_Communications, which I will abbreviate as "Serial". The
"Set" procedure isn't detailed enough, I need "fcntl", "tcgetattr" and
"tcsetattr" which are private in Serial. To call them, I need the file
descriptor which is a private component of the type "Port".

The Idea
--------
1. Create a new package "SerAux"
2. declare "fcntl", "tcgetattr" and "tcsetattr" the same way as in
"Serial", but public
3. define a new type "Port" the same way as in "Serial", but public,
and do the conversion somehow.

The obstacles
-------------
Creating the new Port requires to override its methods "Read" and
"Write", but even doing that the same way as in "Serial" gets me a lot
of error messages. The SerAux.adb begins like this (reproduced from
original line # 4 on):

--------------------------------------------------------------
...
package body SerAux is

type Port_Data is new int;
type Port_Data_Access is access Port_Data;
type Port is new Ada.Streams.Root_Stream_Type with record
H : Port_Data_Access;
end record;


overriding procedure Read
(--Port : in out Serial_Port;
Port : in out Port;
Buffer : out Ada.Streams.Stream_Element_Array;
Last : out Ada.Streams.Stream_Element_Offset)
is
begin
Read (Serial.Serial_Port(Port), Buffer, Last);
end Read;

...
----------------------------------------------------------------

The error messages for this fragment are:

seraux.adb:8:09: type must be declared abstract or "Read" overridden
seraux.adb:8:09: "Read" has been inherited at line 8
seraux.adb:8:09: "Read" has been inherited from subprogram at
a-stream.ads:54
seraux.adb:13:15: subprogram "Read" is not overriding
seraux.adb:15:23: formal parameter "Port" cannot be used before end of
specification
seraux.adb:20:07: warning: possible infinite recursion
seraux.adb:20:07: warning: Storage_Error may be raised at run time
...
-----------------------------------------------------------------

The questions
-------------
1. Why is all that an error here and not in "Serial"?
2. Am I on the right way or would you solve the problem completely
different?

Please consider that I am a beginner.

--
hreba

Simon Wright

unread,
Apr 6, 2014, 2:57:04 PM4/6/14
to
hreba <hr...@terra.com.br> writes:

> package body SerAux is
>
> type Port_Data is new int;
> type Port_Data_Access is access Port_Data;
> type Port is new Ada.Streams.Root_Stream_Type with record
> H : Port_Data_Access;
> end record;

The declaration of type Port needs to be in the spec; and you need to
have the spec of Read there also.

> overriding procedure Read
> (--Port : in out Serial_Port;
> Port : in out Port;

You can't use the name Port twice like that. I'd suggest using
Serial_Port for the type name, like the original (why change?).

In fact, why not just copy the contents of the GNAT version and change
the bits you need to?

Shark8

unread,
Apr 6, 2014, 3:06:07 PM4/6/14
to
On 06-Apr-14 12:57, Simon Wright wrote:
>> > overriding procedure Read
>> > (--Port : in out Serial_Port;
>> > Port : in out Port;
> You can't use the name Port twice like that. I'd suggest using
> Serial_Port for the type name, like the original (why change?).

You can use the same name as the type if you fully qualify it:

Generic
Type Element(<>) is private;
Maximum_Size : Positive:= 1024;
Package Stack_Package is
type Stack is private;

Function Pop( Stack : Stack_Package.Stack ) return Element;

End;

hreba

unread,
Apr 7, 2014, 7:44:58 AM4/7/14
to
On 04/06/2014 03:57 PM, Simon Wright wrote:
> hreba <hr...@terra.com.br> writes:
>
>> package body SerAux is
>>
>> type Port_Data is new int;
>> type Port_Data_Access is access Port_Data;
>> type Port is new Ada.Streams.Root_Stream_Type with record
>> H : Port_Data_Access;
>> end record;
>
> The declaration of type Port needs to be in the spec; and you need to
> have the spec of Read there also.
>
>> overriding procedure Read
>> (--Port : in out Serial_Port;
>> Port : in out Port;
>
> You can't use the name Port twice like that. I'd suggest using
> Serial_Port for the type name, like the original (why change?).

Thanks for the hints.
>
> In fact, why not just copy the contents of the GNAT version and change
> the bits you need to?
>
This might not be the most elegant solution, but the one with the least
effort. I think I'll try that.
--
hreba

hreba

unread,
Apr 7, 2014, 7:47:03 AM4/7/14
to
Ok, thanks.

--
hreba

wvxvw

unread,
Nov 4, 2023, 10:23:32 AM11/4/23
to
Hello.

I'm learning Ada as well as how to use Usenet, so don't be too harsh.
As a learning exercise, I want to write a program that, beside other
things, needs to compute SHA256 hashes. I discovered GNAT.SHA256
library and was able to use it (by calling Digest(<some string>)),
however the result is different from what I'd get for the same string
with running sha256sum.

Eg, with GNAT.SHA256 for string "foo" I get:

❯ ./bin/test_sha --arg foo
2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
❯ echo foo | sha256sum -
b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c -

My Ada code looks (with some unrelated stuff removed) like this:

with GNAT.Command_Line; use GNAT.Command_Line;
with GNAT.SHA256; use GNAT.SHA256;

procedure Main is
loop
case Getopt ("-arg=") is
when '-' =>
if Full_Switch = "-arg" then
Put_Line (Digest (Parameter));
end if;
end case;
end loop;
end Main;

My understanding is that there are plenty of configuration settings to
how the checksum is computed, but I know very little about it. My goal
is to produce the same checksum as would be produced by sha256sum
though, as the checksums are to be used outside of my program.

Finally: is GNAT.SHA256 a good way to go if I need this functionality (I
don't care about portability, if that's a concern)?

Thanks!
0 new messages