谁能给我一个串口通信的源程序?(100分)

  • 主题发起人 主题发起人 wangyuhu2002
  • 开始时间 开始时间
W

wangyuhu2002

Unregistered / Unconfirmed
GUEST, unregistred user!
本人正在做毕业设计,想用delphi做一个同单片机的串口通信,能把单片机传来的二进制数读出来在pc机上显示,如果各位大侠有这方面的源程序的话!请给我一个,谢谢!
 
顶啊,急用啊
 
使用SPCOMM控件就行了。网上很多资料,看看就可以了
 
我的一个,告诉我mail
 
To wangyuhu2002,
可以加QQ:82780254
 
我的email:wangyuhu2002@126.com
 
太简单了吧.还是自己好好研究一下吧!做毕设还是要自己动手做.不要想着走捷径.这个不难.
他们说得对.用spcomm就很容易实现.顺便看一下网上关于这个控件的介绍.还有例程.例程里面有些错误,感觉像是专门改了一下.这个在这个在bbs里面有很多人都在问这个问题.看一下回复就可以了.
 
这只是我毕设的一小部分了
我就想找个例子来看看
 
to lt66
暂时没收到
 
To wangyuhu2002,
你去找SPPCOM 控件吧,这个入门容易
 
老人家
没加我qq 啊
 
[:D] 收到了!
等我看看先
 
刚刚作完这个东东(spcomm)
如果有什么问题
可以MAIL我
whbfeng@163.com
 
我的598588189帮帮了啊,呵呵
 
to wangyuhu2002
用spcomm这个空间进行串口通uxn问题,源程序在我的笔记中有,请查看我的笔记,后者还有问题请发 信给我,longskyliu@163.com
 
供参考

{*******************************************************}
{ }
{ Newpower Software LIR-256B01 }
{ 串口API定义单元 }
{ }
{ Copyright (c) 2004, Newpower Corporation }
{ Date: 2004-4-10 }
{ Check: 2004-4-21, 2004-7-2 }
{ Author: muhx }
{ }
{*******************************************************}

unit COMDefineUnit;

interface

uses Windows, SysUtils;

type
TComPort = Class
private
FTimeOut: COMMTIMEOUTS;
FiOutQueue: Integer;
FiInQueue: Integer;
FiEventMask: Integer;
public
function CheckConnect: Boolean;
function Connect: Boolean;
function ChangeComSet(nBaud,nParity: Integer): Boolean;
function ReadData(p: PChar; Length: Integer): Boolean;
function WriteData(p: PChar; Length: Integer): Boolean;
function Close: Boolean;
procedure ClearComPort;
procedure SetComPara(iParity,iStopBits,iByteSize,iBaudRate: Integer);
public
constructor Create(strCom: string);
destructor Destroy; override;
private
FsCom: string;
FiParity: Integer;
FiBaud: Integer;
FiStopBits: Integer;
FiByteSize: Integer;
FhSerial: THANDLE;
end;
{ TComPort }

implementation


constructor TComPort.Create(strCom: string);
begin
FhSerial := INVALID_HANDLE_VALUE;

FiInQueue := 4096;
FiOutQueue := 4096; //quene buffer length
FiEventMask := EV_RXCHAR; //Every char

FTimeOut.ReadIntervalTimeout := 50; //the inter time of the two following read char
FTimeOut.ReadTotalTimeoutMultiplier := 5; //
FTimeOut.ReadTotalTimeoutConstant := 100; //当没有字节时立刻返回
FTimeOut.WriteTotalTimeoutMultiplier := 5; //每传输一个字节平均5ms;
FTimeOut.WriteTotalTimeoutConstant := 50; //5000//传输常量=50ms;

FsCom := UpperCase(strCom);
end;

//检查串口是否已经连接,若连接返回True,否则连接
function TComPort.CheckConnect: Boolean;
begin
if FhSerial <> INVALID_HANDLE_VALUE then
begin
Result := True;
Exit;
end;
Result := Connect;
end;

//打开串口
function TComPort.Connect: Boolean;
var
d: DCB;
begin
if FhSerial <> INVALID_HANDLE_VALUE then
CloseHandle(FhSerial);

FhSerial := CreateFile(PChar(FsCom), GENERIC_READ or GENERIC_WRITE,
0, nil, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, // overlapped I/O
0);

if FhSerial = INVALID_HANDLE_VALUE then
begin
Result := False;
Exit;
end;

//ini comport
SetCommMask(FhSerial, FiEventMask);

SetCommTimeouts(FhSerial, FTimeOut);

SetupComm(FhSerial ,FiInQueue, FiOutQueue); //The size of buffer

d.DCBlength := sizeof(DCB);
GetCommState(FhSerial, d);
d.ByteSize := FiByteSize; // data size, xmit, and rcv
d.StopBits := FiStopBits; // one stop bit
d.BaudRate := FiBaud; // set the baud rate
d.Parity := FiParity; // no parity bit

if not SetCommState(FhSerial, d) then
begin
CloseHandle(FhSerial);
Result := False;
Exit;
end;

SetCommMask(FhSerial, EV_RXCHAR);
Result := True;
end;

//读取数据
function TComPort.ReadData(p: PChar; Length: Integer): Boolean;
var
o: TOverLapped;
lReadNum: DWORD;
begin
if FhSerial = INVALID_HANDLE_VALUE then
begin
Result := False;
Exit;
end;

lReadNum := 0;
Fillchar(o, Sizeof(o), 0);
o.hEvent := CreateEvent(nil, True, False, nil);//create an autoret Event object

if not ReadFile(FhSerial, p[0], Length, lReadNum, @o) then
begin
if GetLastError() = ERROR_IO_PENDING then
begin
if WaitForSingleObject(o.HEvent, INFINITE) = WAIT_OBJECT_0 then
begin
if not GetOverlappedResult(FhSerial, o, lReadNum, False) then
begin
Result := False;
Closehandle(o.HEVent);
Exit;
end;
end
else begin
Result := False;
Closehandle(o.HEVent);
Exit;
end;
end;
end;

CloseHandle(o.hEvent);
PurgeComm(FhSerial, PURGE_RXCLEAR or PURGE_TXCLEAR);//clear buffer
Result := True;
end;

//写入数据
function TComPort.WriteData(p: PChar; Length: Integer): Boolean;
var
o: TOverLapped;
lWriteNum: DWORD;
begin
if FhSerial = INVALID_HANDLE_VALUE then
begin
Result := False;
Exit;
end;

if Length < 0 then
begin
Result := True;
Exit;
end;

lWriteNum := 0;
FillChar(o, Sizeof(o), 0);
o.hEvent := CreateEvent(nil, True, False, nil);//create an autoret Event object

if not WriteFile(FhSerial, p[0], Length, lWriteNum, @o) then
begin
if GetLastError() = ERROR_IO_PENDING then
begin
if WaitForSingleObject(o.HEvent, INFINITE) = WAIT_OBJECT_0 then
begin
if not GetOverlappedResult(FhSerial, o, lWriteNum, False) then
begin
Result := False;
Closehandle(o.HEVent);
Exit;
end;
end
else begin
Result := False;
Closehandle(o.HEVent);
Exit;
end;
end;
end;

CloseHandle(o.hEvent);
PurgeComm(FhSerial, PURGE_RXCLEAR or PURGE_TXCLEAR);//clear buffer
Result := True;
end;

//改变串口状态
function TComPort.ChangeComSet(nBaud, nParity: Integer): Boolean;
var
d: DCB;
begin
if FhSerial = INVALID_HANDLE_VALUE then
begin
Result := False;
Exit;
end;

if not GetCommState(FhSerial, d) then
begin
Close;
Result := False;
Exit;
end;

FiParity := nParity;
FiBaud := nBaud;

d.BaudRate := FiBaud; // set the baud rate
d.ByteSize := 8; // data size, xmit, and rcv
d.Parity := FiParity; // no parity bit
d.StopBits := ONESTOPBIT; // one stop bit

if not SetCommState(FhSerial, d) then
begin
Close;
Result := False;
Exit;
end;

PurgeComm(FhSerial, PURGE_RXCLEAR or PURGE_TXCLEAR); //clear buffer
Result := True;
end;

//清空缓冲区数据
procedure TComPort.ClearComPort;
begin
PurgeComm(FhSerial, PURGE_RXCLEAR); //清除接受缓冲区中的所有数据
PurgeComm(FhSerial, PURGE_TXCLEAR); //清除传送缓冲区中的所有数据
end;

//设置DCB属性
procedure TComPort.SetComPara(iParity, iStopBits, iByteSize,
iBaudRate: Integer);
begin
FiParity := iParity;
FiBaud := iBaudRate;
FiStopBits := iStopBits;
FiByteSize := iByteSize;
end;

//关闭串口
function TComPort.Close: Boolean;
begin
if FhSerial <> INVALID_HANDLE_VALUE then
begin
CloseHandle(FhSerial);
FhSerial := INVALID_HANDLE_VALUE;
end;
Result:=True;
end;

//析构类
destructor TComPort.Destroy;
begin
if FhSerial <> INVALID_HANDLE_VALUE then
CloseHandle(FhSerial);
inherited;
end;

end.
 
可以也给我发一下好不?
我的邮件:ds_info_company@126.com
 
接受答案了.
 
后退
顶部