-
January 24th, 2011, 10:12 AM
#1
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
-
January 24th, 2011, 10:38 AM
#2
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);
-
January 24th, 2011, 11:06 AM
#3
Re: Help Needed for CSerialPort v1.27
Originally Posted by Skizmo
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.
-
January 24th, 2011, 11:23 AM
#4
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("%.3f"), voltage);
this->m_Voltin.SetWindowTextW(strVoltage);
put the 'sRxBuf' in 'voltage' and you should see something.
-
January 24th, 2011, 12:36 PM
#5
Re: Help Needed for CSerialPort v1.27
Originally Posted by Skizmo
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.
-
January 24th, 2011, 12:46 PM
#6
Re: Help Needed for CSerialPort v1.27
Originally Posted by Kappa3141
... 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):
Originally Posted by Kappa3141
... 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
-
January 24th, 2011, 01:01 PM
#7
Re: Help Needed for CSerialPort v1.27
Originally Posted by VictorN
Sure it is incorrect. Because you defined it wrong (or you passed in the wrong parameter):
I realise that. How can I correct this?
Originally Posted by VictorN
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!
Originally Posted by VictorN
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.
-
January 27th, 2011, 08:52 PM
#8
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
-
May 27th, 2014, 08:05 AM
#9
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?
-
May 27th, 2014, 08:21 AM
#10
Re: Help Needed for CSerialPort v1.27
Originally Posted by newborn2
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
-
May 27th, 2014, 08:32 AM
#11
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...
-
May 27th, 2014, 08:39 AM
#12
Re: Help Needed for CSerialPort v1.27
I cannot help you without seeing your actual code.
Victor Nijegorodov
-
May 27th, 2014, 08:50 AM
#13
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);
}
}
-
May 27th, 2014, 08:52 AM
#14
Re: Help Needed for CSerialPort v1.27
the AngleX and AngleZ is the double type data define in the main dialogue..
-
May 27th, 2014, 08:57 AM
#15
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|