Home > Back-end >  A serial port communication DLL, how will send commands and output in a function to return the resul
A serial port communication DLL, how will send commands and output in a function to return the resul

Time:10-13

Use spcomm communication components, send instructions in a function, function is over after sending, then the result has not returned, need to get back in the event of message processing as a result, how to send instructions and return the results to a function?

CodePudding user response:

You this function also have to wait for, in the event of an SPCOMM data received replacement function variable, the function returns the data, or after a certain time return an error or other values,

CodePudding user response:

Don't receiving, receiving it manually,

A function about the following format:

Write ();//write a serial port
Sleep ();//wait for
Read ();//read serial

Wait here, if the serial device response time are fixed, you can use the Sleep () fixed delay time, if is indefinite long response time, you can check the received data length, receiving data length enough is returned

CodePudding user response:

Think this kind of communication or callback process is better!

CodePudding user response:

Can send in the past, by shaking hands, waiting to come back, come back the information can be carried in the data you want

CodePudding user response:

Query the number of bytes of input buffer (InputCount), if greater than zero is read, of course, the most in the communication process in a thread is better,

CodePudding user response:

I was at a function in a child thread to a serial port to send and receive, the main thread using the waitforsingleobject (thread. Handle, 2000), but the main thread calls waitforsingleobject, child thread also can not receive serial news, waitforsingleobject timeout after receiving the message, you are with what method for?

CodePudding user response:

refer to 6th floor kingbenz response:
I am in the function in a child thread to a serial port to send and receive, the main thread using the waitforsingleobject (thread. Handle, 2000), but the main thread calls waitforsingleobject, child thread also can not receive serial news, waitforsingleobject timeout after received message, you are with what method for?

The main thread using the waitforsingleobject (thread. Handle, 2000), thread. The handle is who handle?

CodePudding user response:

refer to 7th floor JohnYale response:
Quote: refer to the sixth floor kingbenz response:

I was at a function in a child thread to a serial port to send and receive, the main thread using the waitforsingleobject (thread. Handle, 2000), but the main thread calls waitforsingleobject, child thread also can not receive serial news, waitforsingleobject timeout after receiving the message, you are with what method for?

The main thread using the waitforsingleobject (thread. Handle, 2000), thread. The handle is who handle?

Of course, is the child thread handle

CodePudding user response:

May the child thread have received a serial port message, only when the main thread Wait was blocked, so what do you think of received messages
  • Related