CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2005
    Posts
    125

    Serial communication in VC++..

    Sorry folks for posting a new thread. My earlier thread (posted two days ago) was unanswered and since the thread was big, I createda new thread and rephrase my question.
    I am doing a serial communication with a device connected to a EURYSIS frame grabber card. ...
    My API reads and writes values to the device ....
    For instance, when I start, my API will display the stored values in the device. I have also provided a read button for this. .... that will load these values again .. there is a write button to write values to the device ..
    My API is working perfectly fine .. It is displaying the values initially,and hte read function is also working fine ..

    The problem is with the initial display.
    When I start my API, I get the correct values displayed ..... but then I just switch off (power off) my device and press the read button, the value (say ET) is displayed zero. .. that is alright ..
    PROBLEM -1
    After this, I power on the device, press the read the GUI displays the value 69 that is stored value for ET in the device ....that is also good .
    But the second time I press the read button the value displayed is 30. and again when I press the read button It comes back to 89.. after that How many times I press the read button It remains 69. ...
    Q. Why is it going to 30 in the second read process after displaying correctly int the first instance and then and then coming back to the correct value after that ?
    In other words, in my initial step while loading my API I am performing two read operations ... hence when I first start my API after powering up my device, the value of ET is displayed wrongly as 30.
    But one press of read, displays it back to 69 ...

    Folks.... now a bit of my coding ..
    I am using serial communication commands ..
    Creating object like
    DCB PortDCB ...... then ..
    Opening Driver's
    Detecting boards etc etc .
    then a ....CreateFile command with the correct parameters .. .. then, Setting the following timeout values to the DCB object ..

    PortDCB.DCBlength = sizeof(PortDCB);
    PortDCB.BaudRate = 9600;
    PortDCB.ByteSize = 8;
    PortDCB.Parity = NOPARITY;
    PortDCB.StopBits = ONESTOPBIT;
    PortDCB.EvtChar ='\0';
    PortDCB.fNull=FALSE;
    PortDCB.fBinary=TRUE;
    PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
    PortDCB.fRtsControl = DTR_CONTROL_ENABLE;
    PortDCB.fOutxCtsFlow = FALSE;
    PortDCB.fOutxDsrFlow = FALSE;
    PortDCB.fDsrSensitivity = FALSE;
    PortDCB.fOutX = FALSE;
    PortDCB.fInX = FALSE;
    PortDCB.fTXContinueOnXoff = FALSE;
    PortDCB.fParity = FALSE;
    // getting and setting COMSTATE
    COMMTIMEOUTS cto;
    cto.ReadIntervalTimeout=MAXDWORD;
    cto.ReadTotalTimeoutMultiplier= 0;
    cto.ReadTotalTimeoutConstant=300;
    cto.WriteTotalTimeoutMultiplier=(20000 / PortDCB.BaudRate) ? (20000 / PortDCB.BaudRate) : 1;
    cto.WriteTotalTimeoutConstant=300;

    Then let me give one example of one value sat ET. how I read it ...while pressing the read Button ..

    unsigned long *RxIntegration;
    OVERLAPPED oswriter1;
    int nWriteHexValueEt;

    nWriteHexValueEt=0xFF000207;

    WriteFinalValue(nWriteHexValueEt,m_hCommPort[gPortNum]); // My function to write values

    *(RxIntegration)=0;
    memset(&oswriter1,0,sizeof(oswriter1));
    bResult2=ReadFile(m_hCommPort[gPortNum],RxIntegration,sizeof(RxIntegration),&BytesRead,&oswriter1);

    PLEASE NOTE...I have only copied necessary code.. I have all the commands like for instance in my function of WriteFinalValue .. I have used the
    CreateEvent
    GetLastError
    ClearCommError
    and then the WriteFile

    Similarly after the ReadFile also ...

    Any helps please ..... I am a newbe in VC++ so please elaborate your replies ...
    My OS is Windows XP Pro with SP 2..

  2. #2
    Join Date
    Apr 2005
    Posts
    125

    Unhappy Serial communication in VC++..

    I am deeply sorry to PUMP this thread like tis..

    Ok another question...Is there an alternate code for the above said procedures ???

    Waiting for a solution ...

  3. #3
    Join Date
    Apr 2005
    Posts
    125

    Unhappy Re: Serial communication in VC++..

    bumping my thread ...

    Ok I again reframe my question ..
    Is there any bug or defect known with the readfile writefile function ..and or with respect to VC++ ?? if So what is it and how to eliminate it...

    Like for eg: there is a defect that the first readfile would retrieve the previous values and later will retrieve the correct value
    something something ...... ?

    What are the known defects and their possible removals ..
    What is an alternate solution ...

  4. #4
    Join Date
    Sep 2005
    Location
    Singapore
    Posts
    8

    Re: Serial communication in VC++..

    I don't know the following code segment will be helpful to you. This is the function that I created using one of the sample program I found from this forum and it read the value from serial port correctly. I am reading barcode from scanner and I extract the string until I detect '\0x010'(i.e ETX) or '\n' (i.e. LF)

    Code:
    CString ReadComPort()
    {
    	
    	BOOL fWaitingOnRead = FALSE;
    	char cBuffer[READ_BUF_SIZE];
    	DWORD iBytesRead;
    	CString sBuffer; 
    	OVERLAPPED gOverlapped = {0};
    	
    	// Create the overlapped event. Must be closed before exiting
    	// to avoid a handle leak.
    	gOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    	
    	// Error creating overlapped event; abort.
    	if (gOverlapped.hEvent == NULL)
    		AfxMessageBox(_T("Error Creating Overlapped Structure")); 
    
    	if (!fWaitingOnRead) 
    	{
    		// Issue read operation.
    		
    		if (!ReadFile(hComm, cBuffer, READ_BUF_SIZE, &iBytesRead, &gOverlapped))
    		{
    				if (GetLastError() != ERROR_IO_PENDING)     // read not delayed?
    				{	
    					// Error in communications; report it.
    					AfxMessageBox(_T("Error in Communication"));
    					exit(1);
    				}
    				else
    				{
    					fWaitingOnRead = TRUE;
    				}
    		}
    		else 
    		{    
    			// read completed immediately
    			sBuffer = "";
    			if (cBuffer[0] != '\x010')
    			{
    				for(int charcount=0;charcount<READ_BUF_SIZE;charcount++)
    				{
    					if (cBuffer[charcount] == '\n')
    						break;
    					sBuffer = sBuffer + cBuffer[charcount];
    				}
    				fWaitingOnRead = FALSE;
    			}
    		}
    		Sleep(100);
    	}
            return sBuffer;
    	
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured