每10秒自动发送命令的C#方法

每10秒自动发送命令的C#方法,c#,methods,C#,Methods,我有6个设备连接到转换器RS485/USB。 发送ID0#命令时:(0#=01或02或03或04或05或06) 接收字符串IDXX 0#,220082193000000,不适用,不适用! 然后用substring()将字符串分割,并将片段转到变量。 应该是每10秒自动发送命令、接收字符串、使用substring()拆分并将其存储在变量中的方法吗 我试着这样: 以间隔10000的计时器1上的记号链接 private void button6_Click(object sender, Event

我有6个设备连接到转换器RS485/USB。 发送ID0#命令时:(0#=01或02或03或04或05或06) 接收字符串
IDXX 0#,220082193000000,不适用,不适用
! 然后用substring()将字符串分割,并将片段转到变量。 应该是每10秒自动发送命令、接收字符串、使用substring()拆分并将其存储在变量中的方法吗

我试着这样: 以间隔10000的计时器1上的记号链接

 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.Instance;
        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);
        }
    }
谢谢,
ocaccy

如果您想围绕OOP对代码进行更多的组织,可以创建一个执行解析和发送的
RS485USBDevice
类。然后创建并命令6个实例。它可能看起来如下所示:

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
    }
}
公共类RS485USB设备
{
公共int设备ID{get;set;}
公共字符串Id_02mm{get;set;}//需要更具描述性的名称
公共字符串Id_07mm{get;set;}//需要更具描述性的名称
公共字符串Id_10mm{get;set;}//需要更具描述性的名称
公共日期时间LastRequestTime{get;set;}
公共RS485USB设备(内部设备ID)
{
this.DeviceId=DeviceId;
}
公共无效发送请求()
{
//CommPort来自哪里?自定义类?
CommPort com=CommPort.Instance;
string ID=“id0”+this.DeviceId;
ID=转换转义序列(ID);
com.Send(ID);//这不应该有一个返回值吗
//stringOut从何而来?顽皮的全局变量
this.Id_02mm=stringOut.Trim()子字符串(4,5);
this.Id_07mm=stringOut.Trim()子字符串(10,5);
this.Id_10mm=stringOut.Trim()子串(16,5);
this.LastRequestTime=DateTime.Now;
}
}
公共类主类
{
专用列表设备;
公共类()
{
//初始化所有6个设备的设置
这是。_设备=新列表();

对于(int i=1;i你的问题是什么?你尝试的方法似乎有什么问题?我的意思是我有这个代码。但是我认为它非常简单,可以通过使用OOP来改进,因为它们是6个设备,在这个示例中我只使用一个!我的意思是我有这个代码。但是我认为它非常简单,可以n使用OOP可以提高性能,因为它们是6个设备,在本例中,我只使用了一个!几分钟后,我将询问如何启动这些值​​在310行120列的数组中。示例我只使用了一个!谢谢,mellamokb我的朋友mellamokb,如何给你一个拥抱?很好,赢得了一个粉丝!我们真诚地感谢你。