列表中每个对象的属性 euqals,但应该在循环中设置不同

发布于 2024-10-11 11:18:02 字数 4750 浏览 2 评论 0原文

我的代码有问题。 代码应该从 robots.txt 类型构建一个对象列表。每个机器人应该包括一个串行端口和一个名称(也许稍后还有更多属性)。 但此时,我不明白为什么任何机器人都会获得相同的名称 - 系统中最后一个可用的 com 端口的名称。

那么,谁能告诉我,为什么? (几乎在 //TBD 标记的末尾) 并且,请随意发表评论或更正其余部分。我知道,它非常臃肿,而且可能部分不是最好的代码。

所以,提前谢谢你。

#define debug_enabled 0
#define exampleclass_enabled 0

#using <System.dll>

using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;
using namespace System::Collections::Generic;
using namespace System::Timers;


public ref class Robot {
private: static String^ _name;
        static bool _active;

public:
    property String^ name
    {
        String^ get(){return _name;}
        void set (String^ newname) {_name = newname;} }
    static SerialPort^ port;
//  static String^ GetNameString() { return _name;}
    static bool IsActive() {return _active;}
//  static String^ SetName(String^ name) { _name->Copy(name);return _name;} 
    static bool SetActive(bool active) { _active = active; return _active;} 

};
public ref class CommunicatorClass
{
private:
       static System::Timers::Timer^ aTimer;
       static array<String^,2>^ commandList = gcnew array<String^,2>(6,2);


public: 
    static List<Robot^>^ _serialPortList = gcnew List<Robot^>();
    static int baudRate = 9600;
    static int dataBits = 8;
    static System::IO::Ports::StopBits stopBits = System::IO::Ports::StopBits::One;
    static System::IO::Ports::Parity parity = System::IO::Ports::Parity::None;
//  void Main(); /*initialisation of the Com-Ports*/
    static bool SendCommand(String^ command){
        //search in defined commandlist for command to send, if match, send int, if not set int to last default position(start) to wait for start as response
        int commandInArray;

        for(int i=0; i<commandList->Rank; i++)
            for (int j=0;j<1;j++) { //it doesnt care, if the command is the number or the word for it that equals the response
            if(command->Contains(commandList[i,0]))
                commandInArray = i;
            else
                commandInArray = commandList->Rank;
            }


        for each (Robot^ s in _serialPortList)
        {
            if (s->IsActive()){
                if (!s->port->IsOpen){
                    s->port->Open();
                } 

                try
                   {

                        s->port->DiscardInBuffer();
                        s->port->WriteLine(commandList[commandInArray,0]);
                        bool _temp = 0;


                        aTimer = gcnew System::Timers::Timer( 10000 );

                      // Set the Interval to 500 mseconds.
                      aTimer->Interval = 500;
                      aTimer->Enabled = true;

                        do  
                        {if (s->port->ReadLine()->Contains(commandList[commandInArray,1])) _temp = 1; // and in code of robot: /n after each line!
                        if (aTimer->Interval == 0) {
                            _temp = 1;
                            throw gcnew TimeoutException("Timeout on sending command to Robot - no response.");
                            }
                        }
                        while (_temp == 1);

                      }
            catch (TimeoutException ^) {
            return 0;}

                }
            }

        return 1;
    } /*sends the specified command to each robot marked active*/
    static bool refresh(){
        _serialPortList->Clear();
        CommunicatorClass::Main();
        return 1;
    }
    static void Main(){
        //initialize commands [*,0] and responses [*,1] in array
        commandList[0,0] = "8";
        commandList[0,1] = "Vor";
        commandList[1,0] = "6";
        commandList[1,1] = "Links";
        commandList[2,0] = "7";
        commandList[2,1] = "Zurueck";
        commandList[3,0] = "4";
        commandList[3,1] = "Rechts";
        commandList[4,0] = "5";
        commandList[4,1] = "Stop";
        commandList[5,0] = "";
        commandList[5,1] = "Start";


//          _serialPortList->Initialize;
        for each (String^ s in SerialPort::GetPortNames())
        {
#if debug_enabled
            Console::WriteLine("   {0}", s);
#endif

            Robot^ temp = gcnew Robot();
            temp->port = gcnew SerialPort(s,baudRate,parity,dataBits,stopBits);
            //temp->SetName(s);
            temp->name = s;
            _serialPortList->Add(temp); //TBD::error in dereferianciation ?!


        }
#if debug_enabled
        for each (Robot^ s in _serialPortList)
        {
            Console::WriteLine("   {0}", s->name);
        }
#endif
    }

};

I have a problem in my code.
the code should build a list of objects from the type robot. each robot should include a serial port and a name (maybe later some more atributes).
but at this point, i dont understand, why any robot gets the same name - the name of the last com-port available in the system.

so, can anyone tell me, why ? (nearly at the end on //TBD-mark)
and, please feel free to comment or correct the rest also. i know, it is many bloat and maybe partitially not the best code.

so, thank you in advance.

#define debug_enabled 0
#define exampleclass_enabled 0

#using <System.dll>

using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;
using namespace System::Collections::Generic;
using namespace System::Timers;


public ref class Robot {
private: static String^ _name;
        static bool _active;

public:
    property String^ name
    {
        String^ get(){return _name;}
        void set (String^ newname) {_name = newname;} }
    static SerialPort^ port;
//  static String^ GetNameString() { return _name;}
    static bool IsActive() {return _active;}
//  static String^ SetName(String^ name) { _name->Copy(name);return _name;} 
    static bool SetActive(bool active) { _active = active; return _active;} 

};
public ref class CommunicatorClass
{
private:
       static System::Timers::Timer^ aTimer;
       static array<String^,2>^ commandList = gcnew array<String^,2>(6,2);


public: 
    static List<Robot^>^ _serialPortList = gcnew List<Robot^>();
    static int baudRate = 9600;
    static int dataBits = 8;
    static System::IO::Ports::StopBits stopBits = System::IO::Ports::StopBits::One;
    static System::IO::Ports::Parity parity = System::IO::Ports::Parity::None;
//  void Main(); /*initialisation of the Com-Ports*/
    static bool SendCommand(String^ command){
        //search in defined commandlist for command to send, if match, send int, if not set int to last default position(start) to wait for start as response
        int commandInArray;

        for(int i=0; i<commandList->Rank; i++)
            for (int j=0;j<1;j++) { //it doesnt care, if the command is the number or the word for it that equals the response
            if(command->Contains(commandList[i,0]))
                commandInArray = i;
            else
                commandInArray = commandList->Rank;
            }


        for each (Robot^ s in _serialPortList)
        {
            if (s->IsActive()){
                if (!s->port->IsOpen){
                    s->port->Open();
                } 

                try
                   {

                        s->port->DiscardInBuffer();
                        s->port->WriteLine(commandList[commandInArray,0]);
                        bool _temp = 0;


                        aTimer = gcnew System::Timers::Timer( 10000 );

                      // Set the Interval to 500 mseconds.
                      aTimer->Interval = 500;
                      aTimer->Enabled = true;

                        do  
                        {if (s->port->ReadLine()->Contains(commandList[commandInArray,1])) _temp = 1; // and in code of robot: /n after each line!
                        if (aTimer->Interval == 0) {
                            _temp = 1;
                            throw gcnew TimeoutException("Timeout on sending command to Robot - no response.");
                            }
                        }
                        while (_temp == 1);

                      }
            catch (TimeoutException ^) {
            return 0;}

                }
            }

        return 1;
    } /*sends the specified command to each robot marked active*/
    static bool refresh(){
        _serialPortList->Clear();
        CommunicatorClass::Main();
        return 1;
    }
    static void Main(){
        //initialize commands [*,0] and responses [*,1] in array
        commandList[0,0] = "8";
        commandList[0,1] = "Vor";
        commandList[1,0] = "6";
        commandList[1,1] = "Links";
        commandList[2,0] = "7";
        commandList[2,1] = "Zurueck";
        commandList[3,0] = "4";
        commandList[3,1] = "Rechts";
        commandList[4,0] = "5";
        commandList[4,1] = "Stop";
        commandList[5,0] = "";
        commandList[5,1] = "Start";


//          _serialPortList->Initialize;
        for each (String^ s in SerialPort::GetPortNames())
        {
#if debug_enabled
            Console::WriteLine("   {0}", s);
#endif

            Robot^ temp = gcnew Robot();
            temp->port = gcnew SerialPort(s,baudRate,parity,dataBits,stopBits);
            //temp->SetName(s);
            temp->name = s;
            _serialPortList->Add(temp); //TBD::error in dereferianciation ?!


        }
#if debug_enabled
        for each (Robot^ s in _serialPortList)
        {
            Console::WriteLine("   {0}", s->name);
        }
#endif
    }

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2024-10-18 11:18:02
public ref class Robot {
private: static String^ _name;
        static bool _active;

public:
    property String^ name
    {
        String^ get(){return _name;}
        void set (String^ newname) {_name = newname;} 
    }
    static SerialPort^ port;
//  static String^ GetNameString() { return _name;}
    static bool IsActive() {return _active;}
//  static String^ SetName(String^ name) { _name->Copy(name);return _name;} 
    static bool SetActive(bool active) { _active = active; return _active;} 
};

在 Robot 的类定义中,所有内容都定义为静态。这意味着它将在该类的所有实例之间共享,并且可以作为 Robot::SerialPort 进行访问,而不需要像 Robot^ r; 这样的 Robot 实例; r->串行端口

将所有这些切换为非静态,我认为这会解决您的问题。

public ref class Robot {
private: static String^ _name;
        static bool _active;

public:
    property String^ name
    {
        String^ get(){return _name;}
        void set (String^ newname) {_name = newname;} 
    }
    static SerialPort^ port;
//  static String^ GetNameString() { return _name;}
    static bool IsActive() {return _active;}
//  static String^ SetName(String^ name) { _name->Copy(name);return _name;} 
    static bool SetActive(bool active) { _active = active; return _active;} 
};

In your class definition for Robot, you've got everything defined as static. This means it will be shared between all instances of that class, and can be accessed as Robot::SerialPort, instead of requiring an instance of Robot, like Robot^ r; r->SerialPort.

Switch all those to be non-static, and I think that'll solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文