[go: up one dir, main page]

Menu

[r338]: / trunk / uudp.pas  Maximize  Restore  History

Download this file

342 lines (296 with data), 12.9 kB

  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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
{********************************************************************************
*** AlarmeJT ***
*** AlarmeJT is designed to work with exellent WSJT -X Joe Taylor, K1JT ***
*** running on JT65A, JT9 and FT8 modes. ***
*** ------------------------------------------------------------------- ***
*** version : 0.8 UDP beta ***
*** ------------------------------------------------------------------- ***
*** Copyright 2017 Alain Thébault (F5JMH) ***
*** ***
*** UDP and NetworkMessageUtils modules by G4WJS (thank's) ***
*** ***
*** This file is part of AlarmeJT. ***
*** ***
*** AlarmeJT is free software: you can redistribute it and/or modify ***
*** it under the terms of the GNU General Public License as published by ***
*** the Free Software Foundation, either version 2.0 of the License, or ***
*** any later version. ***
*** ***
*** AlarmeJT is distributed in the hope that it will be useful, ***
*** but WITHOUT ANY WARRANTY; without even the implied warranty of ***
*** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ***
*** GNU General Public License for more details. ***
*** ***
*** You should have received a copy of the GNU General Public License ***
*** along with AlarmeJT. If not, see <http://www.gnu.org/licenses/>. ***
*** ***
*** ------------------------------------------------------------------- ***
********************************************************************************}
unit uudp;
{$mode objfpc}{$H+}
interface
uses
{$DEFINE UseCThreads}
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ExtCtrls, DateUtils, IdUDPServer, IdSocketHandle,
IdGlobal, IdComponent, NetworkMessageUtils, ufonction;
type
{ TForm1 }
{ TFUDP }
TFUDP = class(TForm)
IdUDPServer1: TIdUDPServer;
Memo1: TMemo;
procedure sentToWsjtxFreeText(message: string); {........................WSJTx messageType 9}
procedure sentWsjtxMT4(Call: string); {.....................................WSJTx messageType 4}
procedure IdUDPServer1Status(ASender: TObject; const AStatus: TIdStatus;
const AStatusText: string);
procedure IdUDPServer1UDPException(AThread: TIdUDPListenerThread;
ABinding: TIdSocketHandle; const AMessage: String;
const AExceptionClass: TClass);
procedure IdUDPServer1UDPRead(AThread: TIdUDPListenerThread;
AData: TIdBytes; ABinding: TIdSocketHandle);
private
{ private declarations }
public
{ public declarations }
end;
var
FUDP: TFUDP;
UFreq, UModeRX, UModeTX, UDXCall, URSTs, UHeureDeb : string;
UCall,ULoc : string;
UIndex:integer;
peerPort: qword;
SNR: LongInt;
implementation
USES ualarmeJT, ucontact;
{$R *.lfm}
{ TForm1 }
procedure TFUDP.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread;
AData: TIdBytes; ABinding: TIdSocketHandle);
var
index: Integer = 0;
magic, schema, messageType: LongInt;
id, mode, DXCall, report, TXMode, message, DXGrid, DEGrid, DECall, reportReceived: string;
TXPower, comments, DXName: string;
frequency: QWord;
isNew, TXEnabled, transmitting, Decoding: Boolean;
tm: Longword;
ztime: TDateTime;
DT: Double;
DF: Cardinal;
date: TDateTime;
{Ajout ici}
z: TStringList;
Memomessage,locator: string;
rst, RXDF, TXDF: integer;
begin
//FUDP.IdUDPServer1.Bindings := '127.0.0.1:2237,[::]:2237';
peerPort := ABinding.PeerPort; {............................................récupération du port d'écoute}
// peerPort := 2237;
// Memo1.Lines.Add('Datagram received - length: ' + IntToStr(Length(AData))); { DO NOT DELETE THIS }
while index < Length(AData) do
begin
Unpack(AData, index, magic);
//Memo1.Lines.Add('index:' + IntToStr(index) + ' magic:$' + IntToHex(magic,8)); { DO NOT DELETE THIS }
if (magic = LongInt($ADBCCBDA)) and (index < Length(AData)) then
begin
Unpack(AData, index, schema);
if (schema = 2) and (index < Length(AData)) then
begin
Unpack(AData,index,messageType);
Unpack(AData,index,id);
//Memo1.Lines.Add('Message type:' + IntToStr(messageType) + ' from:[' + id + ']'); { DO NOT DELETE THIS }
case messageType of
0: begin
//Memo1.Lines.Add('Heartbeat!'); { DO NOT DELETE THIS }
FalarmeJT.SLedEcoute.Brush.Color:=clGreen;
end;
1: {............................................................Status}
begin
Unpack(AData,index,frequency);
Unpack(AData,index,mode);
Unpack(AData,index,DXCall);
Unpack(AData,index,report);
Unpack(AData,index,TXMode);
Unpack(AData,index,TXEnabled);
Unpack(AData,index,transmitting);
Unpack(AData,index,Decoding);
Unpack(AData,index,RXDF);
Unpack(AData,index,TXDF);
Unpack(AData,index,DECall);
Unpack(AData,index,DEGrid);
Unpack(AData,index,DXGrid);
//{ Désactivé car pose problème pour le moment il faudrait dégager le Memo du MT4 ONLY FOR DEBUG
Memo1.Lines.Add('Status: Frequency: ' + IntToStr(frequency) + ' Mode: ' + mode + ' DX Call: ' + DXCall
+ ' Report: ' + report + ' TX Mode: ' + TXMode + ' TX Enabled: ' + BoolToStr(TXEnabled)
+ ' Transmitting: ' + BoolToStr(transmitting) + ' Decoding: ' + BoolToStr(Decoding)
+ ' RXDF: ' + intToStr(RXDF) + ' TXDF: ' + intToStr(TXDF) + ' DECall: ' + DECall + ' DEGrid: ' + DEGrid
+ ' DXGrid: ' + DXGrid);
//}
Contact.ContactAffiche(DXCall,intToStr(frequency),TXMode,report,DXGrid,BoolToStr(transmitting),BoolToStr(Decoding)); {.... Report = RSTs mode remplacé par TXMode}
if transmitting then
FalarmeJT.SLedTransmit.Brush.Color:=clRed
else
FalarmeJT.SLedTransmit.Brush.Color:=clGreen;
end;
2: {............................................................Décodage}
begin
Unpack(AData,index,isNew);
Unpack(AData,index,tm);
ztime := IncMilliSecond(0,tm);
Unpack(AData,index,SNR);
Unpack(AData,index,DT);
Unpack(AData,index,DF);
Unpack(AData,index,mode);
Unpack(AData,index,message);
Memomessage :='Decode:'+' '+BoolToStr(isNew)+' '+FormatDateTime('hhmm',ztime)+' '+IntToStr(SNR)
+' '+ FloatToStrF(DT, ffGeneral,4,1)+' '+IntToStr(DF)
+' '+ mode +' '+ message +' '+ timeToStr(ztime) +' '+ FloatToStr(DT) +' '+ intToStr(tm);
Memo1.Lines.Add(Memomessage);
rst:=SNR; // Ajout rst
mode:=decodeMode(mode); {.......................................... #=JT65 et @=JT9 tilde = FT8 ou :=qra64}
try
z:=TStringList.Create;
z.Delimiter:=' ';
z.DelimitedText:=message;
if z.count > 1 then begin
z.Add(message);
if z[1] = 'DX' then begin
DXCall := z[2];
locator := z[3];
end
else begin
DXCall := z[1];
locator := z[2];
end;
UCall := z[0];
locator := z[2];
Pvalid(UCall,DXCall,mode,locator,rst); // Ajout rst
// saveinfo(FormatDateTime('hhmm',time),DXCall,mode,SNR); { en test }
end;
except
on E : Exception do WriteLn(Format('ERROR : %s',[e.Message]));
end;
z.Free;
end;
3: {............................................................Clear}
begin
Memo1.Lines.Add('Clear');
end;
5: {............................................................QSO logged}
begin
Unpack(AData,index,date);
Unpack(AData,index,DXCall);
Unpack(AData,index,DXGrid);
Unpack(AData,index,frequency);
Unpack(AData,index,mode);
Unpack(AData,index,report);
Unpack(AData,index,reportReceived);
Unpack(AData,index,TXPower);
Unpack(AData,index,comments);
Unpack(AData,index,DXName);
Memo1.Lines.Add('QSO logged: Date:' + FormatDateTime('dd-mmm-yyyy hh:mm:ss',date)
+ ' DX Call:' + DXCall + ' DX Grid:' + DXGrid
+ ' Frequency:' + IntToStr(frequency) + ' Mode:' + mode + ' Report sent: ' + report
+ ' Report received:' + reportReceived + ' TX Power:' + TXPower
+ ' Comments:' + comments + ' Name:' + DXName);
end
else
Memo1.Lines.Add('Unrecognized message type:' + IntToStr(messageType));
end;
end;
end;
end;
end;
procedure TFUDP.IdUDPServer1UDPException(AThread: TIdUDPListenerThread;
ABinding: TIdSocketHandle; const AMessage: String;
const AExceptionClass: TClass);
begin
Memo1.Lines.Add('UDP server exception: ' + AMessage);
end;
procedure TFUDP.IdUDPServer1Status(ASender: TObject; const AStatus: TIdStatus;
const AStatusText: string);
begin
Memo1.Lines.Add('UDP server status: ' + AStatusText);
end;
procedure TFUDP.sentToWsjtxFreeText(message: string);
var
AData: TIdBytes;
index: Integer = 0;
messageType, magic, schema: LongInt;
TXEnabled: Boolean;
id: String;
begin
magic := $ADBCCBDA;
schema:=2;
messageType:=9;
id:='WSJT-X';
TXEnabled:=True;
pack(AData, magic);
pack(AData, schema);
Pack(AData,messageType);
pack(AData,id);
pack(AData, message);
Pack(AData,TXEnabled);
IdUDPServer1.SendBuffer('127.0.0.1', PeerPort, AData);
end;
procedure TFUDP.sentWsjtxMT4(Call: string);
var
AData: TIdBytes;
messageType, magic, schema: LongInt;
id, Ip: String; //, mode
i: integer;
resultat: TStringList;
DT: Double;
tp2: integer;
message: string;
cpt: integer;
begin
magic := $ADBCCBDA;
schema:=2;
messageType:=4;
id:='WSJT-X';
for i:=0 to Memo1.Lines.Count -1 do
if pos(Call+' ',memo1.Lines[i])>0 then
begin
Resultat := TStringList.Create;
Resultat.Delimiter := ' ';
Resultat.QuoteChar := ' ';
Resultat.DelimitedText := Memo1.Lines[i];
pack(AData, magic); {.............................................Magic number}
pack(AData, schema); {..............................................Schema}
Pack(AData,messageType); {............................................MessageType}
pack(AData,id); {..........................................ID}
cpt:=resultat.Count-1;
tp2:=StrToInt(resultat[cpt]);
{
if (pos('/',resultat[8])<> 0) then
begin
tp2:=StrToInt(resultat[11]); // Dans le cas PREF/CALL
end;
if Length(resultat[8]) = 2 then // Dans le cas de CQ DX CALL
begin
tp2:=StrToInt(resultat[13]);
end else begin
tp2:=StrToInt(resultat[12]);
end;
}
pack(AData,tp2); {..........................................Time}
Pack(AData,strToint(resultat[3])); {......................................SNR}
DT:=StrToFloat(resultat[4]);
pack(AData,DT); {.........................................................DT}
pack(AData,StrToIntDef(resultat[5],0)); {.................................DF}
pack(AData,resultat[6]); {................................................mode}
message:=resultat[7]+' '+resultat[8]; //+' '+resultat[9]
pack(AData, message); {................................................message}
// ShowMessage (message);
// Ip := '127.0.0.1';
// IdUDPServer1.SendBuffer(Ip, 2237, AData);
IdUDPServer1.SendBuffer('127.0.0.1', PeerPort, AData);
end;
resultat.Free;
end;
end.