【问题标题】:Reading String on Serial COM Port在串行 COM 端口上读取字符串
【发布时间】:2014-08-13 07:32:07
【问题描述】:

我正在做一个串行 COM 端口 ANSI 字符读取项目。我可以处理发送数据,但我无法处理接收它。任何帮助都会得到帮助。

这是读取函数:

BOOL ReadString(char *outstring)
{
    int *length;
    *length = sizeof(int);
    BYTE data;
    BYTE dataout[8192]={0};
    int index = 0;
    while(ReadByte(data)== TRUE)
    {
        dataout[index++] = data;
    }
    memcpy(outstring, dataout, index);
    *length = index;
    return TRUE;
}

而 Main.cpp 是:

int main(void)
{
    //  Configs
    hPort = ConfigureSerialPort("COM1");
    if(hPort == NULL)
    {
        printf("Com port configuration failed\n");
        return -1;
    }


    char* cPTR;
    for(;;)
           {
                ReadString(cPTR);
                if(cPTR!=NULL) cout << "Scanned Data: " << cPTR << endl;
                else cout << "No data recieved." << endl;
    }
    ClosePort();
    return 0;
}

【问题讨论】:

  • 出了什么问题? cPTR 从未初始化,因此可能会导致问题,但由于您没有明确说明哪里出了问题,所以很难说。
  • 您是否尝试使用 std::string 重写它以逃避手动内存分配?另一点是你有危险的阅读没有边界检查。
  • int *length; *length = sizeof(int); 这已经足够崩溃了。
  • 如果输入数据中间包含0,剩余数据会丢失。 COM口是二进制流,不是面向文本的。

标签: c++ serial-port dev-c++


【解决方案1】:

有多个错误:使用 C 字符串而不是 std::string、没有边界检查等:

BOOL ReadString(char *outstring)
{
    int *length; // !!!! length is not initialized, it may point to any address
    *length = sizeof(int);

    BYTE data;
    BYTE dataout[8192]={0};
    int index = 0;

    while(ReadByte(data)== TRUE) // !!!! No bound check. What if index will become more than 8192?
    {
        dataout[index++] = data;
    }
    memcpy(outstring, dataout, index);
    *length = index; // Hmm, length is not static it is automatic variable and will not keep the index between the calls

    return TRUE;
}

主要:

int main(void)
{
    //  Configs
    hPort = ConfigureSerialPort("COM1");
    if(hPort == NULL)
    {
        printf("Com port configuration failed\n");
        return -1;
    }


    char* cPTR; // !!!! Not initialized, points to any place in memory
    for(;;)
    {
      ReadString(cPTR); // !!!! cPTR not allocated, you pass the pointer to somwhere

      if(cPTR!=NULL)
        cout << "Scanned Data: " << cPTR << endl;
      else
        cout << "No data recieved." << endl;
    }
    ClosePort();
    return 0;
}

我对读取字符串功能的建议:

void ReadString(
  std::string& result,
  std::size_t& size)
{
  result.clear(); // If you need to keep track - don't clear

  BYTE byteIn;
  while (ReadByte(byteIn))
  {
    result.append(byteIn); // Maybe static_cast<char>(byteIn) required for BYTE to char
    size++;
  }
}

现在我们可以将 main 重写为:

std::string buffer;
std::size_t length = 0;

while (true)
{
  ReadString(buffer, length);

  if(buffer.size())
    cout << "Scanned Data: " << buffer << endl;
  else
    cout << "No data recieved." << endl;

  // Some condition to break the cycle after 1Mb
  if (length > 1024 * 1024)
    break;
}

【讨论】:

    猜你喜欢
    • 2012-08-09
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 2022-12-15
    相关资源
    最近更新 更多