Newer code
unit ScannerScreen;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, RTTICtrls, Forms, Controls, Graphics, Dialogs,
StdCtrls, ExtCtrls, synaser, ComObj;
type
{ TForm1 }
TForm1 = class(TForm)
Buttonconnecttoscanner: TButton;
Buttondisconnectfromscanner: TButton;
CheckBoxtexttospeech: TCheckBox;
ComboBoxcomport: TComboBox;
Label1: TLabel;
Memo1: TMemo;
statictexttime: TStaticText;
StaticTextsystemname: TStaticText;
StaticTextdepartmentname: TStaticText;
StaticTextchannelname: TStaticText;
StaticTextFreq: TStaticText;
TILabel1: TTILabel;
Timerprobescanner: TTimer;
TimerClock: TTimer;
procedure ButtonconnecttoscannerClick(Sender: TObject);
procedure ButtondisconnectfromscannerClick(Sender: TObject);
procedure TimerprobescannerTimer(Sender: TObject);
procedure DumpExceptionCallStack(E: Exception);
procedure TimerClockTimer(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.TimerprobescannerTimer(Sender: TObject);
var
ser: TBlockSerial;
rawmessage, modulation, systemname, departmentname, channelname, freq: string;
glgs: TStringList;
SpVoice: variant;
readtext: WideString;
SavedCW: word;
begin
ser := TBlockSerial.Create;
try
try
ser.ConvertLineEnd := True;
ser.Connect(comboboxcomport.Text);
ser.config(115200, 8, 'N', 0, False, False);
ser.sendstring('GLG' + #13#10);
if (ser.LastError <> 0) then
begin
memo1.Clear;
memo1.Lines.Add('I/O Error!');
StaticTextFreq.Caption := 'I/O Error!';
StaticTextsystemname.Caption :=
'Disconnect cable, Restart scanner and Try again!';
StaticTextdepartmentname.Caption := '';
StaticTextchannelname.Caption := '';
Timerprobescanner.Enabled := False;
Buttondisconnectfromscanner.Enabled := False;
Buttonconnecttoscanner.Enabled := True;
comboboxcomport.Enabled := True;
Exit;
end;
rawmessage := ser.Recvstring(2000);
if pos('GLG,,,,,', rawmessage) > 0 then
begin
memo1.Clear;
memo1.Lines.Add('Scanning....');
StaticTextFreq.Caption := 'Scanning....';
StaticTextsystemname.Caption := '';
StaticTextdepartmentname.Caption := '';
StaticTextchannelname.Caption := '';
exit;
end;
if pos('GLG', rawmessage) > 0 then
begin
memo1.Clear;
memo1.Lines.Add(rawmessage);
GLGS := TStringList.Create;
glgs.Text := rawmessage;
glgs.Delimiter := ',';
glgs.StrictDelimiter := True;
glgs.DelimitedText := glgs.Strings[0];
//cmd := glgs.ValueFromIndex[0];
freq := glgs.ValueFromIndex[1];
modulation := glgs.ValueFromIndex[2];
systemname := glgs.ValueFromIndex[5];
departmentname := glgs.ValueFromIndex[6];
Channelname := glgs.ValueFromIndex[7];
glgs.Free;
if ((TRIM(StaticTextchannelname.Caption) <> TRIM(channelname)) or
(trim(StaticTextdepartmentname.Caption) <> trim(departmentname))) then
begin
StaticTextFreq.Caption := freq + ' (' + modulation + ')';
StaticTextsystemname.Caption := systemname;
StaticTextdepartmentname.Caption := departmentname;
StaticTextchannelname.Caption := channelname;
if CheckBoxtexttospeech.Checked = True then
begin
SpVoice := CreateOleObject('SAPI.SpVoice');
// Change FPU interrupt mask to avoid SIGFPE exceptions
SavedCW := Get8087CW;
if pos(departmentname, channelname) > 0 then
begin
READTEXT := TRIM(channelname);
end
else
READTEXT := trim(DEPARTMENTNAME) + ',' + TRIM(channelname);
try
Set8087CW(SavedCW or $4);
SpVoice.Speak(READTEXT, 0);
spvoice.WaitUntilDone(10000);
finally
// Restore FPU mask
Set8087CW(SavedCW);
end;
end;
end;
end
else
begin
memo1.Clear;
memo1.Lines.Add('Data Error!');
StaticTextFreq.Caption := 'Data Error!';
StaticTextsystemname.Caption :=
'Reconnect to scanner';
StaticTextdepartmentname.Caption := '';
StaticTextchannelname.Caption := '';
Timerprobescanner.Enabled := False;
Buttondisconnectfromscanner.Enabled := False;
Buttonconnecttoscanner.Enabled := True;
comboboxcomport.Enabled := True;
exit;
end;
finally
ser.Free;
end;
except
on E: Exception do
begin
Timerprobescanner.Enabled := False;
Buttondisconnectfromscanner.Enabled := False;
Buttonconnecttoscanner.Enabled := True;
comboboxcomport.Enabled := True;
DumpExceptionCallStack(E);
end;
end;
end;
procedure TForm1.ButtondisconnectfromscannerClick(Sender: TObject);
begin
Timerprobescanner.Enabled := False;
Buttondisconnectfromscanner.Enabled := False;
Buttonconnecttoscanner.Enabled := True;
comboboxcomport.Enabled := True;
memo1.Clear;
StaticTextFreq.Caption := '';
StaticTextsystemname.Caption := '';
StaticTextdepartmentname.Caption := '';
StaticTextchannelname.Caption := '';
end;
procedure TForm1.ButtonconnecttoscannerClick(Sender: TObject);
begin
if ComboBoxcomport.ItemIndex = -1 then
begin
ShowMessage('Select Correct COM Port!');
exit;
end;
Timerprobescanner.Enabled := True;
Buttondisconnectfromscanner.Enabled := True;
Buttonconnecttoscanner.Enabled := False;
comboboxcomport.Enabled := False;
end;
procedure TForm1.DumpExceptionCallStack(E: Exception);
var
I: integer;
Frames: PPointer;
Report: string;
begin
Report := 'Program exception! ' + LineEnding + 'Stacktrace:' +
LineEnding + LineEnding;
if E <> nil then
begin
Report := Report + 'Exception class: ' + E.ClassName + LineEnding +
'Message: ' + E.Message + LineEnding;
end;
Report := Report + BackTraceStrFunc(ExceptAddr);
Frames := ExceptFrames;
for I := 0 to ExceptFrameCount - 1 do
Report := Report + LineEnding + BackTraceStrFunc(Frames);
ShowMessage(Report);
//Halt; // End of program execution
end;
procedure TForm1.TimerClockTimer(Sender: TObject);
var
ThisMoment: TDateTime;
begin
ThisMoment := Now;
statictexttime.Caption := FormatDateTime('hh:nn', ThisMoment) +
' ' + FormatDateTime('MM/DD/YYYY', ThisMoment);
end;
end.