unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, LEDApi, ExtCtrls, dxCntner, dxEditor, Registry,
  dxExEdtr, dxEdLib;

Const
  WM_LED_NOTIFY = WM_USER+1;
  MyIp = '192.168.0.100' ;
  zise = 13;
  ResPath = '\Software\E.A.S\MES';
type
  TfrmLED = class(TForm)
    Memo1: TMemo;
    Label1: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    BitBtn6: TBitBtn;
    BitBtn7: TBitBtn;
    BitBtn8: TBitBtn;
    BitBtn9: TBitBtn;
    BitBtn10: TBitBtn;
    BitBtn12: TBitBtn;
    BitBtn11: TBitBtn;
    cmbPort: TComboBox;
    Label2: TLabel;
    edtDate: TEdit;
    Label3: TLabel;
    edtXuhuan: TEdit;
    Label4: TLabel;
    Timer1: TTimer;
    GroupBox1: TGroupBox;
    Label7: TLabel;
    Edit5: TEdit;
    Label8: TLabel;
    edtToday: TEdit;
    Label11: TLabel;
    todaylv: TEdit;
    Label12: TLabel;
    thegelv: TEdit;
    GroupBox2: TGroupBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn6Click(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure BitBtn8Click(Sender: TObject);
    procedure BitBtn9Click(Sender: TObject);
    procedure BitBtn10Click(Sender: TObject);
    procedure BitBtn12Click(Sender: TObject);
    procedure BitBtn11Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure cmbPortChange(Sender: TObject);



  private
    DuctID,MyWhere: String;
    num: integer;
    IsSelected: Boolean;
    begDate,endDate,S: String;

    procedure Addxunhuan;
    Procedure Addjintai;
    procedure addthree;    
    procedure RegisterMe;
    procedure WrtRegistry(xKeyStr, xConText: String);
    function  RedFrmRegistry(xKeyStr: String): String;
    { Private declarations }
  public
    { Public declarations }
    Procedure LEDNotify(Var Msg: TMessage); Message WM_LED_NOTIFY;
  end;

var
  frmLED: TfrmLED;
  Dev:   Integer;
implementation


{$R *.DFM}

procedure TfrmLED.RegisterMe;
var
  Reg: TRegistry;
begin
  Reg:=TRegistry.Create ;
  Reg.RootKey := HKEY_LOCAL_MACHINE;
  if Reg.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Run', True) then
    Reg.WriteString('Led', ParamStr(0));
  Reg.CloseKey ;
  Reg.Free;
end;


procedure TfrmLED.WrtRegistry(xKeyStr, xConText: String);
var
  UserReg: TRegistry;
begin
  UserReg := TRegistry.Create;
  UserReg.RootKey := HKEY_LOCAL_MACHINE;
  if UserReg.OpenKey(ResPath, true) then
    UserReg.WriteString(xKeyStr, xConText);
  UserReg.Free;
end;


function TfrmLED.RedFrmRegistry(xKeyStr: String): String;
var
  UserReg: TRegistry;
begin
  Result := '';
  UserReg := TRegistry.Create;
  UserReg.RootKey := HKEY_LOCAL_MACHINE;
  if UserReg.OpenKey(ResPath, true) then
    Result := UserReg.ReadString(xKeyStr);
  UserReg.Free;
end;


Procedure TfrmLED.LEDNotify(Var Msg: TMessage);
Var
  P: PNotifyMessage;
Begin
  P:=PNotifyMessage(Msg.wParam);
  Case P^.Message Of
    LM_TX_COMPLETE:
    Begin
      //Memo1.Lines.Add('发送完成');
    End;
    LM_RESPOND:
    Begin
      Case P^.Command of
        PKC_QUERY:
        Begin
          Memo1.Lines.Add('查询状态成功');
        End;
        PKC_GET_POWER:
        Begin
          if P^.Status=LED_POWER_ON Then Memo1.Lines.Add('电源打开')
          Else Memo1.Lines.Add('电源关闭');
        End;
        PKC_SET_POWER:
        Begin
          if P^.Result=99 Then Memo1.Lines.Add('处于自动开关屏模式，不能进行电源操作')
          Else
          Begin
            if P^.Status=LED_POWER_ON Then Memo1.Lines.Add('电源打开')
            Else Memo1.Lines.Add('电源关闭');
          End;
        End;
        PKC_GET_BRIGHT:
        Begin
          Memo1.Lines.Add('显示屏亮度为:'+IntToStr(P^.Status));
        End;
        PKC_SET_BRIGHT:
        Begin
          if P^.Result=99 Then Memo1.Lines.Add('处于自动调节亮度状态，不能进行亮度调节')
          Else Memo1.Lines.Add('显示屏亮度为:'+IntToStr(P^.Status));
        End;
        PKC_ADJUST_TIME:
        Begin
          Memo1.Lines.Add('显示屏时钟已校正');
        End;
      End;
    End;
    LM_TIMEOUT:
    Begin
      if P^.Param.devType=DEVICE_TYPE_COM Then Memo1.Lines.Add('发送超时!  '+'类型: 串口  地址：'+IntToStr(P^.Address))
      Else Memo1.Lines.Add('发送超时!  '+'类型: 网络  发往: '+StrPas(P^.Host));
    End;
  End;
End;

procedure TfrmLED.BitBtn1Click(Sender: TObject);
Var
  Device: TDeviceParam;
begin
{--------------------------------}
{      下面是串口的打开例子      }
{--------------------------------}
  Device.devType:=DEVICE_TYPE_COM;  // 通讯设备类型为串口
  Device.Speed:=SBR_57600;         // 此处必须和主板中的设置一致
  Device.ComPort:=cmbPort.ItemIndex + 1;                // 串口号(1-255)

{--------------------------------}
{  下面是使用网络通讯的例子,只要 }
{  将注释去掉即可                }
{--------------------------------}
{  Device.devType:=DEVICE_TYPE_UDP;  // 通讯设备类型为网络
{  Device.locPort:=8888;             // 本地端口号
{--------------------------------}
  Dev:=LED_Open(@Device,1,Handle,WM_LED_NOTIFY); // Notify=1 表示需要接收消息
  if Dev=INVALID_DEVICE_HANDLE Then
  Begin
    MessageBox(Handle,'无法打开指定通讯链路!','错误信息',MB_OK);
    Exit;
  End;
  Memo1.Lines.Add('通讯链路打开成功!');
end;

procedure TfrmLED.BitBtn2Click(Sender: TObject);
begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出

{
  发出一个查询命令，查看控制卡是否在线
  Procedure LED_Query(dev: Integer; CardAddr: Byte; IP: PChar; Port: Integer);
  dev:      通讯链路句柄
  CardAddr: 控制卡地址(0-31), 控制卡地址由控制卡上的5位拨码开关决定
  IP:       控制卡的IP地址(如果使用串口通讯这个参数可填写''
  Port:     控制卡端口号固定为6666(如果用串口通讯这个参数可填写任意值)
}
  LED_Query(Dev,0,'192.168.0.99',6666);
end;

procedure TfrmLED.BitBtn3Click(Sender: TObject);
begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出

  LED_GetPower(Dev,0,'192.168.0.99',6666);
end;

procedure TfrmLED.BitBtn5Click(Sender: TObject);
begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出

  LED_SetPower(Dev,1,'192.168.0.99',6666,0);
end;

procedure TfrmLED.BitBtn4Click(Sender: TObject);
begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出

  LED_SetPower(Dev,1,'192.168.0.99',6666,1);
end;

procedure TfrmLED.BitBtn6Click(Sender: TObject);
begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出

  LED_SetBrightness(Dev,0,'192.168.0.99',6666,1);
end;

procedure TfrmLED.BitBtn7Click(Sender: TObject);
begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出

  LED_GetBrightness(Dev,0,'192.168.0.99',6666);
end;

procedure TfrmLED.BitBtn9Click(Sender: TObject);
begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出

  LED_AdjustTime(Dev,0,'192.168.0.99',6666);
end;

procedure TfrmLED.BitBtn8Click(Sender: TObject);
Var
  ARect:  TRect;
  Bitmap: TBitmap;
begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出

{
  MakeRoot说明
  Function  MakeRoot(RootType: Integer; ScreenType: Integer): Integer; Stdcall; External LEDSender;
  RootType: 可选ROOT_PLAY或ROOT_DOWNLOAD

  ROOT_PLAY: 表示显示内容仅播放，无需保存，控制卡掉电后不再需要显示
  ROOT_DOWNLOAD: 表示显示内容需要保存在主板中，当控制卡上电后自动播放
                (注意：控制卡只能保存1M的数据)

  ScreenType: 必须和控制卡的设置相同，否则将不会显示
}

  MakeRoot(ROOT_PLAY, SCREEN_COLOR);
  Bitmap:=TBitmap.Create;
  Bitmap.LoadFromFile('test.bmp');
  AddLeaf(20000);    //播放20秒
  ARect:=Rect(0,0,128,32);   //显示区域限定在此区域中
  AddWindow(Bitmap.Canvas.Handle,Bitmap.Width,Bitmap.Height,@ARect,1,1,1);
  LED_SendToScreen(Dev,0,'192.168.0.99',6666);
  Bitmap.Free;
end;

procedure TfrmLED.BitBtn10Click(Sender: TObject);
Var
  ARect:  TRect;
begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出

  MakeRoot(ROOT_PLAY, SCREEN_COLOR);
  AddLeaf(20000);    //播放20秒
  ARect:=Rect(0,0,128,32);   //显示区域限定在此区域中
  AddDateTime(@ARect,1,'Arial',18,RGB(255,255,0),DF_HNS);
  LED_SendToScreen(Dev,1,'192.168.0.99',6666);
end;

procedure TfrmLED.BitBtn12Click(Sender: TObject);

begin
 Addxunhuan;
end;

procedure TfrmLED.BitBtn11Click(Sender: TObject);
begin
  LED_Close(Dev);
end;

procedure TfrmLED.FormCreate(Sender: TObject);
var
  year,month,day: Word;
  ComStr: String;
begin
  ComStr := RedFrmRegistry('Ledcom');
  if Comstr<> '' then
    cmbPort.ItemIndex := StrToInt(ComStr)
  else cmbPort.ItemIndex := 0;
  RegisterMe;
  BitBtn1.Click;
  BitBtn4.Click;
 
  begDate := FormatDateTime('yyyy-mm-dd',now - day + 1);
  endDate := FormatDateTime('yyyy-mm-dd',now) ;
  edtDate.Text := FormatDateTime('yyyy年mm月dd日',Now);
  edtXuhuan.Text := '久乐气囊走向世界！';
  num := 0; 

end;

procedure TfrmLED.Addjintai;
Var
  ARect:  TRect;
  TxtDate,txt1,txt2: pChar;
begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出
{
  Funnction  AddString(Str: PChar; ARect: PRect; Method,Speed,Transparent: Integer; FontSet,FontColor: Integer): Integer; Stdcall; External LEDSender;
  该函数仅发送内码到控制卡，因此发送速度很快，适用于对发送速度要求较高，而且只显示文字的场合
  在使用该函数之前需确定控制卡内下载了字库文件，如果控制卡内没有字库文件，则什么都不显示
}
  MakeRoot(ROOT_PLAY, SCREEN_COLOR);
  AddLeaf(20000);    //播放20秒
  ARect:=Rect(4,2,160,24);   //显示区域限定在此区域中
  //AddString('This is a test 中国人民万岁',@ARect,2,1,1,16,RGB(255,255,255));
  TxtDate := Pchar(edtDate.text);
  AddText(TxtDate,@ARect,1,1,1,'宋体',14,RGB(255,255,255));

  ARect:=Rect(4,24,160,41);   //显示区域限定在此区域中
  txt1 := pchar(Edit1.Text) ;
  AddText(txt1,@ARect,1,1,1,'宋体',zise,RGB(255,255,255));

  ARect:=Rect(4,41,160,58);   //显示区域限定在此区域中
  txt1 := pchar(Edit2.Text) ;
  AddText(txt1,@ARect,1,1,1,'宋体',zise,RGB(255,255,255));

  ARect:=Rect(4,58,160,75);   //显示区域限定在此区域中
  txt1 := pchar(Edit3.Text) ;
  AddText(txt1,@ARect,1,1,1,'宋体',zise,RGB(255,255,255));

  ARect:=Rect(4,75,160,96);   //显示区域限定在此区域中
  txt1 := pchar(Edit4.Text) ;
  AddText(txt1,@ARect,1,1,1,'宋体',zise,RGB(255,255,255));

  LED_SendToScreen(Dev,1,'192.168.0.99',6666);

end;

procedure TfrmLED.Addxunhuan;
Var
  ARect:  TRect;
  TxtDate,xuntxt: pChar;
begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出
{
  Funnction  AddString(Str: PChar; ARect: PRect; Method,Speed,Transparent: Integer; FontSet,FontColor: Integer): Integer; Stdcall; External LEDSender;
  该函数仅发送内码到控制卡，因此发送速度很快，适用于对发送速度要求较高，而且只显示文字的场合
  在使用该函数之前需确定控制卡内下载了字库文件，如果控制卡内没有字库文件，则什么都不显示
}
  MakeRoot(ROOT_PLAY, SCREEN_COLOR);
  AddLeaf(20000);    //播放20秒
 { ARect:=Rect(8,0,160,19);   //显示区域限定在此区域中

  TxtDate := Pchar(edtDate.text);
  AddText(TxtDate,@ARect,1,1,1,'宋体',16,RGB(255,255,255));   }

  ARect:=Rect(0,24,160,80);   //显示区域限定在此区域中
  xuntxt := pchar(edtXuhuan.Text) ;
  AddText(xuntxt,@ARect,2,1,1,'宋体',36,RGB(255,255,255));

  LED_SendToScreen(Dev,1,'192.168.0.99',6666);

end;

procedure TfrmLED.Timer1Timer(Sender: TObject);
var
  curNum: Integer;
  isupdate: Boolean;
begin
  curNum  := (num mod 3);
  case curNum of
    0: Addxunhuan ;
    1: Addjintai;
    2: addthree ;
  end;
  num := num + 1;
end;

procedure TfrmLED.addthree;
Var
  ARect:  TRect;
  TxtDate,txt1,txt2: pChar;

begin
  if Dev=INVALID_DEVICE_HANDLE Then Exit;  //如果没有打开通讯链路，退出
{
  Funnction  AddString(Str: PChar; ARect: PRect; Method,Speed,Transparent: Integer; FontSet,FontColor: Integer): Integer; Stdcall; External LEDSender;
  该函数仅发送内码到控制卡，因此发送速度很快，适用于对发送速度要求较高，而且只显示文字的场合
  在使用该函数之前需确定控制卡内下载了字库文件，如果控制卡内没有字库文件，则什么都不显示
}
  MakeRoot(ROOT_PLAY, SCREEN_COLOR);
  AddLeaf(20000);    //播放20秒
  ARect:=Rect(4,2,160,24);   //显示区域限定在此区域中
  //AddString('This is a test 中国人民万岁',@ARect,2,1,1,16,RGB(255,255,255));
  TxtDate := Pchar(edtDate.text);
  AddText(TxtDate,@ARect,1,1,1,'宋体',14,RGB(255,255,255));

  ARect:=Rect(4,24,160,41);   //显示区域限定在此区域中
  txt1 := pchar(Edit5.Text) ;
  AddText(txt1,@ARect,1,1,1,'宋体',zise,RGB(255,255,255));

  ARect:=Rect(4,41,160,58);   //显示区域限定在此区域中
  txt1 := pchar(edtToday.Text) ;
  AddText(txt1,@ARect,1,1,1,'宋体',zise,RGB(255,255,255));

  ARect:=Rect(4,58,160,75);   //显示区域限定在此区域中
  txt1 := pchar(todaylv.Text) ;
  AddText(txt1,@ARect,1,1,1,'宋体',zise,RGB(255,255,255));

  ARect:=Rect(4,75,160,96);   //显示区域限定在此区域中
  txt1 := pchar(thegelv.Text) ;
  AddText(txt1,@ARect,1,1,1,'宋体',zise,RGB(255,255,255));

  LED_SendToScreen(Dev,1,'192.168.0.99',6666);

end;

procedure TfrmLED.cmbPortChange(Sender: TObject);
begin
  WrtRegistry('LedCom',IntToStr(cmbPort.ItemIndex));
end;

end.
