Home > Back-end >  In the Delphi check serial data within the given time
In the Delphi check serial data within the given time

Time:09-17

Just contact Delph, I was like that, I have a button, request within 3 s has been read serial data, every 1 s 1 time, to meet the requirements of data within a 3 s or time arrive in 3 s, then next step, now check serial data within 3 s stuck in this step, before has always been a MCU, so that writing is SCM, masters help you advice please!

While (3 s time to)
{
Check serial data
}

For the next step was later found not this kind of writing, the timer can trigger the


Delay (1000);
Delay (1000);
Delay (1000);
Check serial data this way also not line, does not respond to the serial port of the time,

CodePudding user response:

Delay (1000);
Delay (1000);
Delay (1000);
Check serial data this way also not line, serial events does not respond,

CodePudding user response:

A thread to try try?

CodePudding user response:

You may be no concept for events,
Directly put a TTimer component, OnTimer event response is ok,

CodePudding user response:

Use a timer, or thread can operate

CodePudding user response:

Your idea is wrong, cannot use timer, otherwise your application will be out of the question,
Written using Win32 dynamic library to read the data, then it is ok to use Delphi to invoke the dynamic library,
Because when a serial port data will generate interrupts, automatic trigger thread to read data,
If company behavior, agreement, you gave me I write dynamic library to you!

CodePudding user response:

My advice is as follows:
1, Delphi serial data received with control, such as the triggering event in a spcomm) to receive, don't take the initiative to query a serial port; In the single chip microcomputer and seldom use query access to a serial port data;
2, set a flag bit in the triggering event, when the received data, said mark position 1 received data
3, set the timer, time for 1 s trigger, apply for a global variable, used to calculate the number of timer trigger;
4, the program content is testing whether there is data received, if there is data received, determine whether the data meet the requirements; If required, turn to your other operations, timer stop at the same time, the counter is reset, otherwise, the timer counter plus 1;
5, when the timer counter reaches 3, if you can't meet the requirements of data into your fault operation, then the timer stop, counter reset;
6, button code, reset the counter first, and then reset the serial data reception sign, and then open the timer;
7, actually oneself add debug details

CodePudding user response:

98 receive serial data processing threads, you can consult,
It is best to use thread processing, use WaitCommEvent monitoring port status in the thread, and to read the data and cache,
HCom is created through CreateFile port handle,

This thread was housed in the ComDrv, open after the port is ready to receive data after starting, put the thread run,

You can open port, run the thread, and then wait for 3 seconds in the main process, it is time to wait to check whether the data in the buffer in accordance with your request,

//TWaitComm 
The constructor TWaitComm. Create (hCom: THandle; DCB: TDCB; Po: POVERLAPPED; Buf: PChar; PBufState: TPPortBufState);
The begin
FhCom:=hCom;
FDcb:=DCB;
Fpo:=Po;
FState:=tsRun;
Priority:=tpLowest;

FPortBuf:=Buf;
FpPortBufState:=pBufState;

Inherited the Create (False);
end;

Procedure TWaitComm. SetState (const State: TThreadState);
The begin
FState:=the State;
end;

Procedure TWaitComm. ProcRun;
Var
DwNOBR dwErrorFlags: dwords.
The ComStat: TCOMSTAT;
IBufFreeSize: integer;
The begin
If (WaitCommEvent (FhCom FdwEvtMask, Fpo)) then
The begin
If ((FdwEvtMask and EV_RXCHAR)=EV_RXCHAR) then
The begin
//wait for buffer free
While FpPortBufState ^. NowOprt<> OpIdle do Sleep (5);

//set the buffer to write state
//buffer the remaining space size
FpPortBufState ^. NowOprt:=opInWrite;
IBufFreeSize:=PORT_BUFFER_SIZE - FpPortBufState ^. Tail;

//read port data to the buffer
//only try to read number of bytes in the queue
ClearCommError (FhCom dwErrorFlags, @ ComStat);
If (iBufFreeSize> ComStat. CbInQue) then
DwNOBR:=ComStat cbInQue
The else
DwNOBR:=iBufFreeSize;

ReadFile (FhCom FPortBuf [FpPortBufState ^. Tail], dwNOBR, dwNOBR, Fpo);
FpPortBufState ^. Tail:=FpPortBufState ^. Tail + dwNOBR;

//restore buffer for free
FpPortBufState ^. NowOprt:=opIdle;
end;
end;
end;

Procedure TWaitComm. Execute;
The begin
{Place thread code here}
While True do
The begin
Case FState of
TsRun:
The begin
ProcRun;
end;
TsPause:
The begin
Sleep (10);
end;
TsEnd:
The begin
Exit;
end;
end;//end case
end;//end while
end;
//=========================TWaitComm

CodePudding user response:

The building Lord as controls, use the COM XE recommended CPORT, D6, D7 can use SPCOM, use port trigger, generally don't use the timer
  • Related