?? umain.pas
字號:
unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, MSCommLib_TLB, ComCtrls, IdBaseComponent,
IdComponent, IdUDPBase, IdUDPServer,IdSocketHandle, CoolTrayIcon, Menus;
type
TForm1 = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Label1: TLabel;
btnExe: TButton;
btnSoundUp: TButton;
btnSoundDown: TButton;
btnMuteOn: TButton;
btnMuteOff: TButton;
cmbInput: TComboBox;
MSComm1: TMSComm;
btnStop: TButton;
m1: TMemo;
IdUDPServer1: TIdUDPServer;
m2: TMemo;
CoolTrayIcon1: TCoolTrayIcon;
PopupMenu1: TPopupMenu;
menuShow: TMenuItem;
menuExit: TMenuItem;
procedure btnExeClick(Sender: TObject);
procedure btnSoundUpClick(Sender: TObject);
procedure btnSoundDownClick(Sender: TObject);
procedure btnMuteOnClick(Sender: TObject);
procedure btnMuteOffClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure MSComm1Comm(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure menuShowClick(Sender: TObject);
procedure menuExitClick(Sender: TObject);
procedure CoolTrayIcon1DblClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
bzw:boolean;
procedure OutputToCom(pi_oleData:oleVariant);
procedure SwitchInput(pi_char:Char);
procedure IncSound();
procedure DecSound();
procedure StopChangeSound();
procedure MuteON();
procedure MuteOFF();
procedure Proccess(pi_sStr:String);
procedure Log(pi_sStr:String);
public
{ Public declarations }
procedure ApplicationEventsException(Sender: TObject; E: Exception);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnExeClick(Sender: TObject);
begin
SwitchInput(cmbInput.Text[1]);
end;
procedure TForm1.btnSoundUpClick(Sender: TObject);
begin
IncSound();
end;
procedure TForm1.btnSoundDownClick(Sender: TObject);
begin
DecSound();
end;
procedure TForm1.btnMuteOnClick(Sender: TObject);
begin
MuteON();
end;
procedure TForm1.btnMuteOffClick(Sender: TObject);
begin
MuteOFF();
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if mscomm1.PortOpen then
begin
mscomm1.PortOpen:=false;
end;
end;
procedure TForm1.MSComm1Comm(Sender: TObject);
begin
case mscomm1.CommEvent of
comEvReceive: bzw := true;
end;
end;
procedure TForm1.btnStopClick(Sender: TObject);
begin
StopChangeSound();
end;
procedure TForm1.SwitchInput(pi_char: Char);
var
senddata:array of byte;
begin
Log('SwitchInput');
Setlength(senddata,4);
senddata[0]:=$11; //要發(fā)送的數(shù)據(jù)
senddata[1]:=$80;
senddata[3]:=$B1;
case pi_char of
'A':
senddata[2]:=$20;
'B':
senddata[2]:=$21;
'C':
senddata[2]:=$22;
'D':
senddata[2]:=$23;
end;
OutputToCom(senddata);
end;
procedure TForm1.Log(pi_sStr: String);
begin
if m1.Lines.Count>1024 then
begin
m1.Lines.Clear;
end;
m1.Lines.Insert(0,DateTimeToStr(now)+':'+pi_sStr);
end;
procedure TForm1.OutputToCom(pi_oleData: oleVariant);
begin
if not MSComm1.PortOpen then
begin
MSComm1.PortOpen := true;
Log('打開端口成功!');
end;
if MSComm1.PortOpen then
begin
msComm1.Output := pi_oleData ;
Log('執(zhí)行命令成功!');
end
else
begin
Log('執(zhí)行命令失敗');
end;
end;
procedure TForm1.IncSound;
var
senddata:array of BYTE;
begin
Log('Inc Sound');
SetLength(senddata,4);
senddata[0]:=($11); //要發(fā)送的數(shù)據(jù)
senddata[1]:=($82);
senddata[2]:=($21);
senddata[3]:=($B1);
OutputToCom(senddata);
Sleep(500);
StopChangeSound();
end;
procedure TForm1.StopChangeSound;
var
senddata:array of BYTE;
begin
SetLength(senddata,4);
senddata[0]:=($11); //要發(fā)送的數(shù)據(jù)
senddata[1]:=($82);
senddata[2]:=($22);
senddata[3]:=($B1);
OutputToCom(senddata);
end;
procedure TForm1.DecSound;
var
senddata:array of BYTE;
begin
Log('DecSound');
SetLength(senddata,4);
senddata[0]:=($11); //要發(fā)送的數(shù)據(jù)
senddata[1]:=($82);
senddata[2]:=($20);
senddata[3]:=($B1);
OutputToCom(senddata);
Sleep(500);
StopChangeSound();
end;
procedure TForm1.MuteON;
var
senddata:array of BYTE;
begin
Log('MuteON');
SetLength(senddata,4);
senddata[0]:=($11); //要發(fā)送的數(shù)據(jù)
senddata[1]:=($81);
senddata[2]:=($20);
senddata[3]:=($B1);
OutputToCom(senddata);
end;
procedure TForm1.MuteOFF;
var
senddata:array of BYTE;
begin
Log('MuteOFF');
SetLength(senddata,4);
senddata[0]:=($11); //要發(fā)送的數(shù)據(jù)
senddata[1]:=($81);
senddata[2]:=($21);
senddata[3]:=($B1);
OutputToCom(senddata);
end;
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
begin
m2.Lines.LoadFromStream(AData);
Proccess(m2.Lines.Text);
end;
procedure TForm1.Proccess(pi_sStr: String);
begin
Log('收到_'+pi_sStr);
//字串為5種格式 (SEL_A,VOL_UP,VOL_DOWN,MUTE_OFF,MUTE_ON)
if Pos('VOL_UP',pi_sStr)>0 then
begin
IncSound();
exit;
end;
if Pos('VOL_DOWN',pi_sStr)>0 then
begin
DecSound();
exit;
end;
if Pos('MUTE_OFF',pi_sStr)>0 then
begin
MuteOFF();
exit;
end;
if Pos('MUTE_ON',pi_sStr)>0 then
begin
MuteON();
exit;
end;
if Pos('SEL_',pi_sStr)>0 then
begin
SwitchInput(pi_sStr[5]);
exit;
end;
Log('非法命令!');
end;
procedure TForm1.ApplicationEventsException(Sender: TObject; E: Exception);
begin
log(e.Message);
if MSComm1.PortOpen then
begin
MSComm1.PortOpen := false;
end;
end;
procedure TForm1.menuShowClick(Sender: TObject);
begin
CoolTrayIcon1.ShowMainForm;
end;
procedure TForm1.menuExitClick(Sender: TObject);
begin
Application.Terminate;
end;
procedure TForm1.CoolTrayIcon1DblClick(Sender: TObject);
begin
CoolTrayIcon1.ShowMainForm;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
mscomm1.CommPort :=1; //指定端口
mscomm1.Settings := '9600,o,8,1'; //其它參數(shù)
mscomm1.InBufferSize := 1024; //接收緩沖區(qū)
mscomm1.OutBufferSize := 1024; //發(fā)送緩沖區(qū)
mscomm1.InputMode :=comInputModeBinary; //接收模式
mscomm1.InputLen := 1; //一次讀取所有數(shù)據(jù)
mscomm1.SThreshold := 0; //一次發(fā)送所有數(shù)據(jù)
mscomm1.InBufferCount := 0; //清空讀取緩沖區(qū)
mscomm1.OutBufferCount := 0; //清空發(fā)送緩沖區(qū)
MSComm1.RThreshold := 1; //設(shè)置接收多少字節(jié)開產(chǎn)生oncomm事件
mscomm1.PortOpen:=true; //打開端口
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -