Home > Software engineering >  MFC CserialPort multithreading serial communication
MFC CserialPort multithreading serial communication

Time:09-30

Programs written in the CSerialPort class to realize serial data reading and writing, is now going to implement a feature like this: PC cycle constantly send instruction to the next bit machine (up and down a machine through the wireless digital radio connected to a serial port communication), after receive to return response, upper stop sending, after receive to reply
I am now in sending a command button using CSerialPort class writetoport () method can realize a writing, but in the loop, repeating writing, application sometimes does not respond to other action,
Think of the solution is, in a command button to create a thread, write a loop in the thread processing function, the loop body for CSerialPort class writetoport () method, but the problems is that a run will prompt: the memory cannot be read, can't write errors, such as
Consult everybody: how to invoke the CSerialPort class writetoport in thread () method? Or do you have any other solution, thank you!

CodePudding user response:

Fyi:
 # pragma comment (lib, "user32") 
#include
#include
#include
#include
Char datestr [16].
Char timestr [16].
Char MSS [4].
Void the log (char * s) {
* struct tm now;
Struct timeb TB;

Ftime (& amp; TB);
Now=localtime (& amp; TB. Time);
Sprintf (datestr, "% d % % 4 d - 02-02 d", now - & gt; Tm_year + 1900, now - & gt; Tm_mon + 1, now - & gt; Tm_mday);
Sprintf (timestr, "02 02 02 % d: % d: % d", now - & gt; Tm_hour, now - & gt; Tm_min, now - & gt; Tm_sec);
Sprintf (MSS, "% 3 d", TB. Millitm);
Printf (" % s % s. % s % s ", datestr, timestr, MSS, s);
}
VOID CALLBACK myTimerProc1 (
HWND HWND,//handle of the window for the timer messages
UINT uMsg,//WM_TIMER message
UINT idEvent,//timer identifier
DWORD dwTime//the current system time
) {
The log (" In myTimerProc1 \ n ");
}
VOID CALLBACK myTimerProc2 (
HWND HWND,//handle of the window for the timer messages
UINT uMsg,//WM_TIMER message
UINT idEvent,//timer identifier
DWORD dwTime//the current system time
) {
The log (" In myTimerProc2 \ n ");
}
Int main () {
int i;
MSG MSG.

SetTimer (NULL, 0, 0100 myTimerProc1);
SetTimer (NULL, 0, 0200 myTimerProc2);
for (i=0; I<20; I++) {
Sleep (500);
The log (In the main "\ n");
If (GetMessage (& amp; MSG, NULL, 0, 0)) {
TranslateMessage (& amp; MSG);
DispatchMessage (& amp; MSG);
}

}
return 0;
}
//the 2012-07-26 17:29:06. 375 In the main
//the 2012-07-26 17:29:06. 875 In myTimerProc1
//the 2012-07-26 17:29:07. 375 In the main
//the 2012-07-26 17:29:07. 875 In myTimerProc2
//the 2012-07-26 17:29:08. 375 In the main
//the 2012-07-26 17:29:08. 375 In myTimerProc1
//the 2012-07-26 17:29:08. 875 In the main
//the 2012-07-26 17:29:08. 875 In myTimerProc1
//the 2012-07-26 17:29:09. 375 In the main
//the 2012-07-26 17:29:09. 890 In myTimerProc2
//the 2012-07-26 17:29:10. 390 In the main
//the 2012-07-26 17:29:10. 390 In myTimerProc1
//the 2012-07-26 17:29:10. 890 In the main
//the 2012-07-26 17:29:10. 890 In myTimerProc1
//the 2012-07-26 17:29:11. 390 In the main
//the 2012-07-26 17:29:11. 890 In myTimerProc2
//the 2012-07-26 17:29:12. 390 In the main
//the 2012-07-26 17:29:12. 390 In myTimerProc1
//the 2012-07-26 17:29:12. 890 In the main
//the 2012-07-26 17:29:12. 890 In myTimerProc1
//the 2012-07-26 17:29:13. 390 In the main
//the 2012-07-26 17:29:13. 890 In myTimerProc2
//the 2012-07-26 17:29:14. 390 In the main
//the 2012-07-26 17:29:14. 390 In myTimerProc1
//the 2012-07-26 17:29:14. 890 In the main
//the 2012-07-26 17:29:14. 890 In myTimerProc1
//the 2012-07-26 17:29:15. 390 In the main
//the 2012-07-26 17:29:15. 890 In myTimerProc2
//the 2012-07-26 17:29:16. 390 In the main
//the 2012-07-26 17:29:16. 390 In myTimerProc1
//the 2012-07-26 17:29:16. 890 In the main
//the 2012-07-26 17:29:16. 890 In myTimerProc1
//the 2012-07-26 17:29:17. 390 In the main
//the 2012-07-26 17:29:17. 890 In myTimerProc2
//the 2012-07-26 17:29:18. 390 In the main
//the 2012-07-26 17:29:18. 390 In myTimerProc1
//the 2012-07-26 17:29:18. 890 In the main
//the 2012-07-26 17:29:18. 890 In myTimerProc1
//the 2012-07-26 17:29:19. 390 In the main
//the 2012-07-26 17:29:19. 890 In myTimerProc2

CodePudding user response:

Debugging and running, the Debug version appeared error retry running, combining the call stack, modify the code

CodePudding user response:

PC cycle constantly send instruction to the next bit machine, with timer, timer processing whether received reply there, get off timer, or send instructions

CodePudding user response:

Can also use these API functions: createFile open the serial port, setcommstate set serial port, readfile read serial data, writefile write a serial port data, closefile close a serial port data,

CodePudding user response:

Multiple Threads in the User Interface of http://msdn.microsoft.com/zh-cn/library/ms810439.aspx

CodePudding user response:

Memory access errors?? Debugging and see

CodePudding user response:

Top up for you, brothers, have tried to use this class, generate multiple object open multiple serial port?

CodePudding user response:

A serial port is simple post code or I'll give you an example
  • Related