📄 hardwareinterfacesrc.~pas
字号:
unit HardwareInterfaceSrc;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm, MPlayer;
type
TFormHardwareInterface = class(TForm)
ComboBoxCOM: TComboBox;
Button1: TButton;
Button2: TButton;
ListenComm: TComm;
MediaPlayer1: TMediaPlayer;
EditLevel1: TEdit;
EditLevel2: TEdit;
procedure ButtonOKClick(Sender: TObject);
procedure ButtonCancelClick(Sender: TObject);
procedure OnReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormHardwareInterface: TFormHardwareInterface;
sBuf, rBuf:array[1..8] of byte;
implementation
{$R *.dfm}
uses
BJDataSrc, BJInfoSrc, MainSrc;
procedure TFormHardwareInterface.ButtonOKClick(Sender: TObject);
begin
//先停止串口
ListenComm.StopComm;
//修改串口
ListenComm.CommName := ComboBoxCOM.Text;
//再开启串口
ListenComm.StartComm;
Close;
end;
procedure TFormHardwareInterface.ButtonCancelClick(Sender: TObject);
begin
Close;
end;
procedure TFormHardwareInterface.OnReceiveData(Sender: TObject;
Buffer: Pointer; BufferLength: Word);
var
iLouhao, iDanyuanhao, iFanghao, iLevel:Integer;
strLevel:string;
strDateAndTime:string;
strSQL:string;
//strPopUp:String;
i:Integer;
//j:Integer;
begin
//MessageBox(Application.Handle, '接收数据成功', '提示', MB_OK);
//for i := 1 to 8 do
// rBuf[i] := 0;
move(Buffer^, pchar(@rBuf[1])^, BufferLength);
if rBuf[1] > 16 then //若是0xaa的握手信号, 则确认该信号
begin
//MessageBox(Application.Handle, '接收握手成功', '提示', MB_OK);
if not ListenComm.writecommdata(@rBuf[1], 1) then
begin
MessageBox(Application.Handle, '发送握手确认信号错误', '错误', MB_OK);
end;
for i := 1 to 8 do
rBuf[i] := 0;
end
//如果不是0Xaa,则说明开始发送数据部分
else
begin //314790
//MessageBox(Application.Handle, PCHAR(@rBuf[1]), '接收数据部分', MB_OK);
iLouhao := rBuf[1] * 10 + rBuf[2];
iDanyuanhao := rBuf[3];
iFangHao := rBuf[4] * 10 + rBuf[5];
iLevel := rBuf[6];
if iLevel = 0 then
begin
strLevel := '普通';
//EditLevel.Text := '普通';
end
else
begin
strLevel := '紧急';
//EditLevel.Text := '紧急';
end;
//获取日期和时间
strDateAndTime := FormMain.EditDataAndTime.Text;
//下面写入报警信息表
strSQL := 'Insert into 报警信息(楼号, 单元号, 房号, 警情等级, 日期和时间) values (' +
'"' + IntToStr(iLouhao) + '"' + ',' + '"' + IntToStr(iDanyuanhao) + '"' + ',' + '"' +
IntToStr(iFanghao) + '"' + ',' + '"' + strLevel + '"' + ',' + '"' + strDateAndTime + '"' +
')';
FormBJInfo.ADOQuery1.Close;
FormBJInfo.ADOQuery1.SQL.Clear;
FormBJInfo.ADOQuery1.SQL.Text := strSQL;
FormBJInfo.ADOQuery1.ExecSQL;
//更新显示
strSQL := 'select * from 报警信息';
FormBJInfo.ADOQuery1.SQL.Text := strSQL;
FormBJInfo.ADOQuery1.Open;
//下面是报警音乐
if iLevel = 0 then
begin
//FormMain.light6to10.Font.Color := clOlive;
//FormMain.light1to5.Font.Color := clOlive;
MediaPlayer1.Close;
MediaPlayer1.FileName := 'picture\\music0.mp3';
MediaPlayer1.Open();
MediaPlayer1.Play;
end
else
begin
//FormMain.light6to10.Font.Color := clRed;;
//FormMain.light1to5.Font.Color := clRed;;
MediaPlayer1.Close;
MediaPlayer1.FileName := 'picture\\music1.mp3';
MediaPlayer1.Open;
MediaPlayer1.Play;
end;
//下面使能信号灯控制时钟
if iLouhao <= 50 then
begin
if strLevel = '紧急' then
begin
FormMain.light1to5.Font.Color := clRed;
end
else
begin
FormMain.light1to5.Font.Color := clOlive;
end;
EditLevel1.Text := strLevel;
FormMain.light1to5.Visible := true;
FormMain.TimerFlashJu.Enabled := true;
//FormMain.TimerFlashJin.Enabled := false;
end
else
begin
if strLevel = '紧急' then
begin
FormMain.light6to10.Font.Color := clRed;
end
else
begin
FormMain.light6to10.Font.Color := clOlive;
end;
EditLevel2.Text := strLevel;
FormMain.light6to10.Visible := true;
FormMain.TimerFlashJin.Enabled := true;
//FormMain.TimerFlashJu.Enabled := false;
end;
//下面返回信息,以让单片机认为发送成功
sBuf[1] := 170;
if not ListenComm.writecommdata(@sBuf[1], 1) then
begin
MessageBox(Application.Handle, '发送接收完毕确认信息时错误', '错误', MB_OK);
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -