开发者

C# Method that automates sending commands every 10 seconds

开发者 https://www.devze.com 2023-04-13 02:50 出处:网络
I have 6 devices connected to a converter RS485/USB. When sending ID0# command: (0# = 01 or 02 or 03 or 04 or 05 or 06)

I have 6 devices connected to a converter RS485/USB. When sending ID0# command: (0# = 01 or 02 or 03 or 04 or 05 or 06) receive a string IDXX  0#,22008,21930,00000, n / a, n / a ! Then the string is divided with substring() and the fragments go to variables. As should be the method which automates sending commands every 10 seconds, receive the strings, splits with substring() and stores them in variables?

I try like this: linked with a tick on timer1 with interval 10000.

 private void button6_Click(object sender, EventArgs e)
    {
        string s_ID = stringOut.Trim().Substring(1,2);
        string ID01 = "id01";
        string id01_02mm = "";
        string id01_07mm = "";
        string id01_10mm = "";
        CommPort com = CommPort.Inst开发者_如何学JAVAance;
        ID01 = ConvertEscapeSequences(ID01);
        com.Send(ID01);            

        if(s_ID=="01")
        {
           string id01_time=DateTime.Now.ToString("HH:mm:ss");
           string id01_ID = "ID01";
           id01_02mm=stringOut.Trim().Substring(4,5);
           id01_07mm=stringOut.Trim().Substring(10,5);
           id01_10mm=stringOut.Trim().Substring(16,5);
        }
    }

Thanks, ocaccy


If you want to organize your code a little more around OOP, maybe create a RS485USBDevice class that does the parsing and sending. Then you create and command 6 instances. It might look something like the following:

public class RS485USBDevice
{
    public int DeviceId { get; set; }
    public string Id_02mm { get; set; } // need more descriptive name
    public string Id_07mm { get; set; } // need more descriptive name
    public string Id_10mm { get; set; } // need more descriptive name
    public DateTime LastRequestTime { get; set; }

    public RS485USBDevice(int deviceId)
    {
        this.DeviceId = deviceId;
    }

    public void SendRequest()
    {
        // where does CommPort come from? custom class?
        CommPort com = CommPort.Instance;
        string ID = "id0" + this.DeviceId;
        ID = ConvertEscapeSequences(ID);
        com.Send(ID);  // shouldn't this have a return value

        // where does stringOut come from? naughty global variable
        this.Id_02mm = stringOut.Trim().Substring(4,5);
        this.Id_07mm = stringOut.Trim().Substring(10,5);
        this.Id_10mm = stringOut.Trim().Substring(16,5);
        this.LastRequestTime = DateTime.Now;
    }
}

public class MainClass
{
    private List<RS485USBDevice> _devices;

    public MainClass()
    {
        // initialize setup of all 6 devices
        this._devices = new List<RS485USBDevice>();
        for (int i = 1; i <= 6; i++)
        {
            var device = new RS485USBDevice(i);
            this._devices.Add(device);
        }
    }

    // this should be timer tick event on 10 seconds
    private void button6_Click(object sender, EventArgs e)
    {
        // make all devices send
        foreach (var device in this._devices)
            device.SendRequest();

        // do something with response data from device obj
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消