小弟在用DELPHI写串口通讯程序时碰到这样的问题:
程序如下:
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
  Handle:Thandle;
  ErrorInfo:Integer;
  dwInbuffer:integer;
  dwOutbuffer:integer;
  aHandle:Thandle;
const
  SUCCESS=0; //成功
  INVALID_COM_HANDLE=1; 
  INVALID_COM_PORT=2;  
  INVALID_BUFFER_ERROR=4;
  COM_CREATE_ERROR=10;
  COM_CLOSE_ERROR=11;  
  COM_BUFFER_ERROR=12;  
  COM_BUILDDCB_ERROR=13;  
  COM_SETDCB_ERROR=14; 
  COM_GETSTATE_ERROR=15; 
  COM_OTO_ERROR=16;  
  COM_PURGE_ERROR=17;  
  COM_SETMASK_ERROR=18; 
  COM_CLEARERROR_ERROR=19;  
  COM_RECEIVENUM_ERROR=20;  
//下面为所有函数,返回值0表示失败,1表示成功
//******************************************************************
//传口初试化
implementation
{$R *.dfm}
function OpenComm(port:integer;portset:string;inbuffer:DWORD;outbuffer:DWORD;var hfwnd:DWORD):integer;stdcall;
var
  lpdef:Pchar;
  lpdcb:Tdcb;
  comport:String;
  oto:TCommTimeOuts;
begin
//***************判别参数的正常性和合法性****************//  if (port<0) or (port>32767) then
    begin
      ErrorInfo:=INVALID_COM_PORT;
      result:=0;
      exit
    end;
  if (inbuffer<0) or (outbuffer<0) or (inbuffer>32767) or (outbuffer>32767) then
    begin
      ErrorInfo:=INVALID_BUFFER_ERROR;
      result:=0;
      exit
    end;
//*******************************************************//
//获取COM的串口号
  comport:='COM'+inttostr(port); 
//打开串口
  Handle:=createfile(pchar(comport),GENERIC_READ or GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
  hfwnd:=Handle;
//判别串口是否正常打开
  if Handle=INVALID_HANDLE_VALUE then
    begin
      ErrorInfo:=COM_CREATE_ERROR;
      result:=0;
      exit;
    end;
//判别返回的串口句柄是否有效
  if GetFileType(Handle)<>FILE_TYPE_CHAR then
    begin
      CloseHandle(Handle);
      ErrorInfo:=COM_CREATE_ERROR;
      result:=0;
      exit
    end;
  ErrorInfo:=SUCCESS;
  result:=1;
//清空I/O的BUFFER
  if PurgeComm(Handle,PURGE_TXABORT or PURGE_RXABORT or PURGE_TXCLEAR or PURGE_RXCLEAR)=False then
    begin
      CloseHandle(Handle);
      ErrorInfo:=COM_PURGE_ERROR;
      result:=0;
      exit
    end;
//OVERLAPED的读写TimeOut设置
  oto.ReadIntervalTimeout:=100;
  oto.ReadTotalTimeoutMultiplier:= 0;
  oto.ReadTotalTimeoutConstant:= 0;
  oto.WriteTotalTimeoutMultiplier:= 0;
  oto.WriteTotalTimeoutConstant:= 0;
  if setcommtimeouts(Handle,oto)=False then
    begin
      CloseHandle(Handle);
      ErrorInfo:=COM_OTO_ERROR;
      exit
    end;
//设置I/O的BUFFER大小
  if setupcomm(Handle,inbuffer,outbuffer)=False then
    Begin
      CloseHandle(Handle);
      ErrorInfo:=COM_BUFFER_ERROR;
      result:=0;
      exit
    end;
  dwInbuffer:=inbuffer;
  dwOutbuffer:=outbuffer;
//DCB(Data Control Block)的属性设置
  if GetCommState(Handle,lpdcb)=False then
    begin
      CloseHandle(Handle);
      ErrorInfo:=COM_GETSTATE_ERROR;
      result:=0;
      exit
    end;
  //设置端口状态
  lpdef:=pchar(comport+': '+portset);
  lpdcb.Flags:=1;
  if not BuildCommDCB(lpdef,lpdcb) then
    begin
      CloseHandle(Handle);
      ErrorInfo:=COM_BUILDDCB_ERROR;
      exit
    end;
  if not setcommstate(Handle,lpdcb) then
    begin
      CloseHandle(Handle);
      ErrorInfo:=COM_SETDCB_ERROR;
      exit
    end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  OpenComm(1,'9600,n,8,1',1024,124,aHandle);
end;
可是在执行if not setcommstate(Handle,lpdcb) then这句语句时好象返回的值不对,我不太清楚倒底怎么会事情?
还有在DELPHI中的DCB结构好像和VC中的不一样,它有个flags不知道具体什么含义
是不是因为这个才使setcommstate(Handle,lpdcb)失败。
希望各位有惊讶的大侠能帮忙看看谢谢啦!!!