1 |
281 |
jeremybenn |
------------------------------------------------------------------------------
|
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 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 2007-2009, AdaCore --
|
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 2, 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. See the GNU General Public License --
|
17 |
|
|
-- for more details. You should have received a copy of the GNU General --
|
18 |
|
|
-- Public License distributed with GNAT; see file COPYING. If not, write --
|
19 |
|
|
-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
|
20 |
|
|
-- Boston, MA 02110-1301, USA. --
|
21 |
|
|
-- --
|
22 |
|
|
-- As a special exception, if other files instantiate generics from this --
|
23 |
|
|
-- unit, or you link this unit with other files to produce an executable, --
|
24 |
|
|
-- this unit does not by itself cause the resulting executable to be --
|
25 |
|
|
-- covered by the GNU General Public License. This exception does not --
|
26 |
|
|
-- however invalidate any other reasons why the executable file might be --
|
27 |
|
|
-- covered by the GNU Public License. --
|
28 |
|
|
-- --
|
29 |
|
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
30 |
|
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
31 |
|
|
-- --
|
32 |
|
|
------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
-- Serial communications package, implemented on Windows and GNU/Linux
|
35 |
|
|
|
36 |
|
|
with Ada.Streams;
|
37 |
|
|
with Interfaces.C;
|
38 |
|
|
|
39 |
|
|
package GNAT.Serial_Communications is
|
40 |
|
|
|
41 |
|
|
Serial_Error : exception;
|
42 |
|
|
-- Raised when a communication problem occurs
|
43 |
|
|
|
44 |
|
|
type Port_Name is new String;
|
45 |
|
|
-- A serial com port name
|
46 |
|
|
|
47 |
|
|
function Name (Number : Positive) return Port_Name;
|
48 |
|
|
-- Returns a possible port name for the given legacy PC architecture serial
|
49 |
|
|
-- port number (COM<number>: on Windows, ttyS<number-1> on Linux).
|
50 |
|
|
-- Note that this function does not support other kinds of serial ports
|
51 |
|
|
-- nor operating systems other than Windows and Linux. For all other
|
52 |
|
|
-- cases, an explicit port name can be passed directly to Open.
|
53 |
|
|
|
54 |
|
|
type Data_Rate is
|
55 |
|
|
(B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200);
|
56 |
|
|
-- Speed of the communication
|
57 |
|
|
|
58 |
|
|
type Data_Bits is (CS8, CS7);
|
59 |
|
|
-- Communication bits
|
60 |
|
|
|
61 |
|
|
type Stop_Bits_Number is (One, Two);
|
62 |
|
|
-- One or two stop bits
|
63 |
|
|
|
64 |
|
|
type Parity_Check is (None, Even, Odd);
|
65 |
|
|
-- Either no parity check or an even or odd parity
|
66 |
|
|
|
67 |
|
|
type Serial_Port is new Ada.Streams.Root_Stream_Type with private;
|
68 |
|
|
|
69 |
|
|
procedure Open
|
70 |
|
|
(Port : out Serial_Port;
|
71 |
|
|
Name : Port_Name);
|
72 |
|
|
-- Open the given port name. Raises Serial_Error if the port cannot be
|
73 |
|
|
-- opened.
|
74 |
|
|
|
75 |
|
|
procedure Set
|
76 |
|
|
(Port : Serial_Port;
|
77 |
|
|
Rate : Data_Rate := B9600;
|
78 |
|
|
Bits : Data_Bits := CS8;
|
79 |
|
|
Stop_Bits : Stop_Bits_Number := One;
|
80 |
|
|
Parity : Parity_Check := None;
|
81 |
|
|
Block : Boolean := True;
|
82 |
|
|
Timeout : Duration := 10.0);
|
83 |
|
|
-- The communication port settings. If Block is set then a read call
|
84 |
|
|
-- will wait for the whole buffer to be filed. If Block is not set then
|
85 |
|
|
-- the given Timeout (in seconds) is used. Note that the timeout precision
|
86 |
|
|
-- may be limited on some implementation (e.g. on GNU/Linux the maximum
|
87 |
|
|
-- precision is a tenth of seconds).
|
88 |
|
|
|
89 |
|
|
overriding procedure Read
|
90 |
|
|
(Port : in out Serial_Port;
|
91 |
|
|
Buffer : out Ada.Streams.Stream_Element_Array;
|
92 |
|
|
Last : out Ada.Streams.Stream_Element_Offset);
|
93 |
|
|
-- Read a set of bytes, put result into Buffer and set Last accordingly.
|
94 |
|
|
-- Last is set to Buffer'First - 1 if no byte has been read, unless
|
95 |
|
|
-- Buffer'First = Stream_Element_Offset'First, in which case the exception
|
96 |
|
|
-- Constraint_Error is raised instead.
|
97 |
|
|
|
98 |
|
|
overriding procedure Write
|
99 |
|
|
(Port : in out Serial_Port;
|
100 |
|
|
Buffer : Ada.Streams.Stream_Element_Array);
|
101 |
|
|
-- Write buffer into the port
|
102 |
|
|
|
103 |
|
|
procedure Close (Port : in out Serial_Port);
|
104 |
|
|
-- Close port
|
105 |
|
|
|
106 |
|
|
private
|
107 |
|
|
|
108 |
|
|
type Port_Data;
|
109 |
|
|
type Port_Data_Access is access Port_Data;
|
110 |
|
|
|
111 |
|
|
type Serial_Port is new Ada.Streams.Root_Stream_Type with record
|
112 |
|
|
H : Port_Data_Access;
|
113 |
|
|
end record;
|
114 |
|
|
|
115 |
|
|
Data_Rate_Value : constant array (Data_Rate) of Interfaces.C.unsigned :=
|
116 |
|
|
(B1200 => 1_200,
|
117 |
|
|
B2400 => 2_400,
|
118 |
|
|
B4800 => 4_800,
|
119 |
|
|
B9600 => 9_600,
|
120 |
|
|
B19200 => 19_200,
|
121 |
|
|
B38400 => 38_400,
|
122 |
|
|
B57600 => 57_600,
|
123 |
|
|
B115200 => 115_200);
|
124 |
|
|
|
125 |
|
|
end GNAT.Serial_Communications;
|