使用MSCOMM进行串口通讯时,遇到的问题,在线急救! (100分)

  • 主题发起人 主题发起人 ghtghtmalone
  • 开始时间 开始时间
G

ghtghtmalone

Unregistered / Unconfirmed
GUEST, unregistred user!
问题:我从串口发送十六进制串
'3c 0e 01 71 13 90 58 15 08 68 00 00 d1 a8 bd 24 0c 0d'
但我收到的却是十六进制串
'3c 0e 01 71 13 90 58 15 08 68 00 00 d1 a8 3f 0c 0d'
'bd 24'变成了 '3f'

补充说明:如果用串口调试助手测试却发现结果正确。
部分代码如下:
函数说明:
Get_BccEnd(KeyStr):取得串的异或值和结束位'0d' 即得到串:
'3c 0e 01 71 13 90 58 15 08 68 00 00 d1 a8 bd 24 0c 0d' ;
Rf_Key_Down():向串口发送串:
'3c 0e 01 71 13 90 58 15 08 68 00 00 d1 a8 bd 24 0c 0d' ;


procedure TFrm_Key_Down.BTn_Load_KeyClick(Sender: TObject);
Var
iPOrt_RetState:integer;
KeyStr:Widestring;
Begin
Edit1.text:='139058150868';
KeyStr:='3c0e0171'+Edit1.Text+'0000'+'d1a8bd24';
Keystr:=get_BccEnd(Keystr);
iPOrt_RetState:=Rf_Key_Down(amsComm[Int_Com],keyStr);
End;



Function Get_BccEnd(BccStr:String):String;
Var
Xor_Int,i:Integer;
Bcc_Bit:String;
Begin
Xor_Int:=HexToStr(Copy(BccStr,1,2)) Xor HexToStr(Copy(BccStr,3,2));
fOR I:=3 TO Trunc(length(BccStr)/2) DO
Xor_Int:=xor_Int xor HexToStr(Copy(BccStr,2*i-1,2));
Bcc_Bit:=Inttohex(Xor_int,2);
If Length(Bcc_Bit)=1 Then
Bcc_Bit:='0'+Bcc_Bit;
Result:=BccStr+Bcc_Bit+'0D';
End;

Function Rf_Key_Down(MsComm:TMsComm;Var keyStr:Widestring):Integer;
Var
iPOrt_RetState,i:integer;
Hstr:string;
Begin
CurMSComm:=MSComm;
Hstr:=KeyStr;
For i:=1 to trunc((length(Hstr))/2) Do
Begin
Query_Card:=Chr(StrToIntDef('$'+Copy(HStr,i*2-1,2),10));
End;
CurMSComm.Output:=Query_Card[1]+Query_Card[2]+Query_Card[3]+Query_Card[4]+Query_Card[5]+Query_Card[6]+Query_Card[7]+Query_Card[8]+Query_Card[9]+Query_Card[10]+Query_Card[11]+Query_Card[12]+Query_Card[13]+Query_Card[14]+Query_Card[15]+Query_Card[16]+Query_Card[17]+Query_Card[18];
sleep(100);
End;

请问为什么会出现这种情况?如何解决!谢谢!欢迎各位提出意见和建议。
 
问题路标:对不起了各位,我真的急,
原谅我用这种方法,请进:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1792180
 
串口程序我后来研究了好久,写了下面的代码,后台生成一个线程监听串口,不影响前台工作。效果很好,一直用于GPS仪器的数据接收。

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

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.
 
TO HUMANC2D4:
谢谢你提供的信息,我慢慢研究一下,你能帮我调一下吗?
如果你愿意的话,我可以将我的破源码给你!谢谢。
ght_ght@mail.china.com
qq:53588198
 
后退
顶部