|
1 | 1 | ------------------------------------------------------------------------------
|
2 | 2 | -- --
|
3 |
| --- Copyright (C) 2016, AdaCore -- |
| 3 | +-- Copyright (C) 2016-2025, AdaCore -- |
4 | 4 | -- --
|
5 | 5 | -- Redistribution and use in source and binary forms, with or without --
|
6 | 6 | -- modification, are permitted provided that the following conditions are --
|
|
30 | 30 | ------------------------------------------------------------------------------
|
31 | 31 |
|
32 | 32 | -- This file contains the source code for an interactive program to be run on
|
33 |
| --- a host computer, to communicate via serial port with a program running on |
34 |
| --- a target board. It uses streams to send and receive strings to/from the |
35 |
| --- target board program. |
| 33 | +-- a host computer. It communicates via a host serial port with the streaming |
| 34 | +-- demonstration program running on a target board. It uses streams to send |
| 35 | +-- and receive strings to/from the target board program for the sake of |
| 36 | +-- compatible handling of the bounds. This host program is only intended |
| 37 | +-- for the streaming demonstration, not for the other target demonstration |
| 38 | +-- programs. |
36 | 39 |
|
37 |
| --- The program can be connected to the target board with a serial cable that |
38 |
| --- presents a serial port to the host operating system. The "README.md" file |
39 |
| --- associated with this project describes such a cable. |
| 40 | +-- This program is to be built with a native machine compiler, not a |
| 41 | +-- cross-compiler. The program is known to work on Windows and should work |
| 42 | +-- on Linux as well, according to the comments in package GNAT.Communications. |
40 | 43 |
|
41 |
| --- You can change the COM port number, or even get it from the command line |
42 |
| --- as an argument, but it must match what the host OS sees from the USB-COM |
43 |
| --- cable. This program has been run successfully on Windows 7 but should run |
44 |
| --- on Linux as well. |
| 44 | +-- The host program requires a serial port to communicate with the serial port |
| 45 | +-- on the target board, so a cable is required. One way to do that is to use |
| 46 | +-- a special cable that makes a host USB port function like a serial port. The |
| 47 | +-- "README.md" file associated with this project describes such a cable, which |
| 48 | +-- was used in testing. |
45 | 49 |
|
46 |
| --- When running it, enter a string at the prompt (">") or just hit carriage |
| 50 | +-- On the command line invocation you must specify the host serial port number |
| 51 | +-- as an integer number (in any base if you use Ada syntax). That number must |
| 52 | +-- correspond to the host serial port connected to the target board. No other |
| 53 | +-- command line parameters are accepted. |
| 54 | + |
| 55 | +-- During execution, enter a string at the prompt (">") or just hit carriage |
47 | 56 | -- return if you are ready to quit. If you do enter a string, it will be sent
|
48 | 57 | -- to the target board, along with the bounds. The program running on the
|
49 | 58 | -- target echos it back so this host app will show that response from the
|
50 | 59 | -- board.
|
| 60 | +-- |
| 61 | +-- Note that the baud rate for the host serial port is set below, and although |
| 62 | +-- arbitrary, must match the value set by the streaming demo program on the |
| 63 | +-- target board. That could be an additional host argument in the future. The |
| 64 | +-- target serial port is set to eight data bits, no parity, and one stop bit. |
| 65 | +-- Those settings are typically what a host serial port uses for defaults, but |
| 66 | +-- not necessarily. |
51 | 67 |
|
52 | 68 | with GNAT.IO; use GNAT.IO;
|
53 | 69 | with GNAT.Serial_Communications; use GNAT.Serial_Communications;
|
| 70 | +with Ada.Command_Line; |
54 | 71 |
|
55 | 72 | procedure Host is
|
56 |
| - COM : aliased Serial_Port; |
57 |
| - COM3 : constant Port_Name := Name (3); |
| 73 | + |
| 74 | + COM : aliased Serial_Port; |
| 75 | + Selected_Port : Integer; |
| 76 | + Valid_Selection : Boolean; |
58 | 77 |
|
59 | 78 | Outgoing : String (1 .. 1024); -- arbitrary
|
60 |
| - Last : Natural; |
| 79 | + Last : Natural range Outgoing'First - 1 .. Outgoing'Last; |
| 80 | + |
| 81 | + procedure Get_Port_Number |
| 82 | + (Selected_Port : out Integer; |
| 83 | + Valid : out Boolean); |
| 84 | + -- Get the port number from the command line arguments (only one is |
| 85 | + -- expected), ensuring that the argument is a well-formed integer with |
| 86 | + -- the required range. |
| 87 | + -- |
| 88 | + -- Note that it does not check whether a valid argument corresponds to an |
| 89 | + -- existing host serial port. If it does not, GNAT.Serial_Communications |
| 90 | + -- will raise Serial_Error when Open is called. |
| 91 | + |
| 92 | + --------------------- |
| 93 | + -- Get_Port_Number -- |
| 94 | + --------------------- |
| 95 | + |
| 96 | + procedure Get_Port_Number |
| 97 | + (Selected_Port : out Integer; |
| 98 | + Valid : out Boolean) |
| 99 | + is |
| 100 | + use Ada.Command_Line; |
| 101 | + begin |
| 102 | + Valid := False; |
| 103 | + Selected_Port := 0; -- won't be used unless the caller ignores Valid |
| 104 | + |
| 105 | + if Argument_Count /= 1 then |
| 106 | + Put_Line ("You must specify (only) the number of the COM port to open on this host."); |
| 107 | + Put_Line ("For example, to specify COM3 the invocation would be:"); |
| 108 | + Put_Line (" host 3"); |
| 109 | + Put_Line ("The following Windows PowerShell command lists all existing ports:"); |
| 110 | + Put_Line (" [System.IO.Ports.SerialPort]::GetPortNames()"); |
| 111 | + return; |
| 112 | + end if; |
| 113 | + |
| 114 | + Well_Formed : begin |
| 115 | + Selected_Port := Integer'Value (Argument (1)); |
| 116 | + exception |
| 117 | + when others => |
| 118 | + Put_Line ("You must specify a syntactically valid (positive) integer value for the host COM port."); |
| 119 | + return; |
| 120 | + end Well_Formed; |
| 121 | + |
| 122 | + if Selected_Port < 1 then |
| 123 | + Put_Line ("You must specify a positive number for the host COM port."); |
| 124 | + -- Because function Name from package GNAT.Serial_Communications |
| 125 | + -- requires a positive value. |
| 126 | + return; |
| 127 | + end if; |
| 128 | + |
| 129 | + Valid := True; |
| 130 | + end Get_Port_Number; |
| 131 | + |
61 | 132 | begin
|
62 |
| - COM.Open (COM3); |
| 133 | + Get_Port_Number (Selected_Port, Valid_Selection); |
| 134 | + if not Valid_Selection then |
| 135 | + return; |
| 136 | + end if; |
| 137 | + |
| 138 | + COM.Open (Name (Selected_Port)); |
63 | 139 | COM.Set (Rate => B115200, Block => False);
|
| 140 | + -- The baud rate is arbitrary but must match the selection by the target |
| 141 | + -- board's stream demonstration program. The other target serial port |
| 142 | + -- settings are N81. That stop bit is likley critical to proper function |
| 143 | + -- of the demo. |
64 | 144 |
|
65 | 145 | loop
|
66 | 146 | Put ("> ");
|
|
74 | 154 | declare
|
75 | 155 | Incoming : constant String := String'Input (COM'Access);
|
76 | 156 | begin
|
77 |
| - Put_Line ("From board: " & Incoming); |
| 157 | + Put_Line ("Received from board: " & Incoming); |
78 | 158 | end;
|
79 | 159 | end loop;
|
80 | 160 |
|
|
0 commit comments