-
Notifications
You must be signed in to change notification settings - Fork 17
/
SerialEcho.lpr
146 lines (129 loc) · 6.07 KB
/
SerialEcho.lpr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
program SerialEcho;
{$mode delphi}{$H+}
{ Synapse example - Serial Echo }
{ }
{ An example that reads characters from the serial port and echos them back. }
{ }
{ You will need a serial cable or a USB to serial converter to connect the Pi }
{ to your computer, the Pi uses GPIO 14 (Transmit) and 15 (Receive) as well as }
{ a Ground pin to make the connection. The documentation below shows you where }
{ to find each of the GPIO connections on the physical pins of your Pi. }
{ }
{ Raspberry Pi Models A+/B+/Zero/2B/3B/4B/400 (40 pin header) }
{ https://www.raspberrypi.com/documentation/computers/os.html#gpio-and-the-40-pin-header }
{ }
{ Raspberry Pi Pinout (26 and 40 pin header) }
{ https://pinout.xyz/ }
{ }
{ You will also need a terminal program running on your computer, you can use }
{ something like PuTTY to create a serial connection to the COM port you are }
{ using. For this example we'll use these connection settings: }
{ }
{ Speed: 9600 }
{ Data Bits: 8 }
{ Stop Bits: 1 }
{ Parity: None }
{ Flow Control: None }
{ }
{ To compile the example select Run, Compile (or Run, Build) from the menu. }
{ }
{ Once compiled copy the kernel7.img file to an SD card along with the }
{ firmware files and use it to boot your Raspberry Pi. }
{ }
{ This version is for Raspberry Pi 2B and will also work on a 3B/3B+/3A+/Zero2W}
{ To create a version for the A/B/A+/B+/Zero or for the 4B/400 simply create a }
{ new project, copy this code into it and modify as required. }
{ }
{ This example was adapted for Ultibo from the Synapse modem example. }
{ }
{ Note: If you downloaded this example separate from the Synapse zip file }
{ then you may need to add the path to the Synapse sources to your project }
{ options. }
{ }
{ To do that go to Project, Project Options... in the Lazarus menu and then }
{ select Compiler Options, Other unit files (-Fu) and enter the correct path. }
{ }
uses
RaspberryPi2,
GlobalConst,
Threads,
Classes,
SysUtils,
Console,
SynaSer;
var
Handle:THandle;
Character:Char;
Characters:String;
BlockSerial:TBlockSerial;
begin
try
{Create our console window}
Handle:=ConsoleWindowCreate(ConsoleDeviceGetDefault,CONSOLE_POSITION_FULL,True);
ConsoleWindowWriteLn(Handle,'Starting Synapse Serial Echo Example');
{Wait a couple of seconds for C:\ drive to be ready}
ConsoleWindowWriteLn(Handle,'Waiting for drive C:\');
while not DirectoryExists('C:\') do
begin
{Sleep for a moment}
Sleep(100);
end;
ConsoleWindowWriteLn(Handle,'C:\ drive is ready');
ConsoleWindowWriteLn(Handle,'');
{Create blocking serial object}
BlockSerial:=TBlockSerial.Create;
BlockSerial.RaiseExcept:=True;
try
{Connect}
BlockSerial.Connect('Serial0');
if BlockSerial.LastError = 0 then
begin
ConsoleWindowWriteLn(Handle,'Connected');
{Configure}
BlockSerial.Config(9600,8,'N',0,False,False);
if BlockSerial.LastError = 0 then
begin
ConsoleWindowWriteLn(Handle,'Configured');
{Loop endlessly waiting for data}
while True do
begin
{Receive a string of characters (Up to the CR LF terminator)}
//Characters:=BlockSerial.RecvString(-1); //The RecvString method only uses CR LF
Characters:=BlockSerial.RecvTerminated(-1,Chr(13)); //Chr(13) + Chr(10) //Change these values if your terminal program uses something different
ConsoleWindowWriteLn(Handle,'Received string: ' + Characters);
if Uppercase(Characters) = 'QUIT' then
begin
{If received then say goodbye and exit our loop}
Characters:='Goodbye!' + Chr(13) + Chr(10);
BlockSerial.SendString(Characters);
{Wait for the data to be sent}
Sleep(1000);
Break;
end;
{Add a carriage return and line feed}
Characters:=Characters + Chr(13) + Chr(10);
{And echo them back to the serial device}
BlockSerial.SendString(Characters);
end;
end
else
begin
ConsoleWindowWriteLn(Handle,'Failed to configure serial port');
end;
end
else
begin
ConsoleWindowWriteLn(Handle,'Failed to connect to serial port');
end;
finally
BlockSerial.free;
end;
{Halt the main thread}
ThreadHalt(0);
except
on E: Exception do
begin
ConsoleWindowWriteLn(Handle,'An exception happened at address ' + IntToHex(PtrUInt(ExceptAddr),8) + ' the message was ' + E.Message);
end;
end;
end.