中国开发网: 论坛: 程序员情感CBD: 贴子 277875
haitao
有点倾向于api或文件操作方式--spcomm是不是需要一个activex的?
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;
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.
我的blog:http://szhaitao.blog.hexun.com & http://www.hoolee.com/user/haitao
--以上均为泛泛之谈--
不尽牛人滚滚来,无边硬伤纷纷现 人在江湖(出来的),哪能不挨刀(总归是要的)
网络对话,歧义纷生;你以为明白了对方的话,其实呢?

您所在的IP暂时不能使用低版本的QQ,请到:http://im.qq.com/下载安装最新版的QQ,感谢您对QQ的支持和使用

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录