串行端口

时间:2017-10-23 13:22:15

标签: .net visual-studio visual-studio-2015 serial-port c++-cli

我有一个自定义硬件和用java编写的代码。它完美地运作。我正在用另一个只使用这种语言的应用程序用c ++重写代码,这就是问题开始的地方。当我读取硬件时,这应该是收到的数据的正确答案:

Manufacturer: NH/Resp - LPM
Product Name: ERGO AcqSyS v1.1

但是在C ++上我得到了这个:

Manufacturer: NH/Resp - LPM
Product Name: 

似乎第二次读/写操作会让我的产品名称无法正常工作。但我无法弄清楚为什么。我正在使用两个单独的数组作为答案,以确保一个答案不会干扰另一个。还尝试在第一次成功操作后从串行端口清除缓冲区,但错误仍然存​​在。 奇怪的是:如果我添加第三个写/读操作,那么应该是第二个操作输出的数据出现,但它们出现在第三行,其中新操作的响应应该是,留下空白“产品名称:”后面的空格,也没有显示第三次操作的结果。像这样:

Manufacturer: NH/Resp - LPM
Product Name:                  //"ERGO AcqSyS v1.1" should be here and not on the line below.
Hardware Version: ERGO AcqSyS v1.1       //The answer for this is not shown.

这是我正在使用的C ++ / CLI中的代码:

#include "stdafx.h"
#include "windows.h"
#using <System.dll>

using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;

public ref class Comunicacao 
{

private:
    static bool _continue;
    static SerialPort^ _serialPort;

public:
    static void Second()
    {

        auto CMD_NULL = gcnew array<System::Byte> {0x01}; //Tells the hardware that the communication is over. Must be executed after every write/read operation
        auto CMD_HELLOW_MANUFACTURER_NAME = gcnew array<System::Byte> {0x03}; //Requests the Manufacturer Name from the hardware
        auto CMD_HELLOW_PROD_NAME = gcnew array<System::Byte> {0x04}; //Requests the Product Name from the hardware
        array<Byte>^ bufferReceived;
        String^ identifierString; 
        array<Byte>^ bufferReceived2;
        String^ identifierString2; 

        // Create a new SerialPort object with default settings.
        _serialPort = gcnew SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort->PortName = "COM3";
        _serialPort->BaudRate = 9600;
        _serialPort->Parity = Parity::None;
        _serialPort->DataBits = 8;
        _serialPort->StopBits = StopBits::One;
        _serialPort->Handshake = Handshake::None;

        // Set the read/write timeouts
        _serialPort->ReadTimeout = 500;
        _serialPort->WriteTimeout = 500;

        _serialPort->Open();


//This gives the correct output

        _serialPort->Write(CMD_HELLOW_MANUFACTURER_NAME, 0, CMD_HELLOW_MANUFACTURER_NAME->Length);
        bufferReceived = gcnew array<Byte>(_serialPort->BytesToRead+1);
        _serialPort->Read(bufferReceived, 0, bufferReceived->Length);
        bufferReceived[bufferReceived->Length-1] = '\0';
        identifierString = System::Text::Encoding::ASCII->GetString(bufferReceived);
        Console::Write("Manufacturer: ");
        Console::Write(identifierString->Substring(2));
        _serialPort->Write(CMD_NULL, 0, CMD_NULL->Length);


//The code below doesn't output its result

        _serialPort->Write(CMD_HELLOW_PROD_NAME, 0, CMD_HELLOW_PROD_NAME->Length);
        bufferReceived2 = gcnew array<Byte>(_serialPort->BytesToRead + 1);
        _serialPort->Read(bufferReceived2, 0, bufferReceived2->Length);
        bufferReceived2[bufferReceived2->Length-1] = '\0';
        identifierString2 = System::Text::Encoding::ASCII->GetString(bufferReceived2);
        Console::Write("\nProduct Name: ");
        Console::Write(identifierString2->Substring(1));
        _serialPort->Write(CMD_NULL, 0, CMD_NULL->Length);

        _serialPort->Close();

    }

};


int main()
{
    Comunicacao::Second();
}

此代码来自此文档:https://msdn.microsoft.com/pt-br/library/system.io.ports.serialport(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-2

我正在使用Visual Studio 2015。新应用程序需要Visual Studio进行编译,这就是我使用它而不是其他工具的原因。 我在代码中使用substring函数,因为数组的前两个信息是执行命令的代码,不需要向用户显示。 我是C ++的新手,还有C ++ / CLI的新手,所以我很难找到错误。

有谁知道如何获得正确的输出?

感谢您的帮助!

0 个答案:

没有答案