⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winport32.dpr

📁 用API函数直接写串口的DLL源码
💻 DPR
字号:
library WinPort32;

uses
  FastMM4,
  SysUtils,
  Windows,
  Classes;

{$R *.res}
var
  T_CommStateDCB    : TDCB;
  T_lpCommTimeOuts  : COMMTIMEOUTS;

  T_Com_ID          : THandle;

  Year, Month, Day, Hour, Min, Sec, MSec: Word;
  command           : string;

  //打开串口并发送命令(串口号,波特率,奇偶校验位,数据位,停止位,数据终端:DTR,发送请求:RTS;命令字符串):返回布尔
function Send_Command_State(T_Com, T_Baud, T_Parity, T_Data, T_Stop, T_DTR, T_RTS, T_Command: string): Boolean;
//十六进制转换
  function SendHex(YT_S: string): Boolean;
  var
    YT_buf          : array[0..7] of Byte;
    YT_i            : Byte;
    Wor_d           : Word;
    dwBytesWritten  : DWORD;
  begin
    try
      Result := Trim(YT_S) <> '';
      if not Result then Exit;

      Wor_d := Length(YT_S) div 2;

      for YT_i := 0 to Wor_d - 1 do
        YT_buf[YT_i] := StrToInt('$' + Copy(YT_S, YT_i * 2 + 1, 2));

      Result := WriteFile(T_Com_ID, YT_buf, 8, dwBytesWritten, nil);
      CloseHandle(T_Com_ID);
    except
      Result := False
    end;
  end;
begin
  Result := False;
  try
    T_Com_ID := CreateFile(PChar('Com' + T_Com), GENERIC_READ or GENERIC_WRITE,
      0, nil, OPEN_EXISTING, 0, 0);

    setupcomm(T_Com_ID, 4096, 4096);
    GetCommState(T_Com_ID, T_CommStateDCB);

    BuildCommDCB(PChar('Baud=' + T_Baud + ' Parity=' + T_Parity + ' Data=' + T_Data + ' Stop=' + T_Stop + ' DTR=' + T_DTR + ' RTS=' + T_RTS), T_CommStateDCB);
    SetCommState(T_Com_ID, T_CommStateDCB);

    T_lpCommTimeOuts.ReadIntervalTimeout := MAXDWORD;
    T_lpCommTimeOuts.ReadTotalTimeoutMultiplier := 0;
    T_lpCommTimeOuts.ReadTotalTimeoutConstant := 0;
    T_lpCommTimeOuts.WriteTotalTimeoutMultiplier := 0;
    T_lpCommTimeOuts.WriteTotalTimeoutConstant := 0;
    SetCommTimeouts(T_Com_ID, T_lpCommTimeOuts);

    PurgeComm(T_Com_ID, PURGE_TXCLEAR);
    PurgeComm(T_Com_ID, PURGE_RXCLEAR);

    Result := SendHex(T_Command);
  except

  end
end;

//画中画处理器控制函数(按钮号,时间,按钮号为9时输入的时间才有效)
procedure PButton_Send(ComPort, Style: Byte; Datetime: TDateTime);
begin
  case Style of
    1: command := '9100000000000011';
    2: command := '9300000000000013';
    3: command := '9400000000000014';
    4: command := '9500000000000015';
    5: command := '9600000000000016';
    6: command := '9700000000000017';
    7: command := '9800000000000018';
    8: command := '9900000000000019';
    9:
      begin
        DecodeDate(Datetime, Year, Month, Day);
        command := inttostr(Year);
        Delete(command, 1, 2);
        DecodeTime(Datetime, Hour, Min, Sec, MSec);

        command := '9b' +
          command + inttohex(Month, 2) + inttohex(Day, 2) + inttohex(Hour, 2) + inttohex(Min, 2) + inttohex(Sec, 2) +
          inttohex(27 xor StrToInt(command) xor Month xor Day xor Hour xor Min xor Sec, 2);
      end;
  end;

  Send_Command_State(inttostr(ComPort), '9600', 'N', '8', '1', 'ON', 'ON', command)
end;

exports
  PButton_Send;

begin

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -