|
-
October 6th, 2010, 10:35 AM
#11
Re: Using C++ for USB interfacing
 Originally Posted by Alex F
Five days after asking the question you write that you are using MFC! Good way to get help.
If you are using MFC, forget about C++/CLI and use plain Windows serial port API. You can use also some native serial port wrappers, available at CodeGuru and CodeProject. Serial port access is not a reason to use C++/CLI.
I wonder why the board vendor doesn't provide C++ code example. Using VB for serial port access is strange decision. Well, you are lucky that this is not JavaScript.
Hi Alex, thanks for the advice, sorry I did not mention the MFC earlier, still a bit of a noob at that.
Anyway, I tried this: http://www.codeproject.com/KB/system...ort.aspx#Usage and have followed the class calling in the demo program
Code:
class CSerialPortApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
BOOL CSerialPortApp::InitInstance()
{
BYTE* pBuf = new BYTE[10000];
try
{
COMMCONFIG config;
CSerialPort::GetDefaultConfig(1, config);
CSerialPort port;
port.Open(4, 1200, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl);
HANDLE hPort = port.Detach();
port.Attach(hPort);
DWORD dwModemStatus;
port.GetModemStatus(dwModemStatus);
DCB dcb;
port.GetState(dcb);
dcb.BaudRate = 9600;
port.SetState(dcb);
DWORD dwErrors;
port.ClearError(dwErrors);
port.SetBreak();
port.ClearBreak();
COMSTAT stat;
port.GetStatus(stat);
COMMTIMEOUTS timeouts;
port.GetTimeouts(timeouts);
port.Setup(10000, 10000);
port.GetConfig(config);
config.dcb.BaudRate = 9600;
port.SetConfig(config);
port.Set0WriteTimeout();
port.Set0ReadTimeout();
char sBuf[] = "This should appear on the serial port";
port.Write(sBuf, strlen(sBuf));
DWORD dwMask;
port.GetMask(dwMask);
port.SetMask(EV_TXEMPTY);
//port.WaitEvent(dwMask);
port.TerminateOutstandingWrites();
port.TransmitChar('p');
port.Set0Timeout();
char sRxBuf[10];
DWORD dwRead = port.Read(sRxBuf, 10);
port.TerminateOutstandingReads();
port.ClearDTR();
port.ClearRTS();
port.SetDTR();
port.SetRTS();
port.SetXOFF();
port.SetXON();
COMMPROP properties;
port.GetProperties(properties);
port.ClearWriteBuffer();
port.ClearReadBuffer();
port.Flush();
port.Close();
}
catch (CSerialException* pEx)
{
TRACE(_T("Handle Exception, Message:%s\n"), pEx->GetErrorMessage());
pEx->Delete();
}
delete [] pBuf;
};
After doing this, I put the following code into the button
Code:
void CzebraDlg::OnBnClickedButton5()
{
char mychar;
CSerialPortApp theApp;
try {
CSerialPort port;
port.Open(4, 9600, CSerialPort::NoParity, 8,
CSerialPort::OneStopBit,
CSerialPort::XonXoffFlowControl);
char sBuf[] = "@00 ON 0"; //THIS WILL BE SENT TO TURN ON RELAYS (TAKEN FROM BOARD MANUAL)
port.Write(sBuf, strlen(sBuf));
Sleep(1000); //RELAYS WITH REMAIN ON FOR 1 SECOND
char sBuf2[] = "@00 OF 1";
port.Write(sBuf2, strlen(sBuf));
}
catch (CSerialException* pEx)
{
CerrorDlg cid; //*** Create an instance of our dialog
cid.DoModal(); //Display Dialog box
}
}
The code compiles fine but when I click the button, nothing happens. It is meant to connect to COM4. Did I make a mistake somewhere? Thanks a lot in advance!!
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
|