begin
//команда датчику
Buf2[0]:=$0023;
Buf2[1]:=$0030;
Buf2[2]:=$0033;
Buf2[3]:=$004F;
Buf2[4]:=$004E;
Buf2[5]:=$000D;
hPort:=CreateFile(PChar('Com1'), Generic_Read or Generic_Write, 0, nil,
Open_Existing, File_Attribute_Normal, 0);
if hPort = INVALID_HANDLE_VALUE then begin
exit;
end;
if GetCommState(hPort, DCB) then ;
DCB.BaudRate := CBR_9600;
DCB.ByteSize := 8;
DCB.StopBits := ONESTOPBIT;
DCB.Parity := NOPARITY;
if not SetCommState(hPort, DCB) then
if not SetupComm(hPort, 16, 16) then
if PurgeComm(hPort, PURGE_TXABORT or PURGE_RXABORT or PURGE_TXCLEAR or PURGE_RXCLEAR) then ;
if not SetCommMask(hPort, EV_RXCHAR) then raise Exception.Create('Error setting port mask');
// Запись в порт
OverWrite.hEvent := CreateEvent(nil, True, False, nil);
if OverWrite.hEvent = Null then
raise Exception.Create('Error creating write event');
if (not WriteFile(hPort, buf2, Length(buf2),
Bytes, nil))
and (GetLastError <> ERROR_IO_PENDING) then
raise Exception.Create('Error writing port');
// Чтение из порта
ClearCommError(hPort,Errors,@TheStruct);
if TheStruct.cbInQue>0 then
begin
Edit1.Text:='есть';
ReadFile(hPort, Buf1, TheStruct.cbInQue, Bytes, nil);
end
else
begin Edit1.Text:='нет'; end;
end;
запись в порт производится намана - прибор реагирует на команды. а вот ответное сообщение прочитать не получается.
што я делаю не так? каких настроек или еще чего не хватат?
подскажите плиз!
|