private void btn_write_Click(object sender, EventArgs e) { try { if (_plc == null) { lbMsg.Text = "请先连接"; return; } if (string.IsNullOrEmpty(cb_addr.Text)) { lbMsg.Text = "请输入地址"; return; } if (string.IsNullOrEmpty(txt_val.Text)) { lbMsg.Text = "请输入数据"; return; } lbMsg.Text = "写入中"; List <DataItem> dataItems = new List <DataItem>(); var item = DataItem.FromAddressAndValue(cb_addr.Text, txt_val.Text); dataItems.Add(item); _plc.Open(); _plc.Write(dataItems.ToArray()); _plc.Close(); lbMsg.Text = "写入成功"; } catch (PlcException ex) { lbMsg.Text = ex.Message; } }