我在串口上收到垃圾邮件

Maw*_*awg 5 c windows serial-port

我有一个Atmel mirocontroller发送数据,我希望通过COM1在我的PC上接收数据.

当我附加终端程序时,数据被正确接收(它都是ascii,除了\n之外都是可打印的).

但是,我的代码似乎正在接收垃圾邮件(非ascii字符).谁能看到我做错了什么?谢谢

发送代码,仅供参考

// USART options.
static const usart_options_t USART_CONSOLE_OPTIONS =
{
    .baudrate     = 115200,
    .charlength   = 8,
    .paritytype   = USART_NO_PARITY,
    .stopbits     = USART_1_STOPBIT,
    .channelmode  = USART_NORMAL_CHMODE
};
Run Code Online (Sandbox Code Playgroud)

接收代码

E_boolean OpenCom1(void)
{
   COMMTIMEOUTS timeouts;

   comPortHandle = CreateFile("COM1",  // Specify port device: default "COM1"
   GENERIC_READ | GENERIC_WRITE,       // Specify mode that open device.
   0,                                  // the device isn't shared.
   NULL,                               // the object gets a default security.
   OPEN_EXISTING,                      // Specify which action to take on file.
   0,                                  // default (not overlapped i/o).
   NULL);                              // default (hTemplate must be NULL for COM devices).

   if (comPortHandle == INVALID_HANDLE_VALUE)
      return False;

   deviceControlBlock.DCBlength = sizeof(deviceControlBlock);

    if((GetCommState(comPortHandle, &deviceControlBlock) == 0))
    {
      // CodeMe: do what?
      return False;
    }

    deviceControlBlock.BaudRate = CBR_115200;
    deviceControlBlock.StopBits = ONESTOPBIT;
    deviceControlBlock.Parity   = NOPARITY;
    deviceControlBlock.ByteSize = DATABITS_8;
    deviceControlBlock.fRtsControl = 0;

    if (!SetCommState(comPortHandle, &deviceControlBlock))
    {
      // CodeMe: do what?
      return False;
    }

    // set short timeouts on the comm port.
    timeouts.ReadIntervalTimeout = MAXDWORD;
    timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
    timeouts.ReadTotalTimeoutConstant = 1000;   // oen second
    timeouts.WriteTotalTimeoutMultiplier = 1;
    timeouts.WriteTotalTimeoutConstant = 1;
    if (!SetCommTimeouts(comPortHandle, &timeouts))
    {
      // CodeMe: do what?
      return False;
    }

   FlushFileBuffers(comPortHandle);

   PurgeComm (comPortHandle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

   return True;
}//OpenCom1()

// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
void      ReadCharacterFromCom1(INPUT char *theCharacter)
{
   DWORD numBytesRead;

   numBytesRead = 0;

   while (numBytesRead == 0)
   {
      ReadFile(comPortHandle,           // handle of file to read
               theCharacter,            // store read data here
               sizeof(char),            // number of bytes to read
               &numBytesRead,           // pointer to number of bytes actually read
               NULL);
   }

   return;
}//ReadCharacterFromCom1()
Run Code Online (Sandbox Code Playgroud)

小智 5

使用"sizeof(char)"调用函数"ReadFile"以读取要读取的字节数.这将被评估为1,可能不是您想要的值.结果是每次调用ReadCharacterFromCom1只会从端口读取1个有效字符并返回,其余的你看到的是缓冲区中留下的空白,因为缓冲区不是(手动)以null结尾.

建议您将其更改为:

 /* ============================================================ */
DWORD ReadCharacterFromCom1(char *pszBuffer, int nMaxCharToRead)
{
    DWORD dwBytesRead = 0;
    while (dwBytesRead == 0)
    {   ReadFile(comPortHandle, // handle of file to read
            pszBuffer,  // store read data here
            nMaxCharToRead, // number of bytes to read
            &dwBytesRead,   // pointer to number of bytes actually read
            NULL);
    }
    // terminate string with null
    pszBuffer[dwBytesRead] = 0;
    return dwBytesRead;
}

// test code ------------------------
char szBuffer[512];
DWORD dwCount = ReadCharacterFromCom1(szBuffer, sizeof(szBuffer)-1);
printf(_T("Receive %d chars: <%s>"), nCount, szBuffer);
Run Code Online (Sandbox Code Playgroud)