2,586
社区成员
发帖
与我相关
我的任务
分享
void CmainView::openCom()
{
if (!m_bcom)//打开串口
{
if (m_SerialPort.InitPort(this, 4, 9600, 'N', 8, 1, m_dwCommEvents, 512))
{
m_SerialPort.StartMonitoring();
m_bcom = TRUE;
}
else
{
AfxMessageBox("打开串口失败");
return;
}
}
}
void CmainView::colseCom()//关闭
{
// TODO: 在此添加控件通知处理程序代码
if(m_bcom)
{
m_SerialPort.ClosePort();
m_bcom = FALSE;
}else
{
MessageBoxDlg("串口未打开!");
}
}
void CmainView::WriteB12()//发送
{
// TODO: 在此添加控件通知处理程序代码
char array[1024] = {0};
CString strSend;
strSend = "01030000001445c5";
m_icom = 45;
if(m_bcom)
{
int len = strTohex(strSend, array);
m_SerialPort.WriteToPort(array, len);
}else
{
MessageBoxDlg("串口未打开!");
}
}
//按钮点击事件
void CmainView::OnBnClickedBtnOpen()
{
// TODO: 在此添加控件通知处理程序代码
openCom();
WriteB12();
}
void CmainView::OnBnClickedBtnClose()
{
// TODO: 在此添加控件通知处理程序代码
colseCom();
}
void CSerialPort::WriteToPort(char* string,int n)
{
assert(m_hComm != 0);
m_szWriteBuffer = new char[n + 1];
memcpy(m_szWriteBuffer, string, n);
m_nWriteSize = n;
SetEvent(m_hWriteEvent);
}
n是string长度,以前代码是new char[strlen(string) + 1];
不知道为啥错了,不是16进制发送那个也有问题,也是这个发送函数,我怀疑我下载的这个文件是错的,因为百度其他人都没有遇到这个问题。。。。。。
BOOL CSerialPort::InitPort(CWnd* pPortOwner, // the owner (CWnd) of the port (receives message)
UINT portnr, // portnumber (1..4)
UINT baud, // baudrate
char parity, // parity
UINT databits, // databits
UINT stopbits, // stopbits
DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
UINT writebuffersize) // size to the writebuffer
{
assert(portnr > 0 && portnr < 8);
assert(pPortOwner != NULL);
if(m_bThreadAlive)
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE("Thread ended\n");
}
if(m_ov.hEvent != NULL)
{
ResetEvent(m_ov.hEvent);
}
else
{
m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
if(m_hWriteEvent != NULL)
{
ResetEvent(m_hWriteEvent);
}
else
{
m_hWriteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
if(m_hShutdownEvent != NULL)
{
ResetEvent(m_hShutdownEvent);
}
else
{
m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
m_hEventArray[0] = m_hShutdownEvent; // highest priority
m_hEventArray[1] = m_ov.hEvent;
m_hEventArray[2] = m_hWriteEvent;
InitializeCriticalSection(&m_csCommunicationSync);
m_pOwner = pPortOwner;
if (m_szWriteBuffer != NULL)
{
delete [] m_szWriteBuffer;
}
m_szWriteBuffer = new char[writebuffersize];
m_nPortNr = portnr;
m_nWriteBufferSize = writebuffersize;
m_dwCommEvents = dwCommEvents;
BOOL bResult = FALSE;
char *szPort = new char[50];
char *szBaud = new char[50];
EnterCriticalSection(&m_csCommunicationSync);
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}
sprintf(szPort, "COM%d", portnr);
sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits);
m_hComm = CreateFile(szPort, // communication port string (COMX)
GENERIC_READ | GENERIC_WRITE, // read/write types
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // Async I/O
0); // template must be 0 for comm devices
if (m_hComm == INVALID_HANDLE_VALUE)
{
delete [] szPort;
delete [] szBaud;
return FALSE;
}
m_CommTimeouts.ReadIntervalTimeout = 1000;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
m_CommTimeouts.WriteTotalTimeoutConstant = 1000;
if(SetCommTimeouts(m_hComm, &m_CommTimeouts))
{
if(SetCommMask(m_hComm, dwCommEvents))
{
if(GetCommState(m_hComm, &m_dcb))
{
m_dcb.EvtChar = 'q';
m_dcb.fRtsControl = RTS_CONTROL_ENABLE; // set RTS bit high!
if (BuildCommDCB(szBaud, &m_dcb))
{
if (SetCommState(m_hComm, &m_dcb)); // normal operation... continue
else
{
ProcessErrorMessage("SetCommState()");
}
}
else
{
ProcessErrorMessage("BuildCommDCB()");
}
}
else
{
ProcessErrorMessage("GetCommState()");
}
}
else
{
ProcessErrorMessage("SetCommMask()");
}
}
else
{
ProcessErrorMessage("SetCommTimeouts()");
}
delete [] szPort;
delete [] szBaud;
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
LeaveCriticalSection(&m_csCommunicationSync);
TRACE("Initialisation for communicationport %d completed.\nUse Startmonitor to communicate.\n", portnr);
return TRUE;
}
错误代码
if (m_szWriteBuffer != NULL)
{
delete [] m_szWriteBuffer;//这一句错了
}
m_szWriteBuffer = new char[writebuffersize];