我想直接按十六进制发送数据,就把值直接赋给字符型数组,别的值都还好说,接收到的都正确,可是为什么发送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);

解决方案 »

  1.   

    你所说他的是谁,如何是BUf, 就认为是.
    串口就不认为.
      

  2.   

    我有自己写的串口类库, 可以发16进制, 类似(Com1.Write(p, 8);), 比较简单;
    blog.csdn.net/wujian53
      

  3.   

    Sport.WriteToPort(buf);
    这个函数连buf的长度都不知道,肯定不对啊!
      

  4.   

    他的WriteToPort()大概是发送以‘\0’结尾的字符串的,猜的
    因为他没有发送长度
      

  5.   

    void CSerialPort::WriteChar(CSerialPort* port)
    {
    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));
    }
    }