wince 串口通讯,只能发8个字节,发10个字节以上就死机!
[复制链接]
初始化 void CPLCDlg::OnInitial()
{
// TODO: Add your control notification handler code here
DCB commParam;
if (m_hComm != INVALID_HANDLE_VALUE)// 已经打开的话,直接返回
{
AfxMessageBox(_T("have created"));
return;
}
m_hComm = CreateFile(
_T("COM1:"),
GENERIC_READ | GENERIC_WRITE, //允许读和写
0, //独占方式(共享模式)
NULL,
OPEN_EXISTING, //打开而不是创建(创建方式)
0,
NULL
);
if (m_hComm == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("CreateFile 返回无效句柄"));
return ;
}
if (!GetCommState(m_hComm,&commParam))
{
AfxMessageBox(_T("无法得到参数"));
return ;
}
//commParam.BaudRate = baud; // 设置波特率
commParam.BaudRate = 2400;
commParam.fBinary = TRUE; // 设置二进制模式,此处必须设置TRUE
commParam.fParity = TRUE; // 支持奇偶校验
commParam.ByteSize = 8;
commParam.Parity = EVENPARITY; // 校验模式
commParam.StopBits = 0;
commParam.fOutX = 0; // No XON/XOFF out flow control
commParam.fInX = 0; // No XON/XOFF in flow control
commParam.fTXContinueOnXoff=0;
commParam.fRtsControl = 0;
commParam.fDtrControl = 0;
commParam.fAbortOnError = FALSE; // 当串口发生错误,并不终止串口读写
if (!SetCommState(m_hComm, &commParam))
{
AfxMessageBox(_T("SetCommState error"));
return ;
}
//设置串口读写时间
COMMTIMEOUTS CommTimeOuts;
GetCommTimeouts(m_hComm, &CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout =100;
CommTimeOuts.ReadTotalTimeoutMultiplier = 1;
CommTimeOuts.ReadTotalTimeoutConstant = 100;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 0;
if(!SetCommTimeouts( m_hComm, &CommTimeOuts ))
{
AfxMessageBox( _T("SetCommTimeouts 返回错误") );
return ;
}
//指定端口监测的事件集
SetCommMask (m_hComm, EV_RXCHAR);
//分配设备缓冲区
SetupComm(m_hComm,1024,1024);
//初始化缓冲区中的信息
PurgeComm(m_hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);
//AfxMessageBox(_T("串口打开成功"));
return;
}
发送数据:
void CPLCDlg::OnFasong()
{
// TODO: Add your control notification handler code here
unsigned char *buf;
int bufLen=8;//如果缓冲区变为10以上,则要死机
buf=new unsigned char(150);
CopyMemory(buf,sin_value_int,bufLen);//sin_value_int【50】为一特征值数组,里面有值。
PurgeComm(m_hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);
DWORD dwNumBytesWritten;
WriteFile (m_hComm,buf,bufLen*sizeof,&dwNumBytesWritten,NULL);
delete [] buf;
return ;
}
代码很简单,但是一直弄不明白为什么这样.当发8个字节的时候,界面怎么点也死不了,但是一发送10个字节以上就挂了.
单步调式的时候还出现:First-chance exception in PLC.exe: 0xC0000005: Access Violation
SENDING command id 0x03EB to CDialog target.
SENDING command id 0x03EB to CDialog target.
SENDING command id 0x03EC to CDialog target.
SENDING command id 0x03EC to CDialog target.
SENDING command id 0x03EC to CDialog target.
SENDING command id 0x03EC to CDialog target.
SENDING command id 0x03ED to CDialog target.
SENDING command id 0x03EE to CDialog target.
Data Abort: Thread=8d3419f4 Proc=8c0b7ab0 'PLC.exe'
AKY=00040001 PC=01f83830 RA=01f841f8 BVA=26055518 FSR=00000007
希望大虾们看下,小弟已经弄得头都炸了。也可以联系QQ409324478 谢谢
我还是新人,分数给不了。。。。。。