Click here to Skip to main content
15,905,414 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: timeout for while loop Pin
Alexander M.,28-Jul-05 8:57
Alexander M.,28-Jul-05 8:57 
GeneralRe: timeout for while loop Pin
trigger9128-Jul-05 9:33
susstrigger9128-Jul-05 9:33 
GeneralRe: timeout for while loop Pin
Alexander M.,29-Jul-05 4:26
Alexander M.,29-Jul-05 4:26 
GeneralRe: timeout for while loop Pin
Peter Weyzen28-Jul-05 9:46
Peter Weyzen28-Jul-05 9:46 
GeneralRe: timeout for while loop Pin
Alexander M.,29-Jul-05 4:25
Alexander M.,29-Jul-05 4:25 
GeneralRe: timeout for while loop Pin
David Crow29-Jul-05 6:03
David Crow29-Jul-05 6:03 
GeneralRe: timeout for while loop Pin
nm_11429-Jul-05 14:16
nm_11429-Jul-05 14:16 
Generalread from comport problem Pin
quarry_0628-Jul-05 8:42
quarry_0628-Jul-05 8:42 
Hi I am using the library I found here:
http://www.codeproject.com/system/serial.asp
in my program. However, I run into the following problem when I am reading from a comport. (example listener).

I took the code, and created a little function
int readme(CSerial &serial)<br />
{<br />
	LONG    lLastError = ERROR_SUCCESS;<br />
	lLastError = serial.SetMask(CSerial::EEventBreak |<br />
								CSerial::EEventCTS   |<br />
								CSerial::EEventDSR   |<br />
								CSerial::EEventError |<br />
								CSerial::EEventRing  |<br />
								CSerial::EEventRLSD  |<br />
								CSerial::EEventRecv);<br />
	if (lLastError != ERROR_SUCCESS)<br />
		return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port event mask"));<br />
<br />
	// Use 'non-blocking' reads, because we don't know how many bytes<br />
	// will be received. This is normally the most convenient mode<br />
	// (and also the default mode for reading data).<br />
    lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking);<br />
	if (lLastError != ERROR_SUCCESS)<br />
		return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port read timeout."));<br />
	<br />
	bool fContinue = true;<br />
	do<br />
	{<br />
		// Wait for an event<br />
		lLastError = serial.WaitEvent();<br />
		if (lLastError != ERROR_SUCCESS)<br />
			return ::ShowError(serial.GetLastError(), _T("Unable to wait for a COM-port event."));<br />
<br />
		// Save event<br />
		const CSerial::EEvent eEvent = serial.GetEventType();<br />
<br />
		// Handle break event<br />
		if (eEvent & CSerial::EEventBreak)<br />
		{<br />
			printf("\n### BREAK received ###\n");<br />
		}<br />
<br />
		// Handle CTS event<br />
		if (eEvent & CSerial::EEventCTS)<br />
		{<br />
			printf("\n### Clear to send %s ###\n", serial.GetCTS()?"on":"off");<br />
		}<br />
<br />
		// Handle DSR event<br />
		if (eEvent & CSerial::EEventDSR)<br />
		{<br />
			printf("\n### Data set ready %s ###\n", serial.GetDSR()?"on":"off");<br />
		}<br />
<br />
		// Handle error event<br />
		if (eEvent & CSerial::EEventError)<br />
		{<br />
			printf("\n### ERROR: ");<br />
			switch (serial.GetError())<br />
			{<br />
			case CSerial::EErrorBreak:		printf("Break condition");			break;<br />
			case CSerial::EErrorFrame:		printf("Framing error");			break;<br />
			case CSerial::EErrorIOE:		printf("IO device error");			break;<br />
			case CSerial::EErrorMode:		printf("Unsupported mode");			break;<br />
			case CSerial::EErrorOverrun:	printf("Buffer overrun");			break;<br />
			case CSerial::EErrorRxOver:		printf("Input buffer overflow");	break;<br />
			case CSerial::EErrorParity:		printf("Input parity error");		break;<br />
			case CSerial::EErrorTxFull:		printf("Output buffer full");		break;<br />
			default:						printf("Unknown");					break;<br />
			}<br />
			printf(" ###\n");<br />
		}<br />
<br />
		// Handle ring event<br />
		if (eEvent & CSerial::EEventRing)<br />
		{<br />
			printf("\n### RING ###\n");<br />
		}<br />
<br />
		// Handle RLSD/CD event<br />
		if (eEvent & CSerial::EEventRLSD)<br />
		{<br />
			printf("\n### RLSD/CD %s ###\n", serial.GetRLSD()?"on":"off");<br />
		}<br />
<br />
		// Handle data receive event<br />
		if (eEvent & CSerial::EEventRecv)<br />
		{<br />
			// Read data, until there is nothing left<br />
			DWORD dwBytesRead = 0;<br />
			char szBuffer[101];<br />
			do<br />
			{<br />
				// Read data from the COM-port<br />
				lLastError = serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead);<br />
				if (lLastError != ERROR_SUCCESS)<br />
					return ::ShowError(serial.GetLastError(), _T("Unable to read from COM-port."));<br />
<br />
				if (dwBytesRead > 0)<br />
				{<br />
					// Finalize the data, so it is a valid string<br />
					szBuffer[dwBytesRead] = '\0';<br />
<br />
					// Display the data<br />
					printf("%s|", szBuffer);<br />
					<br />
					// Check if EOF (CTRL+'[') has been specified<br />
					if (strchr(szBuffer,EOF_Char))<br />
						fContinue = false;<br />
				}<br />
			}<br />
		    while (dwBytesRead == sizeof(szBuffer)-1);<br />
		}<br />
	}<br />
	while (fContinue);<br />
	serial.Close();<br />
	lLastError = serial.Open(_T("COM1"),0,0,false);<br />
	if (lLastError != ERROR_SUCCESS)<br />
		return ::ShowError(serial.GetLastError(), _T("Unable to open COM-port"));<br />
<br />
    // Setup the serial port (9600,8N1, which is the default setting)<br />
    lLastError = serial.Setup(CSerial::EBaud38400,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);<br />
	if (lLastError != ERROR_SUCCESS)<br />
		return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port setting"));<br />
	return 0;<br />
}


then, what I do is following:

int __cdecl _tmain (int /*argc*/, char** /*argv*/)<br />
{<br />
    CSerial serial;<br />
	LONG    lLastError = ERROR_SUCCESS;<br />
<br />
    // Attempt to open the serial port (COM1)<br />
    lLastError = serial.Open(_T("COM1"),0,0,false);<br />
	if (lLastError != ERROR_SUCCESS)<br />
		return ::ShowError(serial.GetLastError(), _T("Unable to open COM-port"));<br />
<br />
    // Setup the serial port (9600,8N1, which is the default setting)<br />
    lLastError = serial.Setup(CSerial::EBaud38400,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);<br />
	if (lLastError != ERROR_SUCCESS)<br />
		return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port setting"));<br />
<br />
    // Register only for the receive event<br />
    <br />
<br />
    // Keep reading data, until an EOF (CTRL-Z) has been received<br />
<br />
	//lLastError = serial.Write("\n");<br />
	//Sleep(1000);<br />
lLastError = serial.Write("\nwhoami\n");<br />
int r;<br />
//r = readme(serial);<br />
if (lLastError != ERROR_SUCCESS)<br />
		return ::ShowError(serial.GetLastError(), _T("Error sending a return"));<br />
lLastError = serial.Write("\nwhoami\n");<br />
		 <br />
r = readme(serial);<br />
if (lLastError != ERROR_SUCCESS)<br />
		return ::ShowError(serial.GetLastError(), _T("Error sending a return"));<br />
<br />
		//r = readme(serial);<br />
<br />
		//lLastError = serial.Write("\nwhoami\n");<br />
if (lLastError != ERROR_SUCCESS)<br />
		return ::ShowError(serial.GetLastError(), _T("Error sending a return"));<br />
<br />
    // Close the port again<br />
    serial.Close();<br />
    return 0;<br />
}


ok when the first is commented out, I see the results of both functions. but when the first readme is there, it only reads up to that point, and goes no further.

why is that so? I would like to read information step by step, as it needs to processed.

thanks alot for any help

quarry
GeneralRe: read from comport problem Pin
Bob Stanneveld28-Jul-05 19:19
Bob Stanneveld28-Jul-05 19:19 
GeneralRe: read from comport problem Pin
quarry_0629-Jul-05 8:15
quarry_0629-Jul-05 8:15 
GeneralRe: read from comport problem Pin
Bob Stanneveld1-Aug-05 1:01
Bob Stanneveld1-Aug-05 1:01 
GeneralWeb Services from VC++ 6.0 Pin
carks28-Jul-05 8:21
carks28-Jul-05 8:21 
GeneralRe: Web Services from VC++ 6.0 Pin
fakefur28-Jul-05 10:05
fakefur28-Jul-05 10:05 
GeneralRe: Web Services from VC++ 6.0 Pin
Ravi Bhavnani28-Jul-05 10:16
professionalRavi Bhavnani28-Jul-05 10:16 
GeneralHelp me... Rich Edit Pin
Aqueel28-Jul-05 8:18
Aqueel28-Jul-05 8:18 
GeneralRe: Help me... Rich Edit Pin
John R. Shaw28-Jul-05 8:51
John R. Shaw28-Jul-05 8:51 
GeneralRe: Help me... Rich Edit Pin
David Crow28-Jul-05 9:49
David Crow28-Jul-05 9:49 
GeneralChild Dialog Background Color Pin
liehann28-Jul-05 8:12
liehann28-Jul-05 8:12 
GeneralRe: Child Dialog Background Color Pin
Alexander M.,28-Jul-05 9:32
Alexander M.,28-Jul-05 9:32 
GeneralRe: Child Dialog Background Color Pin
liehann28-Jul-05 21:01
liehann28-Jul-05 21:01 
General'needs to have dll-interface' warning Pin
Themis28-Jul-05 7:39
Themis28-Jul-05 7:39 
Question#define a comment to remove code? Pin
Giles28-Jul-05 6:31
Giles28-Jul-05 6:31 
AnswerRe: #define a comment to remove code? Pin
Alexander M.,28-Jul-05 9:00
Alexander M.,28-Jul-05 9:00 
AnswerRe: #define a comment to remove code? Pin
Bob Stanneveld28-Jul-05 19:21
Bob Stanneveld28-Jul-05 19:21 
Generalmodeless and modal child dialogs Pin
23_44428-Jul-05 6:12
23_44428-Jul-05 6:12 

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.