c - 求串口读取例子

标签 c windows serial-port

我想从 115200,n,8,1 处的 COM1 读取(最好是阻塞调用,但我可以添加它。而且我不需要线程)。

我能找到的唯一代码是在 this question 中的 Stack Overflow 上(微软也有 some useful info )。

作者说他的代码有效,我不怀疑他,但是当我运行代码时我没有收到任何字符,即使端口正确打开(如果我用终端程序检查,数据是正在发送)。

有人可以发布一些示例 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()

最佳答案

我在这段代码中看到一个问题:

    sizeof(theCharacter),

替换为

    sizeof(char),

因为你要读取一个字节,而sizeof(char*)是4或8。可能还有别的东西,但你需要显示更多代码。

此外,使用 Portmon 程序 http://technet.microsoft.com/en-us/sysinternals/bb896644.aspx查看是否收到数据 - 您可以将其与您的程序一起运行。

关于c - 求串口读取例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15082447/

相关文章:

c# - 在 C# 中获取附加 USB 设备的供应商 ID/制造商

c++ - WriteFile() 和 ReadFile() 异步不写入或读取

serial-port - Arduino伺服平滑

c++ - 在 C 或 C++ 中选择并复制 firefox 内容到剪贴板

c - 如何使用指针数组反转字符串?

c - 错误: Unknown type name

c++ - 更改 Cmake 文件以从源代码编译依赖项而不是使用 FIND_PACKAGE

android - 如何使用 NFC 将文件从 (windows) PC 发送到 android/ios?

windows - 在 Windows 下安装 Node.js 是否有任何安全隐患?

java - Java能改自己控制台的codepage吗?