VB原码为:
Option Explicit
Dim i As Long, j As Integer
Dim Fdata(36) As ByteDim OperOld As ByteSub SendToCard(Port, ReadD As Integer)
Dim Cache As Variant
Dim LLL As Long
Dim Arr() As Byte
Dim yy
LLL = 0
MSComm1.CommPort = Port
MSComm1.Settings = "1200,n,8,1" '
MSComm1.InputLen = 0
MSComm1.InBufferCount = 0 '
MSComm1.OutBufferCount = 0 '
MSComm1.PortOpen = True
MSComm1.InputMode = comInputModeBinary
Cache = MSComm1.Input '
MSComm1.InputLen = 0
Cache = Fdata
MSComm1.Output = Cache Do
yy = DoEvents()
If MSComm1.InBufferCount = ReadD Then
Cache = MSComm1.Input
Arr = Cache
Exit Do
End If
Loop While (1)End SubPrivate Sub CmdRead_Click()
Fdata(0) = &H11
For i = 1 To 36
Fdata(i) = &H0
Next i
SendToCard 1, 37
End Sub
改为D7使用MSCOMM控件的程序,或其它控件的也行,我这里改的不对.
procedure TMainForm.Button4Click(Sender: TObject);
var
checkid: Byte;
senddata: array[0..36] of Byte;
reData: array of Variant;
sendstr, str: string;
restr: string;
i: longint;
begin
Button4.Enabled := False;
mscomm1.CommPort := 1; //指定端口
mscomm1.Settings := '1200,n,8,1'; //其它参数
//mscomm1.InBufferSize := 1024; //接收缓冲区
mscomm1.OutBufferSize := 512; //发送缓冲区
mscomm1.InputLen := 0; //一次读取所有数据
mscomm1.SThreshold := 0; //一次发送所有数据
mscomm1.InBufferCount := 0; //清空读取缓冲区
mscomm1.OutBufferCount := 0; //清空发送缓冲区
mscomm1.PortOpen := true; //打开端口
mscomm1.InputMode := comInputModeBinary; //接收模式 MSComm1.Interval := 1;
MSComm1.CTSTimeout := 1;
MSComm1.CDTimeout := 1;
MSComm1.DSRTimeout := 1; senddata[0] := $11;
sendstr := ''; for i := 0 to 30 do
senddata[i + 6] := $00; for i := 0 to 33 do
checkid := byte(checkid + senddata[i]); MSComm1.Output := senddata[0];
str := MSComm1.Input;
mscomm1.output := sendstr; //发送数据
i := 0;
bzw := false; repeat
sleep(10);
Application.ProcessMessages;
i := i + 1;
if i > 300 then
begin
showmessage('发送超时!');
break;
end;
until bzw = true;
redata := mscomm1.Input;
restr := '';
for i := 0 to vararrayhighbound(redata, 1) do
restr := restr + inttohex(redata[i], 2) + ' ';
mscomm1.PortOpen := false;
end;
Option Explicit
Dim i As Long, j As Integer
Dim Fdata(36) As ByteDim OperOld As ByteSub SendToCard(Port, ReadD As Integer)
Dim Cache As Variant
Dim LLL As Long
Dim Arr() As Byte
Dim yy
LLL = 0
MSComm1.CommPort = Port
MSComm1.Settings = "1200,n,8,1" '
MSComm1.InputLen = 0
MSComm1.InBufferCount = 0 '
MSComm1.OutBufferCount = 0 '
MSComm1.PortOpen = True
MSComm1.InputMode = comInputModeBinary
Cache = MSComm1.Input '
MSComm1.InputLen = 0
Cache = Fdata
MSComm1.Output = Cache Do
yy = DoEvents()
If MSComm1.InBufferCount = ReadD Then
Cache = MSComm1.Input
Arr = Cache
Exit Do
End If
Loop While (1)End SubPrivate Sub CmdRead_Click()
Fdata(0) = &H11
For i = 1 To 36
Fdata(i) = &H0
Next i
SendToCard 1, 37
End Sub
改为D7使用MSCOMM控件的程序,或其它控件的也行,我这里改的不对.
procedure TMainForm.Button4Click(Sender: TObject);
var
checkid: Byte;
senddata: array[0..36] of Byte;
reData: array of Variant;
sendstr, str: string;
restr: string;
i: longint;
begin
Button4.Enabled := False;
mscomm1.CommPort := 1; //指定端口
mscomm1.Settings := '1200,n,8,1'; //其它参数
//mscomm1.InBufferSize := 1024; //接收缓冲区
mscomm1.OutBufferSize := 512; //发送缓冲区
mscomm1.InputLen := 0; //一次读取所有数据
mscomm1.SThreshold := 0; //一次发送所有数据
mscomm1.InBufferCount := 0; //清空读取缓冲区
mscomm1.OutBufferCount := 0; //清空发送缓冲区
mscomm1.PortOpen := true; //打开端口
mscomm1.InputMode := comInputModeBinary; //接收模式 MSComm1.Interval := 1;
MSComm1.CTSTimeout := 1;
MSComm1.CDTimeout := 1;
MSComm1.DSRTimeout := 1; senddata[0] := $11;
sendstr := ''; for i := 0 to 30 do
senddata[i + 6] := $00; for i := 0 to 33 do
checkid := byte(checkid + senddata[i]); MSComm1.Output := senddata[0];
str := MSComm1.Input;
mscomm1.output := sendstr; //发送数据
i := 0;
bzw := false; repeat
sleep(10);
Application.ProcessMessages;
i := i + 1;
if i > 300 then
begin
showmessage('发送超时!');
break;
end;
until bzw = true;
redata := mscomm1.Input;
restr := '';
for i := 0 to vararrayhighbound(redata, 1) do
restr := restr + inttohex(redata[i], 2) + ' ';
mscomm1.PortOpen := false;
end;
改为 senddata[0] := 17;
---------------------for i := 0 to 30 do
senddata[i + 6] := $00;
改为:
for i := 1 to 36 do
senddata[i] := 0;
-----------------------for i := 0 to 33 do
checkid := byte(checkid + senddata[i]);
这段代码有必要吗?建议去掉
下面这句好像有问题,不过不知道怎么改
MSComm1.Output := senddata[0];
把后面的[0]去了又会报错.
我用的是SPComm,下面是我的打开和发送方法,不知能不能对你有点用打开com口
var
str:string;
begin
str := IntTostr(DM.SysData.InstId);
Result := False;
try
Comm1.CommName := DM.SysData.IniFile.ReadString('portinfo'+str,'portname','COM1');
Comm1.BaudRate := DM.SysData.IniFile.ReadInteger('portinfo'+str,'btl',4800);
Comm1.Parity := TParity(DM.SysData.IniFile.ReadInteger('portinfo'+str,'parity',2));
Comm1.ByteSize := TByteSize(DM.SysData.IniFile.ReadInteger('portinfo'+str,'bytesize',2));
Comm1.StopBits := TStopBits(DM.SysData.IniFile.ReadInteger('portinfo'+str,'stopbits',2));
Comm1.IgnoreNullChar := DM.SysData.IniFile.ReadBool('portinfo'+str,'null',True);
Comm1.ReadIntervalTimeout := DM.SysData.IniFile.ReadInteger('portinfo'+str,'interval',100);
Comm1.StartComm;
Result := True;
except
Result := False;
ShowMessage('串口打开失败');
end;
end;
//发送,str为16进制字符串
procedure TfrmMain.SendString(const str: string);
begin
Comm1.WriteCommData(Pchar(str),Length(str));
end;