View Single Post
Join Date: Dec 2008
Posts: 24
Reputation: GadiK is an unknown quantity at this point 
Solved Threads: 0
GadiK GadiK is offline Offline
Newbie Poster

Re: reading from serial port way too slow (C++)

 
0
  #3
Dec 4th, 2008
Hi,
I didn't try on another computer yet.

The Sleep() function is set to 1 milisecond. It is the minimum, so I tested to see if a "longer" sleep would help me - it didn't, it took longer.

Here's the thread:
(I'm using a SerialCom object that somebody else wrote. The function ReadString() uses ReadFile() and returns FALSE if operation didn't succeed. Also, I used AfxBeginThread() to start this thread)
unsigned int CRmi_demoDlg::GetADISData(void *Sender)
{
	CRmi_demoDlg* pSender = NULL;
	pSender = (CRmi_demoDlg *)Sender;
	char filename[60];
	int rd_iter, i;
	GetSystemTime(&st);	
	//------------------------Files--------------------
	
	//open the serial device com port 
	pSender->ADISDevice.Open("COM8");
	
	//setting buffer size for serial device to avoid buffer overrun
	if (!SetupComm(pSender->ADISDevice.hCom,0x00F00000,64))
		myprintf("\nCouldn't set device buffer size!");

	GetCommProperties(pSender->ADISDevice.hCom,&commProp);
	//myprintf prints to a console which logs the threads activities
        myprintf("\nADIS buffer size: %d bytes",(int) commProp.dwCurrentRxQueue);
	
	//setup the serial port
	pSender->ADISDevice.Setup(115200,8,0,1);
	
	
	//if port isn't open;
	if (pSender->ADISDevice.hCom == INVALID_HANDLE_VALUE) {
		pSender->ADISDevice.Close();
		AfxMessageBox((CString)"Couldn't get device handle");
		return 0;  
	}
	
	sprintf_s(filename,60,"test%d_ADIS_%d.%d.%d_%d.            %d.%d.txt",test_num,st.wDay,st.wMonth,st.wYear,st.wHour+3,st.wMinute,st.wSecond);
		
	// open the log file
	ofstream logfile(filename);
	
	
	if ( !logfile.is_open() ) {
		AfxMessageBox((CString) "Couldn't open LOG file!");
		return 0;
	}

	CreateADISFileHeader(logfile);
	//------------------------Sampling--------------------
	
         //clear com before working with it	
         pSender->ADISDevice.clearCom();  
	
        if(pSender && pSender->initialized )
	{
		 char finger_byte[14]="0";
		 short finger_word, index;
		 short short_lowbyte=0;
		 short short_highbyte=0;
		 short flags = 0;
		 short data_error;		//keeps tabs of bit errors
		 bool r_sent = false;
		
		pSender->ADISDevice.WriteString("r",1);  //send "reset" command
		pSender->ADISDevice.WriteString("s",1); //send "start" command
		while ( pSender->ADISDevice.getNumBytesInQue() < 1 ) {};	//wait for data to come into the buffer
		myprintf("\nReceiving Data");
		
		//stop running if no more data in buffer or GUI closed
                while( pSender->initialized && (!r_sent || pSender->ADISDevice.getNumBytesInQue() > 0) )	
		{
			if ( !pSender->is_sample )
			{
				//if stop button pressed tell the ADIS to stop sending data
                               if ( !pSender->ADISDevice.WriteString("r",1) ) myprintf("\nCouldn't write to ADIS"); 
				if (!r_sent) myprintf("\nStop button pressed");
				r_sent = true; //r_sent flags that the device was told to stop sending data
			}
			GetSystemTime(&st);
			//first two bytes of every iteration are 0x55
			if ( pSender->ADISDevice.getNumBytesInQue() >= 2 )
			{
				pSender->ADISDevice.ReadString(finger_byte, 2);
			}
			else if (!r_sent) continue;		//loop only if ADIS stil working
			
			
			if ( (finger_byte[0] != finger_byte[1]) || (finger_byte[0] != 0x55) )
			{
				AfxMessageBox((CString)"Out of sync!");
				logfile.close();
				logfile2.close();
				pSender->ADISDevice.Close();
				AfxEndThread(0);
				return 0;
			}
			//reading iteration = 15 times, each time 14 bytes
			for (rd_iter=0; rd_iter < 15 ; rd_iter++)
			{
				data_error = 0;
				logfile << setw(5) << setfill(' ') << st.wHour+3 << ", " 
						<< setw(7) << setfill(' ') << st.wMinute << ", " 
						<< setw(7) << setfill(' ') << st.wSecond << ", " 
						<< setw(7) << setfill(' ') << st.wMilliseconds << ", ";
				
                               // wait untill there is enough information in the buffer unless ADIS stopped 
                               while ( pSender->ADISDevice.getNumBytesInQue() < 14 && !r_sent)	{}	[
				
				Sleep(1);
				if ( !pSender->ADISDevice.ReadString(finger_byte, 7*2) ) 
				{
					//if no more data in buffer end loop and make sound
                                        myprintf("\nCouldn't read from ADIS");
					MessageBeep(MB_ICONASTERISK);
					Sleep(500);
					MessageBeep(MB_ICONASTERISK);
					Sleep(500);
					MessageBeep(MB_ICONASTERISK);
					Sleep(500);
					break;
				}
				
                                //processing the received 14 bytes
                                //and writing them to the log file
                                for (i=0; i < 7*2 ; i++) 
				{
					short_highbyte=(( short)finger_byte[i]) & 0x00FF;
					short_lowbyte=(( short)finger_byte[++i]) & 0x00FF;
					finger_word=0;
					finger_word=short_highbyte*256+short_lowbyte;
					
					if (i == 1)  //first word is always index
					{
						finger_word = ( short) finger_word/64;  //shift right 6 times
						finger_word = finger_word & 0x000F;
						index = finger_word;
					}
					else  
					{
						flags = 0xC000 & finger_word;  
                                                //contracting 2 msb which are NewData 
                                                //and ErrorAlarm the other 14 bits are data
						finger_word <<= 2;
						finger_word >>= 2;	//with bit extraction because its signed short
											
						// put '1' at the bit corresponding to processed data 
                                                //for example: XAccl error is at bit number 5
						// ZGyro error is at bit number 0
						data_error = data_error << 2;
						flags = flags >>14;	//without bit extraction because its unsigned 
                                                                                   short
						data_error |= flags;
					}


					logfile << setw(10) << setfill(' ') << finger_word << ", ";
				}
				
				
				logfile << setw(10) << setfill(' ') << data_error << endl;
			}
		}
	}
	myprintf("\nNo more data in buffer");
	pSender->ADISDevice.WriteString("r",1);  //send "reset" command
	while (pSender->ADISDevice.getNumBytesInQue() > 0) pSender->ADISDevice.clearCom();
	pSender->ADISDevice.Close();
	
	if( logfile.is_open() )logfile.close();
	AfxEndThread(0);
	return 0;
	
}
/*----------------------------End of GetADISData---------------------------------------------*/
Last edited by GadiK; Dec 4th, 2008 at 4:55 am.
Reply With Quote