Help Needed for CSerialPort v1.27
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Help Needed for CSerialPort v1.27

  1. #1
    Join Date
    Jan 2011
    Posts
    4

    Help Needed for CSerialPort v1.27

    I'm trying to get a C++ MFC SDI working using VS 2008 which takes a 6-bit binary value sent from a PIC16f877a through an RS232 line to a GUI. All I need the GUI to do is display the value sent to it, by an event handler on a button. I'm using Naughter's CSerialPort v1.27 (http://www.naughter.com/serialport.html).

    So my code compiles ok, but doesn't display anything. I'm a complete beginner to C++ and MFC but this is the code I have for the event handler:

    Code:
    void CWECSGUI10View::OnBnClickedBtnVlt()
    {
    	// TODO: Add your control notification handler code here
    
    	try
    	{
    
    	// opens comport 1
    	CSerialPort port;
        port.Open(1, 115200, CSerialPort::NoParity, 8,
    		CSerialPort::OneStopBit,
    		CSerialPort::XonXoffFlowControl, TRUE);
    
    	// reads RS232 input
    	port.Read(m_Voltin,1);
    
    	// display data
    	CString strVoltage;
    	double voltage;
    	strVoltage.Format(_T("%.3f"), voltage);
    	this->m_Voltin.SetWindowTextW(strVoltage);
    
    	port.Flush();
    	port.Close();
    
    	}
    
    	catch (CSerialException* pEx)
    	{
    		TRACE(_T("Handle Exception, Message:%s\n"),
    			pEx->GetErrorMessage());
    
    	pEx->Delete();
    	}
    
    }
    What am I doing wrong and how can I correct it?


    Just to make the information complete, on the PIC side of things I'm compiling with BoostC. My code has been verified with another app to send a 6-bit value through a 10-bit ADC module but here it is anyway:

    Code:
    #include <rs232_driver.h>
    #include <adc.h>
    
    .... default configuration etc ...
    
    	while( 1 )
    	{
    		voltage = adc_measure(0)>>4;
    		delay_ms( 250 );
    		uart_init(1,10); 
       		putc(voltage);
    	}
    Also to make the information complete, I added the m_Voltin variable to the form class as a public control edit variable.
    Last edited by VictorN; May 27th, 2014 at 08:24 AM. Reason: Added CODE tags

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Help Needed for CSerialPort v1.27

    Code:
    port.Read(m_Voltin,1);
    the Read function expects a void* (that needs to be a char buffer, i guess), not a pointer to a GUI component.

    Code:
    DWORD Read(void* lpBuf, DWORD dwCount);

  3. #3
    Join Date
    Jan 2011
    Posts
    4

    Re: Help Needed for CSerialPort v1.27

    Quote Originally Posted by Skizmo View Post
    Code:
    port.Read(m_Voltin,1);
    the Read function expects a void* (that needs to be a char buffer, i guess), not a pointer to a GUI component.

    Code:
    DWORD Read(void* lpBuf, DWORD dwCount);
    Yeah I've tried variations of this such as:

    Code:
    DWORD Read(m_Voltin, DWORD dwCount);
    etc but can't get anything to display. Also lots of variations of:

    char sRxBuf[10];
    DWORD dwRead = port.Read(sRxBuf, 10);
    dwRead; //To remove unreferrenced variable in VC 6.

    (which is from the example app provided with the code) such as

    DWORD dwRead = port.Read(m_Voltin, 1);
    dwRead; //To remove unreferrenced variable in VC 6.

    but still can't get it to display.

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Help Needed for CSerialPort v1.27

    Code:
    char sRxBuf[10];
    DWORD dwRead = port.Read(sRxBuf, 10);
    This is good.

    But your actual display never uses the 'sRxBuf'.
    Code:
    CString strVoltage;
    double voltage;
    strVoltage.Format(_T("&#37;.3f"), voltage);
    this->m_Voltin.SetWindowTextW(strVoltage);
    put the 'sRxBuf' in 'voltage' and you should see something.

  5. #5
    Join Date
    Jan 2011
    Posts
    4

    Re: Help Needed for CSerialPort v1.27

    Quote Originally Posted by Skizmo View Post
    put the 'sRxBuf' in 'voltage' and you should see something.
    I did that and still nothing. When I look at the output window I'm getting the following error message whenever I press the button:

    Code:
    CSerialPort::Read, Failed in call to ReadFile, Error:87
    Warning: throwing CSerialException for error 87
    First-chance exception at 0x7c812aeb in WECSGUI10.exe: Microsoft C++ exception: CSerialException at memory location 0x0012f3fc..
    Handle Exception, Message:The parameter is incorrect.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,359

    Re: Help Needed for CSerialPort v1.27

    Quote Originally Posted by Kappa3141 View Post
    ... I'm getting the following error message whenever I press the button:

    Code:
    CSerialPort::Read, Failed in call to ReadFile, Error:87
    Warning: throwing CSerialException for error 87
    First-chance exception at 0x7c812aeb in WECSGUI10.exe: Microsoft C++ exception: CSerialException at memory location 0x0012f3fc..
    Handle Exception, Message:The parameter is incorrect.
    Sure it is incorrect. Because you defined it wrong (or you passed in the wrong parameter):
    Quote Originally Posted by Kappa3141 View Post
    ... I added the m_Voltin variable to the form class as a public control edit variable.
    Please have a look at the code example that Skizmo has provided you in the post#4.
    Last edited by VictorN; May 27th, 2014 at 08:22 AM.
    Victor Nijegorodov

  7. #7
    Join Date
    Jan 2011
    Posts
    4

    Re: Help Needed for CSerialPort v1.27

    Quote Originally Posted by VictorN View Post
    Sure it is incorrect. Because you defined it wrong (or you passed in the wrong parameter):
    I realise that. How can I correct this?

    Quote Originally Posted by VictorN View Post
    Please have a look at the code example that Skizmo has provided you in the post#4.
    Er sorry but I did that already. In my previous post I said that I tried to replace the voltage variable with sRxBuf. This is what I then used:

    CString strVoltage;
    //double voltage;
    strVoltage.Format(_T("%.3f"), sRxBuf);
    this->m_Voltin.SetWindowTextW(strVoltage);

    Please remember I am a beginner!


    Quote Originally Posted by VictorN View Post
    control edit variable
    I have absolutely no idea what you mean by pointing this out.
    Last edited by VictorN; May 27th, 2014 at 08:22 AM.

  8. #8
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,553

    Re: Help Needed for CSerialPort v1.27

    If this line compiled:
    Code:
    port.Read(m_Voltin,1);
    then since Read() wants a void* then it's unlikely that m_voltin is a "public control edit variable", as indicated in the first post.

    Please post the definition of m_Voltin. It might actually be a CString that's DDX'd to an edit control.

    If so, then try this:
    Code:
    char sRxBuf[11];
    DWORD dwRead = port.Read(sRxBuf, 10);
    sRxBuf[10] = 0;
    
    CString strVoltage;
    strVoltage.Format(_T("dwRead = %d: sRxBuf = %s"), dwRead, sRxBuf);
    m_Voltin = strVoltage;
    UpdateData( FALSE );
    Note that your original format code of T("%.3f") doesn't make any sense, since you were reading only a single byte from the serial port, which can't possibly be converted by the Format() function into a float.

    Mike

  9. #9
    Join Date
    May 2014
    Posts
    17

    Re: Help Needed for CSerialPort v1.27

    how to convert the m_Voltin in CString to double so that we can manipulate the data further?

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,359

    Re: Help Needed for CSerialPort v1.27

    Quote Originally Posted by newborn2 View Post
    how to convert the m_Voltin in CString to double so that we can manipulate the data further?
    If *your* m_Voltin is of a CString type and it contains a text corresponding some double variable then
    Code:
    double dbl = _tcstod(m_Voltin);
    Victor Nijegorodov

  11. #11
    Join Date
    May 2014
    Posts
    17

    Re: Help Needed for CSerialPort v1.27

    I'm try to implement that code... but the problem is the m_Voltin is in another dialogue... I use the same method but clicking some button but my control edit is in another dialogue that being pop up...

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,359

    Re: Help Needed for CSerialPort v1.27

    I cannot help you without seeing your actual code.
    Victor Nijegorodov

  13. #13
    Join Date
    May 2014
    Posts
    17

    Re: Help Needed for CSerialPort v1.27

    this is what I'm doing.. below is my code. when I push Readpot button, the another dialogue will pop up which consists of 2 edit control box named m_Voltin1 and m_Voltin2 which double type data...

    the pop up dialogue name is IDD_Potentiometer with CPotentiometer class while the main dialogue is IDD_My3DbalanceDlg with CMy3DbalanceDlg class.

    both data are read from PIC (COMPort). thats why I ask for the conversion from CString to double.

    Then, I want to transfer the double type data to main dialogue by create the code below:

    ---------------------------------------------------------------------------------------------------
    void CMy3DbalanceDlg::OnBnClickedReadpot()
    {
    CPotentiometer dlg;

    dlg.m_Voltin1=AngleX;
    dlg.m_Voltin2=AngleZ;


    if( IDOK == dlg.DoModal() )

    {
    UpdateData();

    AngleX=dlg.m_Voltin1;
    AngleZ=dlg.m_Voltin2;

    UpdateData(FALSE);
    }


    }

  14. #14
    Join Date
    May 2014
    Posts
    17

    Re: Help Needed for CSerialPort v1.27

    the AngleX and AngleZ is the double type data define in the main dialogue..

  15. #15
    Join Date
    May 2014
    Posts
    17

    Re: Help Needed for CSerialPort v1.27

    I'm use the (http://www.naughter.com/serialport.html) code and include the .cpp file and h file into my project. I'm just include the SerialPort.h header file in my main IDD_My3DbalanceDlg.cpp. Where I should put the COMPort setup like above? in the IDD_My3DbalanceDlg.cpp or IDD_Potentiometer.cpp?

Page 1 of 2 12 LastLast

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center