|
-
March 14th, 2013, 05:48 AM
#23
Re: Sending Byte to serial port
This is the complete code:
Code:
///////////////////////////////// Includes //////////////////////////////////
#include "stdafx.h"
#include "SerialPort.h"
///////////////////////////////// Defines /////////////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//////////////////////////////// Implementation ///////////////////////////////
class CSerialPortApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
CSerialPortApp theApp;
BOOL CSerialPortApp::InitInstance()
{
BYTE* pBuf = new BYTE[10000];
try
{
COMMCONFIG config;
CSerialPort::GetDefaultConfig(1, config);
CSerialPort port;
port.Open(1, 9600, 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);
DWORD dwBytesWaiting = port.BytesWaiting();
dwBytesWaiting;
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, static_cast<DWORD>(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);
dwRead; //To remove unreferrenced variable in VC 6.
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();
//Try out the overlapped functions
CSerialPort port2;
port2.Open(6, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl, TRUE);
CEvent event(FALSE, TRUE);
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
overlapped.hEvent = event;
try
{
// port2.Write(pBuf, 10000, overlapped);
char sBuf[19] = {0x01,0x02,0x04,0x01,0x02,0x09,0x0c,0x10,0x02,0x06,0x12,0x01,0x01,0x13,0x01,0x01,0x49,0xf1,0x00} ;
port2.Write(sBuf,sizeof(sBuf)/sizeof(unsigned char),overlapped );
printf("pBuf %x, \n", sBuf);
SleepEx(INFINITE, TRUE);
// printf("pBuf %x, \n", pBuf);
}
catch(CSerialException* pEx)
{
if (pEx->m_dwError == ERROR_IO_PENDING)
{
DWORD dwBytesTransferred = 0;
port2.GetOverlappedResult(overlapped, dwBytesTransferred, TRUE);
pEx->Delete();
}
else
{
DWORD dwError = pEx->m_dwError;
pEx->Delete();
CSerialPort::ThrowSerialException(dwError);
}
}
try
{
//port2.Read(pBuf, 10, overlapped);
port2.Read(pBuf, 20, overlapped);
SleepEx(INFINITE, TRUE);
printf("pBuf %x, \n", pBuf);
}
catch(CSerialException* pEx)
{
if (pEx->m_dwError == ERROR_IO_PENDING)
{
DWORD dwBytesTransferred = 0;
port2.GetOverlappedResult(overlapped, dwBytesTransferred, TRUE);
pEx->Delete();
}
else
{
DWORD dwError = pEx->m_dwError;
pEx->Delete();
CSerialPort::ThrowSerialException(dwError);
}
}
port2.SetMask(EV_TXEMPTY);
/*char sBuf[19] = {0x01,0x02,0x04,0x01,0x02,0x09,0x0c,0x10,0x02,0x06,0x12,0x01,0x01,0x13,0x01,0x01,0x49,0xf1,0x00} ;
port2.Write(sBuf,sizeof(sBuf)/sizeof(unsigned char),overlapped );
printf("pBuf %x, \n", sBuf);
SleepEx(INFINITE, TRUE);
port2.Read(pBuf, 20);
SleepEx(INFINITE, TRUE);
printf("pBuf %x, \n", pBuf);
*/
/* for testing on NT only
port2.WriteEx(sBuf, static_cast<DWORD>(strlen(sBuf)));
SleepEx(INFINITE, TRUE);
printf("pBuf %x, \n", sBuf);
port2.ReadEx(pBuf, 10);
SleepEx(INFINITE, TRUE);
*/
}
catch (CSerialException* pEx)
{
TRACE(_T("Handle Exception, Message:%s\n"), pEx->GetErrorMessage().operator LPCTSTR());
pEx->Delete();
}
delete [] pBuf;
return FALSE;
}
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
|