unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, MSCommLib_TLB;
type
TForm1 = class(TForm)
Comm: TMSComm;
procedure CommComm(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
procedure initComm;
end;
TScanComData = class(TThread)
private
protected
procedure Execute; override;
public
constructor Create;
destructor Destroy;override;
end;
var
Form1: TForm1;
ScanComData:TScanComData;
implementation
{$R *.dfm}
{ TScanComData }
constructor TScanComData.Create;
begin
inherited Create(True);
FreeOnTerminate := True;
end;
destructor TScanComData.Destroy;
begin
inherited Destroy;
end;
procedure TScanComData.Execute;
var
ole:Olevariant;
i:Integer;
begin
// while True do
// begin
Form1.initcomm;
Form1.comm.RThreshold:=7;//23;
if Form1.comm.PortOpen=false then
Form1.comm.PortOpen:=true;
Form1.comm.DTREnable:=true;
Form1.comm.RTSEnable:=true;
// ReadComCount:=0;
// bIfSend:=True;
ole:=VarArrayCreate([0 ,7],varByte);
ole[0]:= $02;
ole[1]:= $01;
ole[2]:= $08;
ole[3]:= $00;
ole[4]:= $00;
ole[5]:= $02;
ole[6]:= $00;
ole[7]:=0;
for i := 0 to 6 do
ole[7]:=ole[7] xor ole[i];
// ole[7]:= $09;
// Self.Caption:=IntToStr(ole[7]);
Form1.comm.Output := ole;
// end;
end;
{ TForm1 }
procedure TForm1.CommComm(Sender: TObject);
var
ole:Olevariant;
str:String;
i:Integer;
begin
if (Comm.CommEvent=2)then
begin
// 讀卡號(掃描)
if Comm.RThreshold = 7 then
begin
ole:=Comm.Input;
for i:=0 to Comm.RThreshold - 1 do
begin
str:= str + ',' + IntToHex(ole[i],2);
end;
Self.Caption:= String(str);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ScanComData:= TScanComData.Create;
ScanComData.Resume;
end;
procedure TForm1.initComm;
begin
if comm.PortOpen=true then
comm.PortOpen:=false;
comm.Settings:='38400,n,8,1';
comm.InBufferCount:=0; //清慷訓沖區
comm.OutBufferCount:=0;
comm.InputLen:=0;
comm.CommPort:= 2;
end;
end.
為什么進不去commcomm這個事件呀 應該觸發了呀!
uj5u.com熱心網友回復:
怎么沒人呀?有高手幫忙看看呀!uj5u.com熱心網友回復:
每個函式是干啥用的 你也不注釋一下uj5u.com熱心網友回復:
先不要用執行緒,你這個執行緒也只是讓串口發下資料而已,然后用串口除錯程式看下你發的資料,有沒回傳資料。如果要用執行緒,就把串口全部放到執行緒里操作。在執行緒里動態創建串口,收發資料,然后通過訊息通知主執行緒。
還有串口控制元件建議用CPort、Spcomm
uj5u.com熱心網友回復:
MSComm有一個資料長度的屬性,忘記叫什么名字了,用來觸發Oncomm事件,檢查一下,是否這個屬性設定有問題
uj5u.com熱心網友回復:
建議用spcommuj5u.com熱心網友回復:
uj5u.com熱心網友回復:
BufferLength
檢查一下這個屬性設定,是不是過大
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/103105.html
標籤:VCL組件開發及應用
