我想直接按十六进制发送数据,就把值直接赋给字符型数组,别的值都还好说,接收到的都正确,可是为什么发送0就不好使,请大家指教。我的程序如下:
Sport.InitPort(this,1,9600,'n',8,1);
Sport.StartMonitoring();
char buf[100];
memset(&buf, 0, sizeof(buf));
buf[0]=0xa5;
buf[1]=0xc4;
buf[2]=0xa1;
buf[3]=0x40;
buf[4]=0x00;
buf[5]=0x00;
buf[6]=0x00;
buf[7]=0xf5;
Sport.WriteToPort(buf);
Sport.InitPort(this,1,9600,'n',8,1);
Sport.StartMonitoring();
char buf[100];
memset(&buf, 0, sizeof(buf));
buf[0]=0xa5;
buf[1]=0xc4;
buf[2]=0xa1;
buf[3]=0x40;
buf[4]=0x00;
buf[5]=0x00;
buf[6]=0x00;
buf[7]=0xf5;
Sport.WriteToPort(buf);
解决方案 »
- 模态对话框DoModal()的返回值是怎么来的
- 笑口常开 健康永驻(2009-3-2)
- [急求][暂时没有分了:(]求数值方法中的拟牛顿法的C源程序
- windows service服务程序能否直接访问数据库?
- 高分求助:多国语言显示的问题
- 如果让你设计一个 CUdpSocket 你如何布局头文件和实现文件?
- 各位高手给看看这段代码为什么会使ToolBar无法正常显示?
- 关于CFileDialog的问题!在线等!
- 点击树形控件,打开相应文件,怎么做呢?
- 本人想在一背景底图的不同位置处显示多个随数据源变化的文本(如股票显示系统),如何使这些动态文本与背景底图做到无缝。
- [交友]MFC的程序用QT写界面,感兴趣的朋友进(顺便散分)!!
- 套接字??(在线等)
串口就不认为.
blog.csdn.net/wujian53
这个函数连buf的长度都不知道,肯定不对啊!
因为他没有发送长度
{
BOOL bWrite = TRUE;
BOOL bResult = TRUE; DWORD BytesSent = 0; ResetEvent(port->m_hWriteEvent); // Gain ownership of the critical section
EnterCriticalSection(&port->m_csCommunicationSync); if (bWrite)
{
// Initailize variables
port->m_ov.Offset = 0;
port->m_ov.OffsetHigh = 0; // Clear buffer
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); bResult = WriteFile(port->m_hComm, // Handle to COMM Port
port->m_szWriteBuffer, // Pointer to message buffer in calling finction
strlen((char*)port->m_szWriteBuffer), // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&port->m_ov); // Overlapped structure // deal with any error codes
if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
// continue to GetOverlappedResults()
BytesSent = 0;
bWrite = FALSE;
break;
}
default:
{
// all other error codes
port->ProcessErrorMessage("WriteFile()");
}
}
}
else
{
LeaveCriticalSection(&port->m_csCommunicationSync);
}
} // end if(bWrite) if (!bWrite)
{
bWrite = TRUE;
bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port
&port->m_ov, // Overlapped structure
&BytesSent, // Stores number of bytes sent
TRUE); // Wait flag LeaveCriticalSection(&port->m_csCommunicationSync); // deal with the error code
if (!bResult)
{
port->ProcessErrorMessage("GetOverlappedResults() in WriteFile()");
}
} // end if (!bWrite) // Verify that the data size send equals what we tried to send
if (BytesSent != strlen((char*)port->m_szWriteBuffer))
{
TRACE("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n", BytesSent, strlen((char*)port->m_szWriteBuffer));
}
}