1

COM1の115200、n、8,1から読み取りたい(できればブロッキング呼び出しですが、追加できます。スレッド化は必要ありません)。

私が見つけることができる唯一のコードは、この質問のStack Overflowにあります(Microsoftにもいくつかの有用な情報がありました)。

作者は彼のコードは機能していると言っており、私は彼を疑うことはありませんが、コードを実行すると、ポートが正しく開いていても文字が表示されません(ターミナルプログラムで確認すると、データが送信されています) 。

誰かがCコードの例へのURLを投稿できますか?ありがとう。

FWIW、これが私のコードです:

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

#if 0
// alternative to GetCommState() and SetCommState()
// both versions succeed
   if (!BuildCommDCB("115200,n,8,1", &deviceControlBlock))
    {
      // CodeMe: do what?
      return False;
    }
#endif 

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

   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(theCharacter),    // number of bytes to read
               &numBytesRead,           // pointer to number of bytes actually read
               NULL);
   }
   return;
}//ReadCharacterFromCom1()
4

1 に答える 1

2

I see one problem in this code:

    sizeof(theCharacter),

replace this with

    sizeof(char),

because you want to read one byte, and sizeof(char*) is 4 or 8. Possibly there is something else, but you need to show more code.

Also, use Portmon program http://technet.microsoft.com/en-us/sysinternals/bb896644.aspx to see whether data is received - you can run it together with your program.

于 2013-02-26T06:20:53.890 に答える