private void DataForm_Load(object sender, EventArgs e) { UdpSever.ReturnMsg rm = UdpSever.Read_Multiple_Registers(deviceNum, 0, UdpSever.RegisterNum); if (rm.resault) { for (int i = 0; i < rm.DataBuf.Length; i++) { UdpSever.Register[deviceNum, i, 0] = rm.DataBuf[i]; UdpSever.Register[deviceNum, i, 1] = UdpSever.DateTimeToStamp(DateTime.Now); } } else { UdpSever.Shell.WriteError("错误信息", "读取失败!"); } //加载数据 for (int i = 0; i < UdpSever.RegisterNum; i++) { ListViewItem item = new ListViewItem(); item.Text = i.ToString(); //"寄存器" item.SubItems.Add(UdpSever.Register[deviceNum, i, 1].ToString()); //"时间戳" item.SubItems.Add(UdpSever.Register[deviceNum, i, 0].ToString()); //"十进制" item.SubItems.Add(UdpSever.Register[deviceNum, i, 0].ToString("X2")); // "十六进制" item.SubItems.Add(Convert.ToString(UdpSever.Register[deviceNum, i, 0], 2).PadLeft(16, '0')); //"二进制" byte[] bt = new byte[2]; bt[0] = (byte)(UdpSever.Register[deviceNum, i, 0] >> 8); bt[1] = (byte)(UdpSever.Register[deviceNum, i, 0]); string str = Encoding.GetEncoding("GB2312").GetString(bt, 0, 2).Replace("\0", ""); item.SubItems.Add(str);//"字符串" doubleBufferListView1.Items.Add(item); } //启动自动更新进程 th = new Thread(fun); th.Start(); }
private void sendstr(TextBox tx) { UdpSever.ReturnMsg returnmsg = new UdpSever.ReturnMsg(); if (tx.Text.Length > 0) { byte[] byt = Encoding.Default.GetBytes(textBox_str1.Text); UInt16 data = 0; if (byt.Length > 0) { if (byt.Length == 1) { data = (UInt16)(byt[0]); } else if (byt.Length == 2) { data = (UInt16)((byt[0] << 8) | byt[1]); } returnmsg = UdpSever.Write_Register(deviceID, registerID, data); // Console.WriteLine(string.Format("写单个字节结果:{0}\r\n", returnmsg.resault.ToString())); if (!returnmsg.resault) { MessageBox.Show("写入失败!"); } UdpSever.Register[deviceID, registerID, 0] = data; } } }
//读多个寄存器 private void button1_Click(object sender, EventArgs e) { //UdpSever.Log("debug", "开始执行{0}", DateTime.Now.ToLocalTime()); var bb = sender as Button; bb.Enabled = false; UdpSever.ReturnMsg rm = UdpSever.Read_Multiple_Registers(deviceNum, 0, 128); bb.Enabled = true; //UdpSever.Log("debug", "结束{0}", DateTime.Now.ToLocalTime()); }
//写多个寄存器 private void button2_Click(object sender, EventArgs e) { Random rd = new Random(); UInt16[] data = new ushort[128]; for (int i = 0; i < data.Length; i++) { data[i] = (ushort)(rd.Next(1, 1000)); } UdpSever.ReturnMsg rm = UdpSever.Write_Multiple_Registers(deviceNum, 0, 128, data); //Console.WriteLine("写入结果:\r\n{0}", rm.ToString()); }
private void button_0_Click(object sender, EventArgs e) { Int64 data = UdpSever.Register[deviceID, registerID, 0]; data ^= 1; UdpSever.ReturnMsg returnmsg = new UdpSever.ReturnMsg(); returnmsg = UdpSever.Write_Register(deviceID, registerID, (UInt16)data); //Console.WriteLine(string.Format("写单个字节结果:{0}\r\n", returnmsg.resault.ToString())); if (!returnmsg.resault) { MessageBox.Show("写入失败!"); } }
private void senddec(TextBox tx) { UdpSever.ReturnMsg returnmsg = new UdpSever.ReturnMsg(); try { if (tx.Text.Length > 0) { UInt16 data = UInt16.Parse(tx.Text); returnmsg = UdpSever.Write_Register(deviceID, registerID, data); // Console.WriteLine(string.Format("写单个字节结果:{0}\r\n", returnmsg.resault.ToString())); if (!returnmsg.resault) { MessageBox.Show("写入失败!"); } } } catch { } }
private void button1_Click(object sender, EventArgs e) { //读取寄存器值 UdpSever.ReturnMsg rm = UdpSever.Read_Register(deviceNum, 1); Console.WriteLine("读取结果:{0},{1}", rm.resault, rm.Data.ToString("X2")); }