我用"串口调试助手软件"往串口里发"02 30 31 3B 3B 44 37 03"2进制数8个字节.硬件有动作.可我在VC中按以下代码往串口里发同样的8个字节,硬件怎么也不动作?大家帮我分析分析,错误在什么地方?我用GetLastError()截获的写串口函数WriteFile错误号为0,WriteFile返回值也为TRUE.
CString strCmd;
strCmd="";
strCmd+=0x02;
strCmd+="01;;D7";
strCmd+=0x03; DWORD bytewriteCount =strCmd.GetLength();
BYTE *data= new byte[bytewriteCount] ;
for (int i = 0; i<bytewriteCount;i++)
{
data[i] = strCmd[i];
} WriteFile(m_hComHandle2,&data,bytewriteCount,&bytewrite,NULL);
CString strCmd;
strCmd="";
strCmd+=0x02;
strCmd+="01;;D7";
strCmd+=0x03; DWORD bytewriteCount =strCmd.GetLength();
BYTE *data= new byte[bytewriteCount] ;
for (int i = 0; i<bytewriteCount;i++)
{
data[i] = strCmd[i];
} WriteFile(m_hComHandle2,&data,bytewriteCount,&bytewrite,NULL);
BYTE bSend[] = {0x02,0x30,0x31,0x3b,0x3b,0x44,0x37,0x03};
DWORD dwWrite = 0;
if( WriteFile(m_hComHandle2,&bSend,8,&dwWrite,NULL) != TRUE )
{
TRACE("Write File Error: %d\n",GetLastError());
}
BYTE bSend[] = {0x02,0x30,0x31,0x3b,0x3b,0x44,0x37,0x03};
DWORD dwWrite = 0;
if( WriteFile(m_hComHandle2,bSend,8,&dwWrite,NULL) != TRUE )
{
TRACE("Write File Error: %d\n",GetLastError());
}
WriteFile(m_hComHandle2,&data,bytewriteCount,&bytewrite,NULL);
&data和data不是一样吗?都是数组的地址呀!
======
对于你这里是一样的。
: DWORD bytewriteCount =strCmd.GetLength();
BYTE *data= new byte[bytewriteCount] ;
for (int i = 0; i<bytewriteCount;i++)
{
data[i] = strCmd[i];
} WriteFile(m_hComHandle2,&data,bytewriteCount,&bytewrite,NULL);搞定了!
WriteFile(m_hComHandle2,&data,bytewriteCount,&bytewrite,NULL);
&data和data不是一样吗?都是数组的地址呀!