111
|
1 ------------------------------------------------------------------------------
|
|
2 -- --
|
|
3 -- GNAT COMPILER COMPONENTS --
|
|
4 -- --
|
|
5 -- G N A T . S E R I A L _ C O M M U N I C A T I O N S --
|
|
6 -- --
|
|
7 -- S p e c --
|
|
8 -- --
|
131
|
9 -- Copyright (C) 2007-2018, AdaCore --
|
111
|
10 -- --
|
|
11 -- GNAT is free software; you can redistribute it and/or modify it under --
|
|
12 -- terms of the GNU General Public License as published by the Free Soft- --
|
|
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
|
|
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
|
|
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
|
|
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
|
|
17 -- --
|
|
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
|
|
19 -- additional permissions described in the GCC Runtime Library Exception, --
|
|
20 -- version 3.1, as published by the Free Software Foundation. --
|
|
21 -- --
|
|
22 -- You should have received a copy of the GNU General Public License and --
|
|
23 -- a copy of the GCC Runtime Library Exception along with this program; --
|
|
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
|
|
25 -- <http://www.gnu.org/licenses/>. --
|
|
26 -- --
|
|
27 -- GNAT was originally developed by the GNAT team at New York University. --
|
|
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
|
|
29 -- --
|
|
30 ------------------------------------------------------------------------------
|
|
31
|
|
32 -- Serial communications package, implemented on Windows and GNU/Linux
|
|
33
|
|
34 with Ada.Streams;
|
|
35 with Interfaces.C;
|
|
36
|
|
37 package GNAT.Serial_Communications is
|
|
38
|
|
39 -- Following is a simple example of using GNAT.Serial_Communications.
|
|
40 --
|
|
41 -- with Ada.Streams;
|
|
42 -- with GNAT.Serial_Communications;
|
|
43 --
|
|
44 -- procedure Serial is
|
|
45 -- use Ada.Streams;
|
|
46 -- use GNAT;
|
|
47 --
|
|
48 -- subtype Message is Stream_Element_Array (1 .. 20);
|
|
49 --
|
|
50 -- Data : constant String (1 .. 20) := "ABCDEFGHIJLKMNOPQRST";
|
|
51 -- Buffer : Message;
|
|
52 --
|
|
53 -- S_Port : constant Natural := 5;
|
|
54 -- -- Serial port number
|
|
55 --
|
|
56 -- begin
|
|
57 -- -- Convert message (String -> Stream_Element_Array)
|
|
58 --
|
|
59 -- for K in Data'Range loop
|
|
60 -- Buffer (Stream_Element_Offset (K)) := Character'Pos (Data (K));
|
|
61 -- end loop;
|
|
62 --
|
|
63 -- declare
|
|
64 -- Port_Name : constant Serial_Communications.Port_Name :=
|
|
65 -- Serial_Communications.Name (Number => S_Port);
|
|
66 -- Port : Serial_Communications.Serial_Port;
|
|
67 --
|
|
68 -- begin
|
|
69 -- Serial_Communications.Open
|
|
70 -- (Port => Port,
|
|
71 -- Name => Port_Name);
|
|
72 --
|
|
73 -- Serial_Communications.Set
|
|
74 -- (Port => Port,
|
|
75 -- Rate => Serial_Communications.B9600,
|
|
76 -- Bits => Serial_Communications.CS8,
|
|
77 -- Stop_Bits => Serial_Communications.One,
|
|
78 -- Parity => Serial_Communications.Even);
|
|
79 --
|
|
80 -- Serial_Communications.Write
|
|
81 -- (Port => Port,
|
|
82 -- Buffer => Buffer);
|
|
83 --
|
|
84 -- Serial_Communications.Close
|
|
85 -- (Port => Port);
|
|
86 -- end;
|
|
87 -- end Serial;
|
|
88
|
|
89 Serial_Error : exception;
|
|
90 -- Raised when a communication problem occurs
|
|
91
|
|
92 type Port_Name is new String;
|
|
93 -- A serial com port name
|
|
94
|
|
95 function Name (Number : Positive) return Port_Name;
|
|
96 -- Returns a possible port name for the given legacy PC architecture serial
|
|
97 -- port number (COM<number>: on Windows, ttyS<number-1> on Linux).
|
|
98 -- Note that this function does not support other kinds of serial ports
|
|
99 -- nor operating systems other than Windows and Linux. For all other
|
|
100 -- cases, an explicit port name can be passed directly to Open.
|
|
101
|
|
102 type Data_Rate is
|
|
103 (B75, B110, B150, B300, B600, B1200, B2400, B4800, B9600,
|
|
104 B19200, B38400, B57600, B115200);
|
|
105 -- Speed of the communication
|
|
106
|
|
107 type Data_Bits is (CS8, CS7);
|
|
108 -- Communication bits
|
|
109
|
|
110 type Stop_Bits_Number is (One, Two);
|
|
111 -- One or two stop bits
|
|
112
|
|
113 type Parity_Check is (None, Even, Odd);
|
|
114 -- Either no parity check or an even or odd parity
|
|
115
|
|
116 type Flow_Control is (None, RTS_CTS, Xon_Xoff);
|
|
117 -- No flow control, hardware flow control, software flow control
|
|
118
|
|
119 type Serial_Port is new Ada.Streams.Root_Stream_Type with private;
|
|
120
|
|
121 procedure Open
|
|
122 (Port : out Serial_Port;
|
|
123 Name : Port_Name);
|
|
124 -- Open the given port name. Raises Serial_Error if the port cannot be
|
|
125 -- opened.
|
|
126
|
|
127 procedure Set
|
|
128 (Port : Serial_Port;
|
|
129 Rate : Data_Rate := B9600;
|
|
130 Bits : Data_Bits := CS8;
|
|
131 Stop_Bits : Stop_Bits_Number := One;
|
|
132 Parity : Parity_Check := None;
|
|
133 Block : Boolean := True;
|
|
134 Local : Boolean := True;
|
|
135 Flow : Flow_Control := None;
|
|
136 Timeout : Duration := 10.0);
|
|
137 -- The communication port settings. If Block is set then a read call
|
|
138 -- will wait for the whole buffer to be filed. If Block is not set then
|
|
139 -- the given Timeout (in seconds) is used. If Local is set then modem
|
|
140 -- control lines (in particular DCD) are ignored (not supported on
|
|
141 -- Windows). Flow indicates the flow control type as defined above.
|
|
142
|
|
143 -- Note: the timeout precision may be limited on some implementation
|
|
144 -- (e.g. on GNU/Linux the maximum precision is a tenth of seconds).
|
|
145
|
|
146 -- Note: calling this procedure may reinitialize the serial port hardware
|
|
147 -- and thus cause loss of some buffered data if used during communication.
|
|
148
|
|
149 overriding procedure Read
|
|
150 (Port : in out Serial_Port;
|
|
151 Buffer : out Ada.Streams.Stream_Element_Array;
|
|
152 Last : out Ada.Streams.Stream_Element_Offset);
|
|
153 -- Read a set of bytes, put result into Buffer and set Last accordingly.
|
|
154 -- Last is set to Buffer'First - 1 if no byte has been read, unless
|
|
155 -- Buffer'First = Stream_Element_Offset'First, in which case the exception
|
|
156 -- Constraint_Error is raised instead.
|
|
157
|
|
158 overriding procedure Write
|
|
159 (Port : in out Serial_Port;
|
|
160 Buffer : Ada.Streams.Stream_Element_Array);
|
|
161 -- Write buffer into the port
|
|
162
|
|
163 procedure Close (Port : in out Serial_Port);
|
|
164 -- Close port
|
|
165
|
|
166 private
|
|
167
|
|
168 type Port_Data;
|
|
169 type Port_Data_Access is access Port_Data;
|
|
170
|
|
171 type Serial_Port is new Ada.Streams.Root_Stream_Type with record
|
|
172 H : Port_Data_Access;
|
|
173 end record;
|
|
174
|
|
175 Data_Rate_Value : constant array (Data_Rate) of Interfaces.C.unsigned :=
|
|
176 (B75 => 75,
|
|
177 B110 => 110,
|
|
178 B150 => 150,
|
|
179 B300 => 300,
|
|
180 B600 => 600,
|
|
181 B1200 => 1_200,
|
|
182 B2400 => 2_400,
|
|
183 B4800 => 4_800,
|
|
184 B9600 => 9_600,
|
|
185 B19200 => 19_200,
|
|
186 B38400 => 38_400,
|
|
187 B57600 => 57_600,
|
|
188 B115200 => 115_200);
|
|
189
|
|
190 end GNAT.Serial_Communications;
|