api操作串口通訊問題????(200分)

  • 主题发起人 主题发起人 lance0909
  • 开始时间 开始时间
L

lance0909

Unregistered / Unconfirmed
GUEST, unregistred user!
哪位高手能給我看看,操作串口通訊的源碼

分一定加上,不夠可以再加[8D]
 
Hubdog的葵花宝典里面有好几篇这类的文章。
主要是使用CreateFile打开串口,ReadFile和WriteFile操作串口。
 
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#pragma hdrstop

#include "serials.h"
#include "pcomm.h"
#pragma package(smart_init)

#ifndef min(a,b)
#define min(a,b) (a>b?b:a)
#endif


#define B50 0x00
#define B75 0x01
#define B110 0x02
#define B134 0x03
#define B150 0x04
#define B300 0x05
#define B600 0x06
#define B1200 0x07
#define B1800 0x08
#define B2400 0x09
#define B4800 0x0A
#define B7200 0x0B
#define B9600 0x0C
#define B19200 0x0D
#define B38400 0x0E
#define B57600 0x0F
#define B115200 0x10
#define B230400 0x11
#define B460800 0x12
#define B921600 0x13

/*
int iRtn = sio_read( (int)ucPort,(char*)&amp;ucTmp,i_Len);
iRtn = sio_write(ucPort,"S",1);
sio_close(uc);
iRtn=sio_open(uc);
iRtn=sio_ioctl(uc,(int)iBaudrate,0x03);
if(iRtn != 0)

*/


__fastcall Comm::Comm(bool CreateSuspended, String ComName,int baudrate,int bufsize)
: TThread(CreateSuspended)
{
//initializing Ttyinfo;

Name=ComName;
Com_Attribute=1;
Dial_Flag=0;
iCom_Rate=baudrate;
NPTTYINFO npTTYInfo=&amp;TtyInfo;

Buf=NULL;
COMDEV( npTTYInfo ) = 0 ;
CONNECTED( npTTYInfo ) = FALSE ;
LOCALECHO( npTTYInfo ) = FALSE ;
AUTOWRAP( npTTYInfo ) = TRUE ;
PORT( npTTYInfo ) = 1 ;

BYTESIZE( npTTYInfo ) = 8 ;
FLOWCTRL( npTTYInfo ) = FC_RTSCTS;
PARITY( npTTYInfo ) = NOPARITY ;
STOPBITS( npTTYInfo ) = ONESTOPBIT ;
XONXOFF( npTTYInfo ) = FALSE ;
XSIZE( npTTYInfo ) = 0 ;
YSIZE( npTTYInfo ) = 0 ;
XSCROLL( npTTYInfo ) = 0 ;
YSCROLL( npTTYInfo ) = 0 ;
XOFFSET( npTTYInfo ) = 0 ;
YOFFSET( npTTYInfo ) = 0 ;
COLUMN( npTTYInfo ) = 0 ;
ROW( npTTYInfo ) = 0 ;
USECNRECEIVE( npTTYInfo ) = TRUE ;
DISPLAYERRORS( npTTYInfo ) = TRUE ;
WRITE_OS( npTTYInfo ).Offset = 0 ;
WRITE_OS( npTTYInfo ).OffsetHigh = 0 ;
READ_OS( npTTYInfo ).Offset = 0 ;
READ_OS( npTTYInfo ).OffsetHigh = 0 ;

THREADID(npTTYInfo)=0;
Buf=new TQuene(bufsize);

//FDM
String s = Name.SubString(4,Name.Length()-3);
int iport = StrToInt(s);
int iRtn=sio_open(iport);
if(iRtn != 0)
{
EOutOfResources *o=new EOutOfResources("不能打开"+Name);
throw *o;
delete o;
}

switch(baudrate)
{
case 4800:
BAUDRATE( npTTYInfo ) = B4800;
break;
case 9600:
BAUDRATE( npTTYInfo ) = B9600;
break;
case 19200:
BAUDRATE( npTTYInfo ) = B19200;
break;
case 38400:
BAUDRATE( npTTYInfo ) = B38400;
break;
default:
BAUDRATE( npTTYInfo ) = B9600;
}

iRtn=sio_ioctl(iport,(int)BAUDRATE( npTTYInfo ),0x03);
if(iRtn != 0)
{
EOutOfResources *o=new EOutOfResources("不能设置"+Name);
throw *o;
delete o;
}

CONNECTED(npTTYInfo)=TRUE;
}
//---------------------------------------------------------------------------

void __fastcall Comm::Execute()
{
NPTTYINFO npTTYInfo =&amp;(this->TtyInfo) ;
int nLength ;
BYTE abIN[ MAXBLOCK + 1] ;

while ( CONNECTED( npTTYInfo ) )
{
if ((nLength = this->ReadCommBlock((LPSTR) abIN, MAXBLOCK))!=0)
{
this->Buf->put((char*)abIN,nLength);
}
Sleep(50);
int j=0;
if (Dial_Flag==1)
{
DialOn();
for (j=0;j<60;j++)
{
Sleep(500);
if (Dial_Flag==2)
break;
}
if (j==60) Dial_Flag=0;
}
if (Dial_Flag==2)
{
HangUp();
}
}
THREADID( npTTYInfo ) = 0 ;
HTHREAD( npTTYInfo ) = NULL ;

return;
}

int Comm::ReadCommBlock( LPSTR lpszBlock, int nMaxLength )
{
int iReadLength ;
String s = Name.SubString(4,Name.Length()-3);
int iport = StrToInt(s);
iReadLength = sio_read( iport,(char*)lpszBlock,nMaxLength);
return ( iReadLength ) ;
}

BOOL Comm::WriteCommBlock( LPSTR lpByte , DWORD dwBytesToWrite)
{
if(Dial_Flag != 0) return (TRUE);

String s = Name.SubString(4,Name.Length()-3);
int iport = StrToInt(s);
int iRtn = sio_write(iport,(char*)lpByte,dwBytesToWrite);
return ( TRUE ) ;
}

int Comm::WriteCommBlock1( LPSTR lpByte , DWORD dwBytesToWrite)
{
String s = Name.SubString(4,Name.Length()-3);
int iport = StrToInt(s);
int iRtn = sio_write(iport,(char*)lpByte,dwBytesToWrite);
return ( iRtn ) ;
}

Comm::FlushCom()
{
String s = Name.SubString(4,Name.Length()-3);
int iport = StrToInt(s);
sio_AbortWrite(iport);
sio_flush(iport,1);
}

__fastcall Comm :: ~Comm( )
{

NPTTYINFO npTTYInfo=&amp;TtyInfo ;
CONNECTED( npTTYInfo ) = FALSE ;
String s = Name.SubString(4,Name.Length()-3);
int iport = StrToInt(s);
sio_close(iport);

if(Buf!=NULL)
{
delete Buf;
Buf=NULL;
}
return ;

}// end of CloseConnection()

Comm::DialOn()
{
//TODO: Add your source code here
char tmp_command[40];
char m_tmp_command[40];
//NPTTYINFO npTTYInfo=&amp;TtyInfo;
//PurgeComm( COMDEV( npTTYInfo ), PURGE_TXABORT | PURGE_RXABORT |
// PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
FlushCom();
sprintf(m_tmp_command,"/r/n");
WriteCommBlock1(m_tmp_command,strlen(m_tmp_command));
Resume();
sprintf(tmp_command,"AT&amp;K0&amp;D0/r");
WriteCommBlock1(tmp_command,strlen(tmp_command));
Sleep(200);
sprintf(tmp_command,"ATDT %s/r",Dial_Number);
WriteCommBlock1(tmp_command,strlen(tmp_command));
Sleep(2);
}
Comm::HangUp()
{
//TODO: Add your source code here
char tmp_command[40];
sprintf(tmp_command,"+++");
WriteCommBlock1(tmp_command,strlen(tmp_command));
Sleep(2000);
sprintf(tmp_command,"ATH/r");
WriteCommBlock1(tmp_command,strlen(tmp_command));
Sleep(1000);
Dial_Flag=0;
}
 
下载一个COMPORT控件,然后看代码,它的代码很通俗易懂
 
给你一个串口监听的例子
注意CreateFile,ReadFile,WriteFIle,CloseHandle
就是串口操作的Api。实际上串口就是对文件COM1,COM2.....
文件的操作
 
Delphi串口通讯的监听

2001-06-25· ·aizb··天极论坛



  串口程序我后来研究了好久,写了下面的代码,后台生成一个线程监听串口,不影响前台工作。效果很好,一直用于GPS仪器的数据接收。

unit frmComm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls,GeoUtils,GeoGPS;
const MAXBLOCK = 160;
type
TComm = record
idComDev : THandle;
fConnected : Boolean;
end;
TCommForm = class(TForm)
ComboBox1: TComboBox;
Button1: TButton;
StatusBar1: TStatusBar;
Button2: TButton;
ComboBox2: TComboBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
TCommThread = Class(TThread)
protected
procedure Execute;override;
public
constructor Create;
end;
var
CommForm: TCommForm;
CommHandle : THandle;
Connected : Boolean;
CommThread : TCommThread;
implementation
{$R *.DFM}
uses
frmMain,frmMdiMapView;
procedure TCommThread.Execute;
var
dwErrorFlags,dwLength : DWORD;
ComStat : PComStat;
fReadStat : Boolean;
InChar : Char;
AbIn : String;
XX,YY : double; file://经度、纬度
VID : string; file://车号
begin
while Connected do begin
GetMem(ComStat,SizeOf(TComStat));
ClearCommError(CommHandle, dwErrorFlags, ComStat);
if (dwErrorFlags > 0) then begin
PurgeComm(CommHandle,(PURGE_RXABORT and PURGE_RXCLEAR));
// return 0;
end;
dwLength := ComStat.cbInQue;
if (dwLength>0) then begin
fReadStat := ReadFile(CommHandle, InChar, 1,dwLength, nil);
if (fReadStat) then begin
if (InChar <> Chr(13)) and (Length(abIn) < MAXBLOCK+5 ) then AbIn := AbIn + InChar
else begin
...
{接收完毕,}
end;//if (fReadStat>0){
end; file://if (dwLength>0){
FreeMem(ComStat);
end;{while}
end;
constructor TCommThread.Create;
begin
FreeOnTerminate := TRUE;
inherited Create(FALSE); file://Createsuspended = false
end;
//
procedure TCommForm.Button1Click(Sender: TObject);
var
CommTimeOut : TCOMMTIMEOUTS;
DCB : TDCB;
fRetVal : Boolean;
begin
StatusBar1.SimpleText := '连接中...';
CommHandle := CreateFile(PChar(ComboBox1.Text),GENERIC_READ,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL
, 0);
if CommHandle = INVALID_HANDLE_VALUE then begin
StatusBar1.SimpleText := '连接失败';
Exit;
end;
StatusBar1.SimpleText := '已同端口 '+ ComboBox1.Text + ' 连接!';
CommTimeOut.ReadIntervalTimeout := MAXDWORD;
CommTimeOut.ReadTotalTimeoutMultiplier := 0;
CommTimeOut.ReadTotalTimeoutConstant := 0;
SetCommTimeouts(CommHandle, CommTimeOut);
GetCommState(CommHandle,DCB);
DCB.BaudRate := 9600;
DCB.ByteSize := 8;
DCB.Parity := NOPARITY;
DCB.StopBits := ONESTOPBIT;
fRetVal := SetCommState(CommHandle, DCB);
if (fRetVal) then begin
Connected := TRUE;
try
CommThread := TCommThread.Create;
except
Connected := FALSE;
CloseHandle(CommHandle);
fRetVal := FALSE;
StatusBar1.SimpleText := '线程建立失败';
Exit;
end;
end
else begin
Connected := FALSE;
CloseHandle(CommHandle);
end;
end;
procedure TCommForm.Button2Click(Sender: TObject);
begin
Connected := FALSE;
CloseHandle(CommHandle);
{终止线程}
CommThread.Terminate;
StatusBar1.SimpleText := '关闭端口'+ComboBox1.Text;
end;
procedure TCommForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Connected := FALSE;
CloseHandle(CommHandle);
StatusBar1.SimpleText := '关闭端口'+ComboBox1.Text;
end;
end.
 
unit Comm;
interface
uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls;
type
TCmdMode = (cmStr, cmBytes);
TComm = class(TGraphicControl)
private { Private declarations }
FPort : string;
FBaudRate: Word; { Baudrate at which runing }
FByteSize: Byte; { Number of bits/byte, 4-8 }
FParity: Byte; { 0-4=None,Odd,Even,Mark,Space }
FStopBits: Byte; { 0,1,2 = 1, 1.5, 2 }

FWaitByteNum : word;
FTimeOut : word;
FMode : TCmdMode;
ColorSet : array [0..3] of TColor;
FCmdStr : string; { Communicate-relate varibles }
State : integer;
dcb : TDCB;
CommBeginTime : TDateTime;
Timer1 : TTimer; { NotifyEvents }
FOnDataLoad : TNotifyEvent;
FOnTimeOut : TNotifyEvent;
procedure CommQuery(Sender : TObject);
procedure LoadData;
procedure SendCmd;
procedure SendStrCmd;

procedure SendBytesCmd;
procedure SetByteNum(val : word);
procedure DecodeCmd(str1 : string;
var char1 : array of char);
protected { Protected declarations }
procedure Paint; override;
public { Public declarations }
hCommDev : integer; { Memory Pool }
connected, WaitOn : boolean;
stat : TComStat;
CmdChar : array[0..64] of Char;
SendLen : word;
pool : array [0..2048] of char;
ms : TMemoryStream;
constructor Create(AOwner : TComponent); override;

procedure Connect;
procedure Excute;
function GetData(Offset : word) : Char;
procedure ClearSigns;
procedure Free;
procedure HardWait;
procedure Query;
published { Published declarations }
property BaudRate : word read FBaudRate write FBaudRate;
property Parity : byte read FParity write FParity;
property ByteSize : byte read FByteSize write FByteSize;
property StopBits : byte read FStopBits write FStopBits;
property CmdStr : string read FCmdStr write FCmdStr;
property WaitByteNum : word read FWaitByteNum write SetByteNum;

property Port : string read FPort write FPort;
property TimeOut : word read FTimeOut write FTimeOut;
property OnTimeOut : TNotifyEvent read FOnTimeOut write FOnTimeOut; property OnDataLoad : TNotifyEvent read FOnDataLoad write FOnDataLoad; property OnClick; property ShowHint; property OnMouseDown; property Mode : TCmdMode read FMode write FMode; end; procedure Register; implementation procedure Register; begin RegisterComponents('Samples', [TComm]); end; constructor TComm.Create(AOwner : TComponent); begin inherited Create(AOwner); ControlStyle := ControlStyle + [csFramed]; FPort := 'COM1'; FBaudRate := 9600; FByteSize := 8; FStopBits := 0; FParity := 0; FTimeOut := 7; Width := 20; Height := 20; WaitOn := False; Connected := False; State := 0; Hint := '空闲'; ShowHint := True; ColorSet[0] := clBlue; ColorSet[1] := clYellow; ColorSet[2] := clOlive; ColorSet[3] := clMaroon; { Create Memory Stream } ms := TMemoryStream.Create; ms.SetSize(1); FWaitByteNum := 1; { Create a Timer } Timer1 := TTimer.Create(self); Timer1.Interval := 100;

Timer1.OnTimer := CommQuery;
end;
procedure TComm.Paint;
var
rGraph : TRect;
begin
with Canvas do
begin
rGraph := Rect(1, 1, Width - 1, Height - 1);
Pen.Color := clBlack;
MoveTo(rGraph.Right, rGraph.Top);
LineTo(rGraph.Left, rGraph.Top);
LineTo(rGraph.Left, rGraph.Bottom);
Pen.Color := clWhite;
LineTo(rGraph.Right, rGraph.Bottom);
LineTo(rGraph.Right, rGraph.Top);
Brush.Color := ColorSet[State];
Pen.Color := clSilver;

InflateRect(rGraph, -3, -3);
Ellipse(rGraph.Left, rGraph.Top, rGraph.Right, rGraph.Bottom);
end;
end;
procedure TComm.SetByteNum(val : word);
begin
FWaitByteNum := val;
ms.Clear;
ms.SetSize(val);
end;
procedure TComm.Connect;
var
PortChar : array[0..12] of Char;
Label ret1;
begin
Connected := False; { Initialize the Communication Port }
StrPCopy(PortChar, FPort);
hCommDev := OpenComm(PortChar, 8192, 2048);
if hCommDev < 0 then goto ret1;

GetCommState(hCommDev, dcb);
dcb.BaudRate := FBaudRate;
dcb.ByteSize := FByteSize;
dcb.Parity := FParity;
dcb.StopBits := FStopBits;
if SetCommState( dcb ) < 0 then begin
CloseComm(hCommDev);
goto ret1;
end;
EscapeCommFunction( hCommDev, SETDTR );
Connected := True;
ret1:
end;
procedure TComm.DecodeCmd( str1 : string; var char1 : array of char);
var
i, j : integer;
btstr : string;
bytebegin : boolean;
begin
if str1[1] = '$' then

begin
i := 1; j := 0; btstr := ''; bytebegin := false;
while (i<=Length(str1)) do
begin
case str1 of
'0'..'9', 'a'..'f', 'A'..'F' :
begin
if not bytebegin then bytebegin := true;
btstr := btstr + str1;
end;
' ' : begin
if bytebegin then
begin
btstr := '$'+btstr;
char1[j] := Chr(StrToInt(btstr));
j := j + 1; bytebegin := false; btstr := '';
end;
end;

end;
i := i + 1;
end;
if bytebegin then
begin
btstr := '$'+btstr;
char1[j] := Chr(StrToInt(btstr));
j := j + 1; bytebegin := false; btstr := '';
end;
char1[j] := Chr(0);
SendLen := j;
end
else begin
StrPCopy(Addr(char1), str1);
SendLen := Length(str1);
end;
end;
procedure TComm.SendCmd;
begin
case FMode of
cmStr : SendStrCmd;
cmBytes : SendBytesCmd;
end;
end;
procedure TComm.SendBytesCmd;
begin
State := 1; Hint := FPort+'-等待';

Refresh; WaitOn := False;
if not Connected then Connect;
if Connected then begin
FlushComm(hCommDev, 0);
FlushComm(hCommDev, 1);
FillChar(pool, 32, 0);
WriteComm(hCommDev, CmdChar, SendLen);
CmdStr := '';
FillChar(CmdChar, 32, 0);
WaitOn := True;
CommBeginTime := Now;
end
else begin
State := 3;
Hint := FPort+'-错误';
Invalidate;
end; end;
procedure TComm.SendStrCmd;
begin
DecodeCmd(CmdStr, CmdChar);

State := 1; Hint := FPort+'-等待';
Refresh; WaitOn := False;
if not Connected then Connect;
if Connected then begin
FlushComm(hCommDev, 0);
FlushComm(hCommDev, 1);
FillChar(pool, 32, 0);
WriteComm(hCommDev, CmdChar, SendLen);
CmdStr := '';
FillChar(CmdChar, 32, 0);
WaitOn := True;
CommBeginTime := Now;
end else begin
State := 3; Hint := FPort+'-错误';
Invalidate;
end; end;
procedure TComm.ClearSigns;

begin
ReadComm(hCommDev, pool, stat.cbInQue);
pool[stat.cbInQue] := #0;
if WaitOn then begin
State := 2; Hint := FPort+'-超时';
Refresh; WaitOn := False;
end;
CommBeginTime := Now;
FlushComm(hCommDev, 0);
FlushComm(hCommDev, 1);
end;
procedure TComm.LoadData;
begin
ReadComm(hCommDev, pool, stat.cbInQue);
pool[stat.cbInQue] := #0;
ms.Seek(0,0);
ms.Write(pool, FWaitByteNum);
State := 0; Hint := FPort+'-空闲';
Refresh; WaitOn := False;

end;
procedure TComm.HardWait;
begin
while Connected and WaitOn do begin
Query;
end; end;
procedure TComm.CommQuery(Sender : TObject);
begin
Query;
end;
procedure TComm.Query;
var Hour, Min, Sec, MSec : Word;
begin
if Connected and WaitOn and (FWaitByteNum > 0) then
begin
GetCommError(hCommDev, stat);
if stat.cbInQue >= FWaitByteNum then begin
LoadData;
if Assigned(FOnDataLoad) then FOnDataLoad(self);
end
else begin
DecodeTime(Now-CommBeginTime, Hour, Min, Sec, MSec); { Communication Timeout Falure }

if (Sec > FTimeOut) or((FTimeOut = 0) and (MSec > 500)) then begin
ClearSigns;
if Assigned(FOnTimeOut) then FOnTimeOut(self);
end;
end;
end;
end;
procedure TComm.Excute;
begin
if not WaitOn then SendCmd;
end;
procedure TComm.Free;
begin
if Connected then begin Connected := False; ClearSigns; CloseComm(hCommDev);
end; end;
function TComm.GetData(Offset : word) : Char;
begin
if Offset <= FWaitByteNum then begin Result := pool[Offset];

end; end;
end. --
///////////////////////////////////////////////
procedure TForm1.Button1Click(Sender: TObject);
var
lpszNumeCom : PChar;
dwAccessType : DWORD;
dwCreationType : DWORD;
creationDCB : TDCB;
hPort : THandle;
buffer : array[0..55] of char;
actRead : integer;
begin
{Some initial settings: I am working with COM2 in read/write}
lpszNumeCom := '//./COM2';
dwAccessType := GENERIC_READ or GENERIC_WRITE;
dwCreationType := OPEN_EXISTING;
hPort := CreateFile(lpszNumeCom,dwAccessType,0,Nil,dwCreationType,0,0);

{here I have the handle for the COM port}
try
if hPort <> INVALID_HANDLE_VALUE then
begin
{Settings for COM : 600bauds, 8,E,1}
GetCommState(hport,creationDCB);
creationDCB.BaudRate := 600;
creationDCB.Parity := EVENPARITY;
creationDCB.ByteSize := 8;
creationDCB.StopBits := ONESTOPBIT;
{Settings for the control type, here I am reading from a Proximity
card
reader and use only the RX line}
creationDCB.Flags := creationDCB.Flags + DTR_CONTROL_DISABLE
+ RTS_CONTROL_DISABLE;
if SetCommState(hPort,creationDCB) then
begin
PurgeComm(hPort,PURGE_RXCLEAR);
{Here I actually can read data from COM}
{The next function call will not return until it actually reads
data
from the serial port}
ReadFile(hPort,buffer,56,actRead,nil);
PurgeComm(hPort,PURGE_RXCLEAR);
{in buffer you have data readed from the port}
begin
{Do something usefull with data}
end
end
end;
finally
CloseHandle(hPort);
end;
end;


fUnit: DWORD; { Unit descriptor handle}
fUnitDescriptor: string; { Unit descriptor Pascal string }

fUnitDescriptor := 'COM2';

procedure TRs232.OpenPort(descriptor: PChar);
begin
fUnit := CreateFile(descriptor, GENERIC_READ or GENERIC_WRITE,
0,nil,OPEN_EXISTING,0,0);
end;

function TRs232.GetOpenPortError: BOOLEAN;
var
isOK: BOOLEAN;
Error: DWORD;

begin
isOK:=(fUnit <> INVALID_HANDLE_VALUE); //DWORD(-1)
if not isOK then
begin
Error := GetLastError;
{case fUnit of}
case Error of
IE_BADID: ShowMessage('"' + fUnitDescriptor + '" invalid or unsupported');
IE_BAUDRATE: ShowMessage('"' + fUnitDescriptor + '" baudrate unsupported');
IE_BYTESIZE: ShowMessage('Specified bytesize is invalid');
IE_DEFAULT: ShowMessage('Default parameters are in error');
IE_HARDWARE: ShowMessage('"' + fUnitDescriptor + '" not available');
IE_MEMORY: ShowMessage('"' + fUnitDescriptor + '" - unable to allocate queues');
IE_NOPEN: ShowMessage('"' + fUnitDescriptor + '" is not open');
IE_OPEN: ShowMessage('"' + fUnitDescriptor + '" is already open');
else
{ShowMessage('Note: "' + fUnitDescriptor + '" returned error ' +
IntToStr(Error)); }
end;
end;
Result:=isOK;
end;



function TRs232.Output(lpBuffer: PChar): BOOLEAN;
var
lpBytesSent,nBufferSize: LongWord;

begin
nBufferSize := StrLen(lpBuffer);
Result := WriteFile(fUnit, //handle to file to write to
lpBuffer, // pointer to data to write to file
nBufferSize, // number of bytes to write
lpBytesSent, // pointer to number of bytes written
nil); // pointer to structure needed for overlapped IO

end;

function TRs232.Enter(nBytesToRead: LongWord; lpString: PChar; var
lpBytesRead: LongWord): BOOLEAN;

begin

Result := ReadFile(fUnit,
lpString,
nBytesToRead,
lpBytesRead,
nil);
end;

destructor TRs232.Destroy;
begin
if not CloseHandle(fUnit) then
ShowMessage('Error closing port "' + fUnitDescriptor + '"');
inherited Destroy;
end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部