I get trash on the serial port

I have an Atmel mirocontroller sending data that I want to receive on my PC via COM1.

When I connect the terminal program, the data is received correctly (this is all ascii, all for printing, except \ n).

However, my code seems to get unwanted (non-ascii characters). Can anyone see what I'm doing wrong? Thanks

Sending a code, for information only

// 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
};

Code retrieval

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()
+5
source share
2 answers

"ReadFile" "sizeof (char)" . 1, , , . ReadCharacterFromCom1 1 , , , - , () .

:

 /* ============================================================ */
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);
+5

, , , , - . (), DCB.

/​​.

, , .

, ASCII . . RTS / DTR.

RS232/V.24, ( Xon/Xoff). Xon/Xoff-handshake ASCII. , ASCII. , base64.

, : http://www.cplusplus.com/forum/windows/89698/

http://msdn.microsoft.com/en-us/library/ff802693.aspx .

+2

All Articles