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

📄 device.pas

📁 控制扫描仪的DELPHI源码,本人最得意的地方是可以双面打印哦
💻 PAS
📖 第 1 页 / 共 2 页
字号:
            end
            else
              if Copy(ReadBuf, 1, length(Enter)) = Enter then
              begin
                FDeviceCom.SendString(Close);
                break;
              end
              else
              begin
                tmpBuf := tmpBuf + Copy(ReadBuf, 1, ReadLen); //---
                tmpLen := tmpLen + ReadLen;
              end;
          end;
          t2 := GetTickCount;
        end; //---
        ReadBuf := tmpBuf; //---
        ReadLen := tmpLen;
        if (ReadLen = 0) and ((t2 - t1 + 10) > FDeviceMsg.COMTimeOut) then
        begin //超时
          DataBuf := '';
          Result := err_TimeOut;
        end
        else
          if (ReadLen < 0) then
          begin
            DataBuf := '';
            Result := err_ComRead;
          end
          else
          begin
            DataBuf := Copy(ReadBuf, 1, ReadLen);

            Result := ReadLen;
          end;
      end //Connect End
      else
      begin
        Result := err_ComOpen;
      end;
      Disconnect;
    end; //end with
  except
    Result := err_Sys;
  end;
end;

//串口读数据
//输入参数 DevType-设备类型
//返回值   InData-读出数据
//         函数返回:成功返回读字段长度,否则返回错误代码

function TdcDevice.readComData(const DevType: char): integer;
var
  ReadLen, tmpLen: smallint;
  ReadBuf, tmpBuf: string;
  t1, t2: longint;
  ReadStr: string;
begin
  try

    t1 := GetTickCount; //计算机启动后的毫秒数
    DataBuf := '';
    if DevType = 'L' then
    begin
      Result := 0;
      Exit;
    end
    else if (DevType = 'K') then   { TODO : 2005-05-24 修改新大陆/实达密码键盘 }
    begin
      if FDeviceMsg.Version = '1' then
      begin
        Result := readComData();
        Exit;
      end;
    end;

    with FDeviceCom do
    begin
      ComPort := TComPortNumber(Ord(FDeviceMsg.COMPort));
      ComPortSpeed := TComportBaudRate(FDeviceMsg.COMPortSpeed);
      ComPortDataBits := TComPortDataBits(FDeviceMsg.COMDataBit); //数据位
      ComPortStopBits := TComPortStopBits(FDeviceMsg.COMStopBit);
      ComPortParity := TComPortParity(FDeviceMsg.COMParity);
      OutPutTimeout := FDeviceMsg.COMTimeOut;
      ReadBuf := StringOfChar(' ', 2);
      Disconnect;
      if Connect then
      begin
        ReadBuf := StringOfChar(' ', 1000);

        ReadLen := ReadData(ReadBuf[1], 999);
        //ReadLen  :=ReadLen+  ReadData(ReadBuf[1+ReadLen], 999-ReadLen);

        t2 := GetTickCount;
        if (ReadLen = 0) and ((t2 - t1 + 10 ) > FDeviceMsg.COMTimeOut) then
        begin //超时
          DataBuf := '';
          Result := err_TimeOut;
        end
        else
          if (ReadLen < 0) then
          begin
            DataBuf := '';
            Result := err_ComRead;
          end
          else
          begin
           writelog.writeLog('读串口数据ReadBuf   :');
            writelog.writeLog(ReadBuf,Length(ReadBuf));

            DataBuf := Copy(ReadBuf, 1, ReadLen);

            writelog.writeLog('读串口数据DataBuf   :');
            writelog.writeLog(DataBuf, Length(DataBuf));
            Result := ReadLen;
          end;

      end //Connect End
      else
      begin
        Result := err_ComOpen;
      end;
      Disconnect;

      //writelog.writeLog(Pchar(FTempInBuffer));
    end; //end with

  except
    Result := err_Sys;
  end;
end;

//获得配置文件路径
//返回值 函数返回 控制文件路径

function TdcDevice.getConfigPath: string;
begin
  Result := FClientConfig.ConfigPath;
end;

//设置配置文件路径
//输入参数 Value-配置文件路径

procedure TdcDevice.setConfigPath(Value: string);
begin
  FClientConfig.ConfigPath := Value;
end;

//打开端口
//输入参数 DevType-设备类型
//返回值   函数返回 0:成功 -1:失败

function TdcDevice.Open(const DevType: char): integer;
var
  ControlMsg: TdcDevMsgStru;
  Data, DefPrtName: string;
  BPDeviceMsg: TdcCfgMsgStru;
begin
  Data := '';
  Result := FClientConfig.getConfig(DevType, FDeviceMsg);
  if Result = 0 then
  begin
    FControlFile.DrvFileName := FDeviceMsg.DrvFileName;
    if DevType = 'L' then
    begin
      if EnumPrt(DefPrtName) < 0 then //获取打印机名称
      begin
        Result := err_Sys;
        ShowMessage('请设置打印机!');
        Exit;
      end;
      StartPrint(DefPrtName);
    end
    else
    begin
      if FDeviceMsg.BPUse then
      begin
        Result := FClientConfig.getConfig('B', BPDeviceMsg);
        if Result = 0 then
        begin
          FControlFile.DrvFileName := BPDeviceMsg.DrvFileName;
          Result := FControlFile.getCmd('B', 'CONTROL', DevType + 'O', Data,
            ControlMsg);
          if Result = 0 then
          begin
            Result := sendComData('B', Data);
            //Result := OpenOrCloseBP(BPDeviceMsg, Data);
            FDeviceMsg.BPOpenEscCmdStr := Data;
            if Result > 0 then
            begin
              Data := '';
              FDeviceMsg.BPConnected := True;
              Result := FControlFile.getCmd('B', 'CONTROL', DevType + 'E', Data,
                ControlMsg);
              if Result = 0 then
              begin
                FDeviceMsg.BPCloseEscCmdStr := Data;
              end;
            end;
          end;
          FControlFile.DrvFileName := FDeviceMsg.DrvFileName;
        end
        else
        begin
          Result := err_Sys;
        end;
      end;
    end;
  end;
end;

//关闭端口

function TdcDevice.Close: integer;
//var
  //BPDeviceMsg: TdcCfgMsgStru;
begin
  try
    Result := 0;
    if FDeviceMsg.DevType = 'L' then
    begin
      EndPrint();
    end
    else
    begin
      if FDeviceMsg.BPConnected then
      begin
        writelog.writeLog('关闭bp盒 :');
        //FClientConfig.getConfig('B', BPDeviceMsg);
        //Result := OpenOrCloseBP(BPDeviceMsg, FDeviceMsg.BPCloseEscCmdStr);
        Result := sendComData('B', FDeviceMsg.BPCloseEscCmdStr);
        if Result = 0 then
        begin
          FDeviceMsg.BPConnected := False;
        end;
      end;
    end;
  except
    Result := err_Sys;
  end;
end;

//外设控制
//输入参数 Option-命令选项 OutData-输出数据
//返回值 函数返回 >=0 向外设发送控制序列长度 <0 错误代码

function TdcDevice.Control(const Option, OutData: string): integer;
var
  Ret: smallint;
  SendStr, ReadStr, Data: string;
  ControlMsg: TdcDevMsgStru;
begin
  try
    if FDeviceMsg.DevType = 'n' then //未打开设备
    begin
      Result := err_Sys;
      Exit;
    end;

    if (FDeviceMsg.BPUse) and (not FDeviceMsg.BPConnected) then
    begin
      SendStr := FDeviceMsg.BPOpenEscCmdStr;
    end
    else
    begin
      SendStr := '';
    end;
    Data := OutData;
    Result := FControlFile.getCmd(FDeviceMsg.DevType, 'CONTROL', Option, Data,
      ControlMsg);
    if Result = 0 then
    begin
      SendStr := SendStr + Data;
      if SendStr <> '' then
      begin
        case FDeviceMsg.DevType of
          'L':
            if ToPrn(SendStr) then
            begin
              Result := Length(SendStr); //流水打印发送
            end
            else
            begin
              Result := err_LPrinter;
            end;
          else
            begin
              Result := sendComData(FDeviceMsg.DevType, SendStr); //发送
              if (Result > 0) and (ControlMsg.RetCmdStrLen <> 0) then
              begin
              writelog.writeLog('zcg111');
                Ret := readComData(FDeviceMsg.DevType); //返回长度
                if Ret >= 0 then
                begin
                  Ret := FControlFile.getRetCode(FDeviceMsg.DevType, ReadStr);
                end;
                if Ret < 0 then
                begin
                  Result := Ret;
                end;
              end;

            end;
        end; // end case
      end; //end if
    end;
    FDeviceMsg.BPConnected := True;
  except
    Result := err_Sys;
  end;
end;

//外设输出
//输入参数 Option-命令选项
//返回值 InData-输入数据
//       函数返回 >=0 InData长度 <0 错误代码

function TdcDevice.Read(const Option: string): integer;
var
  Ret: smallint;
  MidStr: string;
  SendStr, OutData: string;
  ControlMsg: TdcDevMsgStru;
begin
  try

    { TODO : sun 2006-06-21 国光加密密码键盘修改 }
    if (FDeviceMsg.DevType = 'K')and(FDeviceMsg.Version = '2') then
    begin
      Result := Read_keypad(PChar(FDeviceMsg.COMStr),PChar(option));
      Exit;
    end;

      
    if FDeviceMsg.DevType = 'n' then ////未打开设备
    begin
      Result := err_Sys;
      Exit;
    end;

    if (FDeviceMsg.BPUse) and (not FDeviceMsg.BPConnected) then
    begin
      SendStr := FDeviceMsg.BPOpenEscCmdStr;
    end
    else
    begin
      SendStr := '';
    end;
    OutData := '';
    Result := FControlFile.getCmd(FDeviceMsg.DevType, 'READ', Option, OutData,
      ControlMsg);
    if Result = 0 then
    begin
      SendStr := SendStr + OutData;
      if SendStr <> '' then
      begin
        Result := sendComData(FDeviceMsg.DevType, SendStr); //发送
        if Result <= 0 then
        begin
          Exit;
        end;
      end;
      Result := readComData(FDeviceMsg.DevType); //返回长度
      if (Result > 0) and (ControlMsg.RetCmdStrLen <> 0) then
      begin
        Ret := FControlFile.getRetCode(FDeviceMsg.DevType, Copy(DataBuf, 1,
          ControlMsg.RetCmdStrLen));
        if Ret = 0 then
        begin
          DataBuf := Copy(DataBuf, ControlMsg.RetCmdStrLen + 1, Length(DataBuf)
            - ControlMsg.RetCmdStrLen - ControlMsg.EndCmdStrLen);
          Result := Length(DataBuf)
        end
        else
        begin
          Result := Ret;
        end;
      end; {}

    end;

    //去掉中间的间隔符号
    MidStr := FControlFile.getMidStr(FDeviceMsg.DevType, option);
    if (MidStr <> '') then //有间隔符号
    begin
      DataBuf := copy(DataBuf, 1, pos(MidStr, DataBuf) - 1) +
        copy(DataBuf, pos(MidStr, DataBuf) + length(MidStr), length(dataBuf));
    end;

    FDeviceMsg.BPConnected := True;
  except
    Result := err_Sys;
  end;
end;

//外设输出
//输入参数 Option-命令选项 OutData-输出数据
//返回值 函数返回 >=0 向外设发送控制序列长度 <0 错误代码

function TdcDevice.Write(const Option, OutData: string): integer;
var
  Ret: smallint;
  SendStr, Data: string;
  ControlMsg: TdcDevMsgStru;
begin
  try
    if FDeviceMsg.DevType = 'n' then //未打开设备
    begin
      Result := err_Sys;
      Exit;
    end;

    if (FDeviceMsg.BPUse) and (not FDeviceMsg.BPConnected) then
    begin
      SendStr := FDeviceMsg.BPOpenEscCmdStr;
    end
    else
    begin
      SendStr := '';
    end;
    Data := OutData;
    Result := FControlFile.getCmd(FDeviceMsg.DevType, 'WRITE', Option, Data,
      ControlMsg);
    if Result = 0 then
    begin
      SendStr := SendStr + Data;
      if SendStr <> '' then
      begin
        case FDeviceMsg.DevType of
          'L':
            if ToPrn(SendStr) then
            begin
              Result := Length(SendStr); //流水打印发送
            end
            else
            begin
              Result := err_LPrinter;
            end;
          else
            begin
              Result := sendComData(FDeviceMsg.DevType, SendStr); //发送
              if (Result > 0) and (ControlMsg.RetCmdStrLen <> 0 ) then
              begin
                Ret := readComData(FDeviceMsg.DevType); //返回长度
                if Ret >= 0 then
                begin
                  Ret := FControlFile.getRetCode(FDeviceMsg.DevType, Data);
                end;
                if Ret < 0 then
                begin
                  Result := Ret;
                end;
              end;
            end;
        end; // end case
      end; //end if
    end;
    FDeviceMsg.BPConnected := True;
  except
    Result := err_Sys;
  end;
end;

function TdcDevice.GetUseCharDrv: boolean;
begin
  result := self.FControlFile.usCharDrv;
end;

procedure TdcDevice.SetUseCharDrv(Value: boolean);
begin
  self.FControlFile.usCharDrv := value;
end;

function TdcDevice.Read_keypad(Comm,Option: PChar): integer;
var
  Pwd:array [0..300] of Char;
  KeyData:array [0..16] of Char;
  ints: integer;
begin
  //hComm:= InitComm('1,9600,n,8,1');
  hComm:= InitComm(Comm);
  if Option = 'KEYPAD1' then
    Result := Read_Pwd(hComm,1,KeyData)
  else
    Result := Read_Pwd(hComm,2,KeyData);

  DataBuf := KeyData;
  if Result > 0  then
    Result := Length(DataBuf)
  else
    Result := err_TimeOut;
  CloseComm(hComm);
end;

end.

⌨️ 快捷键说明

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