问题描述
- delphi中spccom串口通讯问题
-
/串口接收数据
//------------------------------------------------------------------------------
procedure TFormMain.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);
var
CRC:Word;
pbuf:pchar;
idx:integer;
begin
pbuf:=pchar(Buffer);
SetLength(RBuf,BufferLength);
move(pbuf^,pchar(@RBuf[1])^,bufferlength);
lstLog.Add('接收:'+Rbuf);
lstLog.ItemIndex:=lstLog.Items.Count-1;
end;
//------------------------------------------------------------------------------
// 枚举注册表中已经存在的串口
//----------------------------------------------------------------
procedure TFormMain.EnumComPorts(Ports: TStrings);
var
KeyHandle: HKEY;
ErrCode, Index: Integer;
ValueName, Data: string;
ValueLen, DataLen, ValueType: DWORD;
TmpPorts: TStringList;
begin
ErrCode := RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
'HARDWAREDEVICEMAPSERIALCOMM',
0,
KEY_READ,
KeyHandle);if ErrCode <> ERROR_SUCCESS then Exit; // raise EComPort.Create(CError_RegError, ErrCode);
TmpPorts := TStringList.Create;
try
Index := 0;
repeat
ValueLen := 256;
DataLen := 256;
SetLength(ValueName, ValueLen);
SetLength(Data, DataLen);
ErrCode := RegEnumValue(
KeyHandle,
Index,
PChar(ValueName),
Cardinal(ValueLen),
nil,
@ValueType,
PByte(PChar(Data)),
@DataLen);if ErrCode = ERROR_SUCCESS then begin SetLength(Data, DataLen); TmpPorts.Add(Data); Inc(Index); end else if ErrCode <> ERROR_NO_MORE_ITEMS then exit; //raise EComPort.Create(CError_RegError, ErrCode); until (ErrCode <> ERROR_SUCCESS) ; TmpPorts.Sort; Ports.Assign(TmpPorts);
finally
RegCloseKey(KeyHandle);
TmpPorts.Free;
end;end;
这段程序读不懂,请问各位高手
时间: 2024-10-02 18:55:21