Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

Lazarus IDE and 组件 下载地址版权申明
查看: 4952|回复: 3

Lazarus 上 CreateFile 操控流驱动(简单串口操作代码和示例)(转)

[复制链接]

该用户从未签到

发表于 2010-12-15 11:10:47 | 显示全部楼层 |阅读模式
串口实现单元代码:
  1. unit CE_Series;
  2. interface
  3. uses
  4.   Windows,Classes, SysUtils, LResources, StdCtrls,ExtCtrls;
  5.   
  6. type
  7.   TCE_Series = class(TObject)
  8.   
  9.   private
  10.     hComm: THandle;
  11.   public
  12.     Function OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
  13.     procedure Send(str:String);
  14.     Function Receive():String;
  15.     procedure ClosePort();
  16.   end;
  17. implementation
  18. //===============================================================================================
  19. // 语法格式:OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer)
  20. // 实现功能:打开串口
  21. // 参数:port,串口号;例如wince下为从COM1:,COM2:.....win32下为COM1,COM2....... ;其他略,顾名思义哈
  22. // 返回值:错误信息
  23. //===============================================================================================
  24. function TCE_Series.OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
  25. var
  26.   cc:TCOMMCONFIG;
  27. begin
  28.   result:='';
  29.   hComm:=CreateFile(port, GENERIC_READ or GENERIC_WRITE,
  30.        0, nil, OPEN_EXISTING, 0, 0); // 打开COM
  31.   if (hComm = INVALID_HANDLE_VALUE) then begin  // 如果COM 未打开
  32.     result:='CreateFile Error!';
  33.     exit;
  34.   end;
  35.   GetCommState(hComm,cc.dcb); // 得知目前COM 的状态
  36.   cc.dcb.BaudRate:=BaudRate; // 设置波特率为BaudRate
  37.   cc.dcb.ByteSize:=ByteSize;  // 字节为 ByteSize(8 bit)
  38.   cc.dcb.Parity:=Parity; // Parity 为 None
  39.   cc.dcb.StopBits:=StopBits; // 1 个Stop bit
  40.   
  41.   if not SetCommState(hComm, cc.dcb) then begin// 设置COM 的状态
  42.     result:='SetCommState Error!';
  43.     CloseHandle(hComm);
  44.     exit;
  45.   end;
  46. end;
  47. //===============================================================================================
  48. // 语法格式:Send(str:String)
  49. // 实现功能:发送数据
  50. // 参数:str,数据
  51. // 返回值:无
  52. //===============================================================================================
  53. procedure TCE_Series.Send(str:String);
  54. var
  55.   lrc:LongWord;
  56. begin
  57.   if (hComm=0) then exit; //检查Handle值
  58.   WriteFile(hComm,str,Length(str), lrc, nil); // 送出数据
  59. end;
  60. //=====================================================================
  61. //语法格式: Receive()
  62. //实现功能: 接收串口数据
  63. //参数:     无
  64. //返回值:   收到的字符串
  65. //=====================================================================
  66. Function TCE_Series.Receive():String;
  67. var
  68.   inbuff: array[0..2047] of Char;
  69.   nBytesRead, dwError:LongWORD ;
  70.   cs:TCOMSTAT;
  71. begin
  72.    ClearCommError(hComm,dwError,@CS);  //取得状态
  73.        // 数据是否大于我们所准备的Buffer
  74.    if cs.cbInQue > sizeof(inbuff) then begin
  75.      PurgeComm(hComm, PURGE_RXCLEAR);  // 清除COM 数据
  76.      exit;
  77.    end;
  78.    ReadFile(hComm, inbuff,cs.cbInQue,nBytesRead,nil); // 接收COM 的数据
  79.    //转移数据到变量中
  80.    result:=Copy(inbuff,1,cs.cbInQue);//返回数据
  81. end;                             
  82. //=====================================================================
  83. //语法格式: ClosePort()
  84. //实现功能:关闭串口
  85. //参数:  无
  86. //返回值:  无
  87. //=====================================================================
  88. procedure TCE_Series.ClosePort();
  89. begin
  90.    SetCommMask(hcomm,$0);
  91.    CloseHandle(hComm);
  92. end;
  93. end.
复制代码
回复

使用道具 举报

该用户从未签到

发表于 2010-12-15 12:15:38 | 显示全部楼层
可以操作打印机了
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-1-2 20:42:08 | 显示全部楼层
hComm:=CreateFile(port, GENERIC_READ or GENERIC_WRITE,0, nil, OPEN_EXISTING, 0, 0);                      // 打开串口  
提示错误:
Incompatible type for arg no. 1:Got &quotWideChar",expected &quotChar"

如何解决?我搞单片机的,上位机软件不是很懂
我想先开发WinXP的程序
然后移植到WinCE中
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-1-2 21:40:47 | 显示全部楼层
我在网上找到一段
function TWin32CESerialCom.OpenPort(ComPort:String;BaudRate,ByteSize,Parity,StopBits:integer):String;
var
  cc:TCOMMCONFIG;
{$IFDEF Win32}
  SAnsi:AnsiString;
  PortPCSTR;
{$ENDIF}
{$IFDEF WinCE}
  SWide:WideString;
  PortPCWSTR;
{$ENDIF}

begin
{$IFDEF Win32}
  SAnsi:=ComPort;
  port:=PChar(SAnsi);
{$ENDIF}
{$IFDEF WinCE}
  SWide:=ComPort;
  port:=PWideChar(SWide);
{$ENDIF}

Win32时,Port定义为LPCSTR;
WinCE时,Port定义为LPCWSTR;
我将函数定义为
Function OpenPort(PortPCSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
编译通过
回复 支持 反对

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2025-5-2 22:57 , Processed in 0.027697 second(s), 10 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表