コード例 #1
0
 private string StopPLC()
 {
     if (isReadRandom)
     {
         return("停止成功");              // 测试模式专用
     }
     HslCommunication.OperateResult write = siemensTcpNet.Write("M102", (byte)0);
     return(write.IsSuccess ? "成功停止" : "停止失败:" + write.Message);
 }
コード例 #2
0
ファイル: Form1.cs プロジェクト: liupengh3c/ModBusTcp
        private void Btn_write_coil_send_Click(object sender, EventArgs e)
        {
            List <bool> sendData = new List <bool>();

            switch (tb_write_coil_length.Text)
            {
            case "1":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                break;

            case "2":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                break;

            case "3":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                break;

            case "4":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                break;

            case "5":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                break;

            case "6":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit5.Text == "0" ? false : true);
                break;

            case "7":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit5.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit6.Text == "0" ? false : true);
                break;

            case "8":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit5.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit6.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit7.Text == "0" ? false : true);
                break;

            case "9":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit5.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit6.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit7.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit8.Text == "0" ? false : true);
                break;

            case "10":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit5.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit6.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit7.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit8.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit9.Text == "0" ? false : true);
                break;

            case "11":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit5.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit6.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit7.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit8.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit9.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit10.Text == "0" ? false : true);
                break;

            case "12":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit5.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit6.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit7.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit8.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit9.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit10.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit11.Text == "0" ? false : true);
                break;

            case "13":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit5.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit6.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit7.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit8.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit9.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit10.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit11.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit12.Text == "0" ? false : true);
                break;

            case "14":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit5.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit6.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit7.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit8.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit9.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit10.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit11.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit12.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit13.Text == "0" ? false : true);
                break;

            case "15":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit5.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit6.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit7.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit8.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit9.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit10.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit11.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit12.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit13.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit14.Text == "0" ? false : true);
                break;

            case "16":
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit1.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit2.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit3.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit4.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit5.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit6.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit7.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit8.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit9.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit10.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit11.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit12.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit13.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit14.Text == "0" ? false : true);
                sendData.Add(tb_write_coil_data_bit15.Text == "0" ? false : true);
                break;

            default:
                sendData.Add(tb_write_coil_data_bit0.Text == "0" ? false : true);
                break;
            }
            HslCommunication.OperateResult write = busTcpClient.WriteCoil(this.tb_write_coil_address.Text, sendData.ToArray());
            string strMsg = "[" + DateTime.Now.ToLocalTime().ToString() + "] ";

            strMsg += write.IsSuccess ? "线圈写入成功。" : "线圈写入失败。错误信息:" + write.Message;
            rtb_write_coil_data.Text += strMsg + "\n";
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: liupengh3c/ModBusTcp
        private void Btn_connetc_Click(object sender, EventArgs e)
        {
            string serverIpAddress = this.tb_Server_IpAddress.Text.Trim();

            tb_Server_IpAddress.Enabled = false;
            tb_Server_IpAddress.Enabled = false;
            busTcpClient = new ModbusTcpNet(serverIpAddress, 502, 0x01);
            HslCommunication.OperateResult result = busTcpClient.ConnectServer();
            if (!result.IsSuccess)
            {
                MessageBox.Show("连接服务器失败,请重试");
                return;
            }
            btn_connect.Enabled    = false;
            btn_disconnect.Enabled = true;

            // 读线圈部分使能
            tb_coil_address.Enabled = true;
            tb_coil_length.Enabled  = true;
            rb_coil_one.Enabled     = true;
            rb_coil_two.Enabled     = true;
            rb_coil_four.Enabled    = true;
            btn_coil_send.Enabled   = true;

            // 读寄存器部分使能
            tb_register_address.Enabled = true;
            btn_register_send.Enabled   = true;
            rb_register_one.Enabled     = true;
            rb_register_two.Enabled     = true;
            rb__register_four.Enabled   = true;
            tb_register_length.Enabled  = true;

            // 读离散使能
            tb_discrete_address.Enabled = true;
            btn_discrete_send.Enabled   = true;
            rb_discrete_one.Enabled     = true;
            rb_discrete_two.Enabled     = true;
            rb_discrete_four.Enabled    = true;
            tb_discrete_length.Enabled  = true;

            // 写线圈部分使能
            tb_write_coil_address.Enabled   = true;
            tb_write_coil_length.Enabled    = true;
            rb_write_coil_one.Enabled       = true;
            rb_write_coil_two.Enabled       = true;
            rb_write_coil_four.Enabled      = true;
            btn_write_coil_send.Enabled     = true;
            tb_write_coil_data_bit0.Enabled = true;
            //tb_write_coil_data_bit1.Enabled = true;
            //tb_write_coil_data_bit2.Enabled = true;
            //tb_write_coil_data_bit3.Enabled = true;
            //tb_write_coil_data_bit4.Enabled = true;
            //tb_write_coil_data_bit5.Enabled = true;
            //tb_write_coil_data_bit6.Enabled = true;
            //tb_write_coil_data_bit7.Enabled = true;
            //tb_write_coil_data_bit8.Enabled = true;
            //tb_write_coil_data_bit9.Enabled = true;
            //tb_write_coil_data_bit10.Enabled = true;
            //tb_write_coil_data_bit11.Enabled = true;
            //tb_write_coil_data_bit12.Enabled = true;
            //tb_write_coil_data_bit13.Enabled = true;
            //tb_write_coil_data_bit14.Enabled = true;
            //tb_write_coil_data_bit15.Enabled = true;

            // 写寄存器部分使能
            tb_write_register_address.Enabled  = true;
            btn_write_register_send.Enabled    = true;
            tb_write_register_data.Enabled     = true;
            this.cb_write_register_one.Enabled = true;

            // 自动发送部分使能
            this.cb_cron_coil.Enabled     = true;
            this.cb_cron_register.Enabled = true;
            this.cb_cron_discrete.Enabled = true;
            this.tb_cron_time.Enabled     = true;
            this.btn_cron_start.Enabled   = true;
            this.btn_cron_stop.Enabled    = true;
        }
コード例 #4
0
 /// <summary>
 /// 创建并返回一个失败的结果对象,该对象复制另一个结果对象的错误信息
 /// </summary>
 /// <typeparam name="T1">目标数据类型一</typeparam>
 /// <typeparam name="T2">目标数据类型二</typeparam>
 /// <typeparam name="T3">目标数据类型三</typeparam>
 /// <typeparam name="T4">目标数据类型四</typeparam>
 /// <typeparam name="T5">目标数据类型五</typeparam>
 /// <typeparam name="T6">目标数据类型六</typeparam>
 /// <typeparam name="T7">目标数据类型七</typeparam>
 /// <typeparam name="T8">目标数据类型八</typeparam>
 /// <typeparam name="T9">目标数据类型九</typeparam>
 /// <typeparam name="T10">目标数据类型十</typeparam>
 /// <param name="result">之前的结果对象</param>
 /// <returns>带默认泛型对象的失败结果类</returns>
 public static OperateResult <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> CreateFailedResult <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(OperateResult result)
 {
     return(new OperateResult <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>( )
     {
         ErrorCode = result.ErrorCode,
         Message = result.Message,
     });
 }
コード例 #5
0
 /// <summary>
 /// 创建并返回一个失败的结果对象,该对象复制另一个结果对象的错误信息
 /// </summary>
 /// <typeparam name="T1">目标数据类型一</typeparam>
 /// <typeparam name="T2">目标数据类型二</typeparam>
 /// <typeparam name="T3">目标数据类型三</typeparam>
 /// <typeparam name="T4">目标数据类型四</typeparam>
 /// <param name="result">之前的结果对象</param>
 /// <returns>带默认泛型对象的失败结果类</returns>
 public static OperateResult <T1, T2, T3, T4> CreateFailedResult <T1, T2, T3, T4>(OperateResult result)
 {
     return(new OperateResult <T1, T2, T3, T4>( )
     {
         ErrorCode = result.ErrorCode,
         Message = result.Message,
     });
 }