100分求串口通信的教材和例子(急,分不够可以再加)(100分)

  • 主题发起人 主题发起人 AsleepAnge
  • 开始时间 开始时间
A

AsleepAnge

Unregistered / Unconfirmed
GUEST, unregistred user!
具体是想用COM实现与硬件接口的通信,如接受硬件发来的数据包,和给硬件发送数据包.

希望大侠们多加指点,感激不尽~~~
 
你可以参看“串口调试助手”的DELPHI例子。
非常经典。在Playicq有下。

 
netfun2000大大,可以说详细点吗?我没找到昂~
 
http://new.playicq.com/dispdocnew.php?id=4475
资源名称 串口调试助手源程序
作者: 未知
开发语言: Visual Basic
文件大小 99.770Kb
运行平台:
上传者:
上传日期: 2003-04-27 08:28:23
下载次数: 16
点击: 657
推荐等级
简介 包含串口调试助手源程序V1.0详细编程说明及源码.
 
谢谢大大们,接分了
 
资源名称 九品御厨作品-串口通讯控制器V1.18
作者: 九品御厨
开发语言: Delphi & Kylix
文件大小 294.395Kb
运行平台: Windows Server 2003 Windows 2000 Windows XP Windows 9*/ME Windows NT
上传者: okook007
上传日期: 2004-01-05 17:09:12
下载次数: 71
点击: 593
推荐等级
简介 大名顶顶的九品御厨作品-串口通讯控制器

带有全部delphi源码,做串口开发朋友参考和应用的好东东!
使用的是专业通讯控件:TurboPower Async Professional;
http://kola100.6to23.com
 
是个论坛啊,没有下载昂~
 
我有个调试的源程序,可是却怎么也修改不对,老是出错,还请帮忙
 
我一直使用spcomm做串口通讯。
可以到qq:93882205上找我呀。
 
abc516你什么时候在呀?我很急的
 
我的一个类,供参考,请别直接应用于商业,谢谢。
我用API函数,通讯部分是控制具有RS232的通讯卡

{*******************************************************}
{ }
{ 串口API定义单元 }
{ }
{ Copyright (c) 2004 }
{ Date: 2004-4-10 }
{ Check: 2004-4-21 }
{ Author: muhx }
{ }
{*******************************************************}

unit COMDefineUnit;

interface

uses Windows,SysUtils;

type
TComPort = Class
private
m_TimeOut: COMMTIMEOUTS;
m_iOutQueue: Integer;
m_iInQueue: Integer;
m_iEventMask: 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
m_sCom: string;
m_iParity: Integer;
m_iBaud: Integer;
m_iStopBits: Integer;
m_iByteSize: Integer;
m_hSerial: THANDLE;
end;
{ TComPort }

implementation


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

m_iInQueue := 4096;
m_iOutQueue := 4096; //quene buffer length
m_iEventMask := EV_RXCHAR; //Every char

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

m_sCom := UpperCase(strCom);
end;

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

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

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

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

//ini comport
SetCommMask(m_hSerial,m_iEventMask);

SetCommTimeouts(m_hSerial,m_TimeOut);

SetupComm(m_hSerial,m_iInQueue,m_iOutQueue); //The size of buffer

d.DCBlength := sizeof(DCB);
GetCommState(m_hSerial,d);
d.ByteSize := m_iByteSize; // data size, xmit, and rcv
d.StopBits := m_iStopBits; // one stop bit
d.BaudRate := m_iBaud; // set the baud rate
d.Parity := m_iParity; // no parity bit

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

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

//读取数据
function TComPort.ReadData(p: PChar; Length: Integer): Boolean;
var
o: TOverLapped;
lReadNum: DWORD;
begin
if m_hSerial = 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(m_hSerial,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(m_hSerial,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(m_hSerial,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 m_hSerial = 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(m_hSerial,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(m_hSerial,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(m_hSerial,PURGE_RXCLEAR or PURGE_TXCLEAR);//clear buffer
Result := True;
end;

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

m_iParity := nParity;
m_iBaud := nBaud;

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

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

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

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

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

//设置DCB属性
procedure TComPort.SetComPara(iParity, iStopBits, iByteSize,
iBaudRate: Integer);
begin
m_iParity := iParity;
m_iBaud := iBaudRate;
m_iStopBits := iStopBits;
m_iByteSize := iByteSize;
end;

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

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

end.

 
对呀
楼上可以加下兄弟我的QQ:254734866
 
用MSComm(VB自带的ActivX)我一直用,好用的很.不过在分发时记得将MSComm一起打包呀!
 
怎么有人说spcomm好用,有人说Mscomm好用,到底哪个对初学者最容易上手?
 
come later!!
 
自己写的最好用!!!!!
 
你水平比较好用spcomm,否则mscom,你看spcomm代码就知道怎样作了。已经很多了!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部