Click here to Skip to main content
15,895,606 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionClose Child Common Dialog Pin
john563230-Dec-13 18:21
john563230-Dec-13 18:21 
QuestionRe: Close Child Common Dialog Pin
Richard MacCutchan30-Dec-13 22:55
mveRichard MacCutchan30-Dec-13 22:55 
Questionhow to fix the error of the set and get function in my program? Pin
Member 1049499430-Dec-13 5:16
Member 1049499430-Dec-13 5:16 
AnswerRe: how to fix the error of the set and get function in my program? Pin
Thong LeTrung30-Dec-13 5:27
Thong LeTrung30-Dec-13 5:27 
AnswerRe: how to fix the error of the set and get function in my program? Pin
Albert Holguin30-Dec-13 5:28
professionalAlbert Holguin30-Dec-13 5:28 
QuestionHow to convert a visual c++ project to a dll project file? Pin
Adewumi Adedeji29-Dec-13 23:51
Adewumi Adedeji29-Dec-13 23:51 
AnswerRe: How to convert a visual c++ project to a dll project file? Pin
Richard MacCutchan30-Dec-13 0:44
mveRichard MacCutchan30-Dec-13 0:44 
QuestionOverlapped serial comunication Pin
Palmitoxico29-Dec-13 11:55
Palmitoxico29-Dec-13 11:55 
Hi,

I'm have been trying to write an code in C++ by using the Win32 API to communicate through the RS232 serial port with equipment. It's a simplex communication (only the equipment sends data in blocks of 289 bytes). I'm using an dedicated thread to read the serial port. Firstly I tried to use non-overlapped communication, but I realized that is impossible to close the serial port if there's no data incoming (the thread gets stuck on WaitCommEvent function). So I tried to use overlapped I/O and WaitForMultipleObjects to solve this problem:



C++
void *SerialRead(void *param)  // param is a dummy pointer
{
	OVERLAPPED overlapped;
	
	memset( &overlapped, 0, sizeof(overlapped) );
	overlapped.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );

	SerialEvents[0] = overlapped.hEvent;
	
	ReadFile(SerialPort, SerialBuffer, 289, &dwBytesRead, &overlapped);//Try to read 289 bytes
	
    
	while (WaitForMultipleObjects(2, SerialEvents, false, INFINITE) == WAIT_OBJECT_0) //Wait for some event, if is an serial port event, executes de loop, if not, exit.
	{

		//...
		//Do stuff
		ReadFile(SerialPort, SerialBuffer, 289, &dwBytesRead, &overlapped); //Attempt to read the next 289 bytes
	}

	ClosePort();
	return 0;
}


I use the function SetEvent(SerialEvents[1]); to close serial port on the main thread.

To open the serial port:

C++
SerialPort = CreateFile(PortName,
GENERIC_READ|GENERIC_WRITE,//access ( read and write)
0,    //(share) 0:cannot share the COM port
0,    //security  (None)                
OPEN_EXISTING,// creation : open_existing
FILE_FLAG_OVERLAPPED,    // overlapped I/O
0// no templates file for COM port...
);


The problem is that even if I recive the 289 bytes, the thread keeps wating the function "WaitForMultipleObjects" return. If I recive the next 289 bytes the WaitForMultipleObjects function returns, but the data are mixed.

Someone could help me?
AnswerRe: Overlapped serial comunication Pin
Richard MacCutchan29-Dec-13 22:52
mveRichard MacCutchan29-Dec-13 22:52 
GeneralRe: Overlapped serial comunication Pin
Palmitoxico30-Dec-13 8:09
Palmitoxico30-Dec-13 8:09 
AnswerRe: Overlapped serial comunication Pin
Richard Andrew x6430-Dec-13 9:26
professionalRichard Andrew x6430-Dec-13 9:26 
GeneralRe: Overlapped serial comunication Pin
Palmitoxico31-Dec-13 2:32
Palmitoxico31-Dec-13 2:32 
Questionhow to initialize array of variable length? Pin
Le@rner27-Dec-13 21:18
Le@rner27-Dec-13 21:18 
AnswerRe: how to initialize array of variable length? Pin
Randor 27-Dec-13 21:36
professional Randor 27-Dec-13 21:36 
GeneralRe: how to initialize array of variable length? Pin
Le@rner20-Jan-14 21:26
Le@rner20-Jan-14 21:26 
AnswerRe: how to initialize array of variable length? Pin
Richard MacCutchan27-Dec-13 22:34
mveRichard MacCutchan27-Dec-13 22:34 
AnswerRe: how to initialize array of variable length? Pin
Albert Holguin28-Dec-13 10:05
professionalAlbert Holguin28-Dec-13 10:05 
QuestionRe: how to initialize array of variable length? Pin
David Crow3-Jan-14 4:28
David Crow3-Jan-14 4:28 
AnswerRe: how to initialize array of variable length? Pin
Le@rner19-Jan-14 22:47
Le@rner19-Jan-14 22:47 
Questionhelp me to generate MD5 of Binary hash value. Pin
Le@rner26-Dec-13 21:33
Le@rner26-Dec-13 21:33 
AnswerRe: help me to generate MD5 of Binary hash value. Pin
Richard MacCutchan26-Dec-13 22:25
mveRichard MacCutchan26-Dec-13 22:25 
GeneralRe: help me to generate MD5 of Binary hash value. Pin
Le@rner26-Dec-13 23:45
Le@rner26-Dec-13 23:45 
GeneralRe: help me to generate MD5 of Binary hash value. Pin
Richard MacCutchan27-Dec-13 0:08
mveRichard MacCutchan27-Dec-13 0:08 
GeneralRe: help me to generate MD5 of Binary hash value. Pin
Le@rner27-Dec-13 2:03
Le@rner27-Dec-13 2:03 
GeneralRe: help me to generate MD5 of Binary hash value. Pin
Richard MacCutchan27-Dec-13 3:26
mveRichard MacCutchan27-Dec-13 3:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.