16,548
社区成员




char s[9];
memset(&s, 0, sizeof(s)); //--清空发送数组缓冲区
s[0] = 0x02;
s[1] = 0x00;
s[2] = 0x04;
s[3] = 0xC1;
s[4] = 0x24;
s[5] = 0x00;
s[6] = 0x00;
s[7] = 0xE3;
s[8] = 0x03;
m_SerialPort.WriteToPort(s, 9);
void CSerialPort::WriteToPort(char* string,int n)
{
assert(m_hComm != 0);
m_szWriteBuffer = new char[strlen(string) + 1];
memcpy(m_szWriteBuffer, string, n);
m_nWriteSize = n;
SetEvent(m_hWriteEvent);
}
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]
srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
b[(a=rand()%11)]=0;
Sleep(100);
b[(a=rand()%11)]=1;
Sleep(100);
b[(a=rand()%11)]=2;
Sleep(100);
b[(a=rand()%11)]=3;
Sleep(100);
b[(a=rand()%11)]=4;
Sleep(100);
}
return 0;
}
void CSerialPort::ClosePort()
{
if(m_bIsSuspened)
{
RestartMonitoring();
}
if (m_bThreadAlive)
{
MSG message;
while (m_bThreadAlive)
{
//add by liquanhai 防止死锁 2011-11-06
if(::PeekMessage(&message, m_pOwner->m_hWnd, 0, 0, PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}
SetEvent(m_hShutdownEvent);
}
TRACE("Thread ended\n");
}
if(m_szWriteBuffer != NULL)
{
delete [] m_szWriteBuffer;
m_szWriteBuffer = NULL;
}
//if(m_hComm)
//{
CloseHandle(m_hComm);
m_hComm = NULL;
//}
// Close Handles
if(m_hShutdownEvent!=NULL)
ResetEvent(m_hShutdownEvent);
if(m_ov.hEvent!=NULL)
ResetEvent(m_ov.hEvent);
if(m_hWriteEvent!=NULL)
ResetEvent(m_hWriteEvent);
}
你试试这个,不行就按你们经理说的吧
void CSerialPort::ClosePort()
{
SetEvent(m_hShutdownEvent);
}
/*
void CSerialPort::ClosePort()
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
// if the port is still opened: close it
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}
// Close Handles
if(m_hShutdownEvent!=NULL)
CloseHandle( m_hShutdownEvent);
if(m_ov.hEvent!=NULL)
CloseHandle( m_ov.hEvent );
if(m_hWriteEvent!=NULL)
CloseHandle( m_hWriteEvent );
TRACE("Thread ended\n");
delete [] m_szWriteBuffer;
}
*/
这2个都不行
void CSerialPort::WriteToPort(char* string,int n)
{
assert(m_hComm != 0);
//m_szWriteBuffer = new char[strlen(string) + 1];
memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
memcpy(m_szWriteBuffer, string, n);
m_nWriteSize = n;
SetEvent(m_hWriteEvent);
}
这样好像就可以了,就是这发送函数错误
if (m_szWriteBuffer != NULL)
delete [] m_szWriteBuffer;
m_szWriteBuffer = new char[writebuffersize];