コード例 #1
0
        private void button6_Click(object sender, EventArgs e)
        {
            tabControl1.Enabled = false;
            timer1.Enabled      = true;
            byte[] tx = new byte[2];
            if (rbp1on.Checked)
            {
                tx[0] = 0x01;
            }
            else
            {
                tx[0] = 0x00;
            }
            if (rbp2on.Checked)
            {
                tx[1] = 0x01;
            }
            else
            {
                tx[1] = 0x00;
            }

            byte[] tx03 = CP1616_NoAddr_Packet.MakeCP1616_NoAddr_Packet(0x07, tx);
            tcpAsyncServer.Send(Form1.mcuClientContext, tx03);
            this.Invoke((EventHandler)(delegate
            {
                textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                textBox1.AppendText("发送到" + Form1.mcuClientContext.clientEndPoint.ToString() + ":");
                textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(tx03, " "));
                textBox1.AppendText("\r\n");
            }));
        }
コード例 #2
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (mcuClientContext != null)
            {
                byte[] tx = CP1616_NoAddr_Packet.MakeCP1616_NoAddr_Packet(0x01);
                this.Invoke((EventHandler)(delegate
                {
                    textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                    textBox1.AppendText("发送到" + mcuClientContext.clientEndPoint.ToString() + ":");
                    textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(tx, " "));
                    textBox1.AppendText("\r\n");
                }));
                tcpAsyncServer.Send(mcuClientContext, tx);
            }
            //看看是否有未知的板子接入
            ClientContext[] clientList = tcpAsyncServer.ClientContextList;
            foreach (ClientContext client in clientList)
            {
                if (mcuClientContext != null)
                {
                    if (client == mcuClientContext)
                    {
                        continue;
                    }
                }
                byte[] tx = CP1616_NoAddr_Packet.MakeCP1616_NoAddr_Packet(0xf0);
                tcpAsyncServer.Send(client, tx);
            }
            //if (bPic)
            //{

            //}
        }
コード例 #3
0
 private void tcpAsyncServer_AcceptServer(object sender, AcceptServerEventArgs e)
 {
     this.Invoke((EventHandler)(delegate
     {
         NetLog(DateTime.Now.ToLongTimeString() + ":");
         NetLog("Accept:" + e.Client.ClientSocket.RemoteEndPoint.ToString());
         NetLog("\r\n");
     }));
     byte[] tx = CP1616_NoAddr_Packet.MakeCP1616_NoAddr_Packet(0xf0);
     tcpAsyncServer.Send(e.Client, tx);
 }
コード例 #4
0
 private void button5_Click(object sender, EventArgs e)
 {
     tabControl1.Enabled = false;
     timer1.Enabled      = true;
     byte[] tx06 = CP1616_NoAddr_Packet.MakeCP1616_NoAddr_Packet(0x06);
     tcpAsyncServer.Send(clientContext, tx06);
     this.Invoke((EventHandler)(delegate
     {
         textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
         textBox1.AppendText("发送到" + clientContext.clientEndPoint.ToString() + ":");
         textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(tx06, " "));
         textBox1.AppendText("\r\n");
     }));
 }
コード例 #5
0
 private void button3_Click(object sender, EventArgs e)
 {
     tabControl1.Enabled = false;
     timer1.Enabled      = true;
     byte[] tx = new byte[2];
     tx[0] = BytesOP.GetHighByte((ushort)(numericUpDown3.Value * 10));
     tx[1] = BytesOP.GetLowByte((ushort)(numericUpDown3.Value * 10));
     byte[] tx03 = CP1616_NoAddr_Packet.MakeCP1616_NoAddr_Packet(0x04, tx);
     tcpAsyncServer.Send(Form1.mcuClientContext, tx03);
     this.Invoke((EventHandler)(delegate
     {
         textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
         textBox1.AppendText("发送到" + Form1.mcuClientContext.clientEndPoint.ToString() + ":");
         textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(tx03, " "));
         textBox1.AppendText("\r\n");
     }));
 }
コード例 #6
0
ファイル: Form1.cs プロジェクト: wangf0228GitHub/ZXJ
 private void timer1_Tick(object sender, EventArgs e)
 {
     //看看是否有未知的板子接入
     ClientContext[] clientList = tcpAsyncServer.ClientContextList;
     foreach (ClientContext client in clientList)
     {
         if (mcuClientContext != null)
         {
             if (client == mcuClientContext)
             {
                 continue;
             }
         }
         byte[] tx = CP1616_NoAddr_Packet.MakeCP1616_NoAddr_Packet(0xf0);
         tcpAsyncServer.Send(client, tx);
     }
 }
コード例 #7
0
ファイル: Form2.cs プロジェクト: wangf0228GitHub/ZXJ
 private void button1_Click(object sender, EventArgs e)
 {
     if (Form1.mcuClientContext != null)
     {
         tabControl1.Enabled = false;
         timer1.Enabled      = true;
         byte[] tx = CP1616_NoAddr_Packet.MakeCP1616_NoAddr_Packet(0x02);
         tcpAsyncServer.Send(Form1.mcuClientContext, tx);
         this.Invoke((EventHandler)(delegate
         {
             textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
             textBox1.AppendText("发送到" + Form1.mcuClientContext.clientEndPoint.ToString() + ":");
             textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(tx, " "));
             textBox1.AppendText("\r\n");
         }));
     }
     else
     {
         MessageBox.Show("单片机采集板尚未接入,请稍后重试!!");
     }
 }
コード例 #8
0
        private void tcpAsyncServer_ReceiveServerEvent(object sender, ReceiveServerEventArgs e)
        {
            CP1616_NoAddr_Packet rx = (CP1616_NoAddr_Packet)e.netPacket;
            IPEndPoint           ip = (IPEndPoint)e.Client.ClientSocket.RemoteEndPoint;

            this.Invoke((EventHandler)(delegate
            {
                textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " ") + "\r\n");
                if (rx.Header.Command == 0x50)//握手电源控制器状态上报
                {
                    double f;
                    byte[] fb = new byte[4];
                    int indexLi = -1;
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        if (listView1.Items[i].Text == ip.Address.ToString())
                        {
                            indexLi = i;
                            break;
                        }
                    }
                    if (indexLi == -1)
                    {
                        NetLog(DateTime.Now.ToLongTimeString() + ":");
                        NetLog("未处理ip数据:" + e.Client.ClientSocket.RemoteEndPoint.ToString());
                        NetLog("\r\n");
                        return;
                    }
                    string strLog = e.Client.ClientSocket.RemoteEndPoint.ToString() + ":";
                    //MS8607
                    if (rx.Data[12] > 10)
                    {
                        listView1.Items[indexLi].SubItems[2].Text = "xxx.x";
                        listView1.Items[indexLi].SubItems[3].Text = "xxx.x";
                        listView1.Items[indexLi].SubItems[4].Text = "xxx.x";
                    }
                    else
                    {
                        f = BitConverter.ToSingle(rx.Data, 0);
                        listView1.Items[indexLi].SubItems[2].Text = f.ToString("F2");
                        strLog += f.ToString("F2") + ",";
                        f = BitConverter.ToSingle(rx.Data, 4);
                        //f = f;
                        listView1.Items[indexLi].SubItems[3].Text = f.ToString("F2");
                        strLog += f.ToString("F2") + ",";
                        f = BitConverter.ToSingle(rx.Data, 8);
                        listView1.Items[indexLi].SubItems[4].Text = f.ToString("F2");
                        strLog += f.ToString("F2") + ",";
                    }
                    if (rx.Data[13] == 0)
                    {
                        listView1.Items[indexLi].SubItems[5].Text = "漏水";
                    }
                    else
                    {
                        listView1.Items[indexLi].SubItems[5].Text = "正常";
                    }
                    strLog += listView1.Items[indexLi].SubItems[5].Text + ",";
                    if (rx.Data[14] == 0)
                    {
                        listView1.Items[indexLi].SubItems[6].Text = "漏水";
                    }
                    else
                    {
                        listView1.Items[indexLi].SubItems[6].Text = "正常";
                    }
                    strLog += listView1.Items[indexLi].SubItems[5].Text + ",";
                    if (rx.Data[15] == 0)
                    {
                        listView1.Items[indexLi].SubItems[7].Text = "断电";
                    }
                    else
                    {
                        listView1.Items[indexLi].SubItems[7].Text = "供电";
                    }
                    strLog += listView1.Items[indexLi].SubItems[5].Text;
                    NetLog(DateTime.Now.ToLongTimeString() + ":");
                    NetLog(strLog);
                    NetLog("\r\n");
                }
                else
                {
                    NetLog(DateTime.Now.ToLongTimeString() + ":");
                    NetLog("未知指令数据:" + e.Client.ClientSocket.RemoteEndPoint.ToString());
                    NetLog("\r\n");
                    NetLog(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " "));
                    NetLog("\r\n");
                }
            }));
        }
コード例 #9
0
ファイル: Form2.cs プロジェクト: wangf0228GitHub/ZXJ
        private void tcpAsyncServer_ReceiveServerEvent(object sender, ReceiveServerEventArgs e)
        {
            IPEndPoint ip = (IPEndPoint)e.Client.ClientSocket.RemoteEndPoint;

            //if (ip.Address.Equals(Form1.mcuSocketIP.Address))
            if (Form1.mcuClientContext != null && e.Client == Form1.mcuClientContext)
            {
                this.Invoke((EventHandler)(delegate
                {
                    CP1616_NoAddr_Packet rx = (CP1616_NoAddr_Packet)e.netPacket;
                    if (rx.Header.Command == 0x02)                    //读取网络参数
                    {
                        tabControl1.Enabled = true;
                        timer1.Enabled = false;
                        textBox6.Text = StringsFunction.byteToHexStr(rx.Data, 0, 8, " ");
                        byte[] bIP = new byte[4];
                        for (int i = 0; i < 4; i++)
                        {
                            bIP[i] = rx.Data[8 + i];
                        }
                        IPAddress mcuIP = new IPAddress(bIP);
                        textBox2.Text = mcuIP.ToString();

                        for (int i = 0; i < 4; i++)
                        {
                            bIP[i] = rx.Data[12 + i];
                        }
                        mcuIP = new IPAddress(bIP);
                        textBox3.Text = mcuIP.ToString();

                        for (int i = 0; i < 4; i++)
                        {
                            bIP[i] = rx.Data[16 + i];
                        }
                        mcuIP = new IPAddress(bIP);
                        textBox4.Text = mcuIP.ToString();

                        for (int i = 0; i < 4; i++)
                        {
                            bIP[i] = rx.Data[24 + i];
                        }
                        mcuIP = new IPAddress(bIP);
                        textBox5.Text = mcuIP.ToString();

                        numericUpDown2.Value = BytesOP.MakeShort(rx.Data[0x1d], rx.Data[0x1c]);

                        numericUpDown1.Value = BytesOP.MakeShort(rx.Data[0x21], rx.Data[0x20]);
                    }
                    else if (rx.Header.Command == 0x03)                    //设定网络参数
                    {
                        tabControl1.Enabled = true;
                        timer1.Enabled = false;
                        MessageBox.Show("设定网络参数成功");
                    }
                    else if (rx.Header.Command == 0x04)                    //设定光源
                    {
                        tabControl1.Enabled = true;
                        timer1.Enabled = false;
                        if (rx.Data[0] != 0)
                        {
                            MessageBox.Show("设定光源电流成功");
                        }
                        else
                        {
                            MessageBox.Show("设定光源电流失败");
                        }
                    }
                    else if (rx.Header.Command == 0x05)                    //设定备用IO成功
                    {
                        tabControl1.Enabled = true;
                        timer1.Enabled = false;
                        MessageBox.Show("设定备用IO成功");
                    }
                    else if (rx.Header.Command == 0x06)                    //设定备用IO成功
                    {
                        tabControl1.Enabled = true;
                        timer1.Enabled = false;
                        MessageBox.Show("设定重启成功");
                    }
                    else if (rx.Header.Command == 0x07)                    //设定备用IO成功
                    {
                        tabControl1.Enabled = true;
                        timer1.Enabled = false;
                        MessageBox.Show("电源设定成功");
                    }
                    //                     if (bSave)
                    //                     {
                    //                         NetLog(DateTime.Now.ToLongTimeString() + "   :   ");
                    //                         NetLog("收到单片机采集板数据" + e.Client.clientEndPoint.ToString() + ":");
                    //                         NetLog(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " "));
                    //                         NetLog("\r\n");
                    //                     }
                    //                     else
                    if (rx.Header.Command != 0x01)
                    {
                        textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                        textBox1.AppendText("收到单片机采集板数据" + e.Client.clientEndPoint.ToString() + ":");
                        textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " "));
                        textBox1.AppendText("\r\n");
                    }
                }));
            }
            else
            {
                this.Invoke((EventHandler)(delegate
                {
                    NetLog(DateTime.Now.ToLongTimeString() + "   :   ");
                    NetLog("收到未知IP数据" + e.Client.clientEndPoint.ToString() + ":");
                    NetLog("\r\n");
                }));
            }
        }
コード例 #10
0
ファイル: Form2.cs プロジェクト: wangf0228GitHub/ZXJ
        private void button2_Click(object sender, EventArgs e)
        {
            if (Form1.mcuClientContext == null)
            {
                MessageBox.Show("单片机采集板尚未接入,请稍后重试!!");
                return;
            }
            IPAddress mcuIP = CheckIPInput(textBox2.Text);

            if (mcuIP == null)
            {
                MessageBox.Show("单片机板ip输入有误");
                return;
            }

            IPAddress mcuSN = CheckIPInput(textBox3.Text);

            if (mcuSN == null)
            {
                MessageBox.Show("单片机板子网掩码输入有误");
                return;
            }

            IPAddress mcuGW = CheckIPInput(textBox4.Text);

            if (mcuGW == null)
            {
                MessageBox.Show("单片机板默认网关输入有误");
                return;
            }

            IPAddress pcIP = CheckIPInput(textBox5.Text);

            if (pcIP == null)
            {
                MessageBox.Show("服务器端ip输入有误");
                return;
            }
            byte[] mac = StringsFunction.strToHexByte(textBox6.Text, " ");
            if (mac.Length != 8)
            {
                MessageBox.Show("MAC输入有误");
                return;
            }
            tabControl1.Enabled = false;
            timer1.Enabled      = true;
            byte[] tx = new byte[36];
            for (int i = 0; i < 8; i++)
            {
                tx[i] = mac[i];
            }
            for (int i = 0; i < 4; i++)
            {
                tx[8 + i]      = mcuIP.GetAddressBytes()[i];
                tx[8 + 4 + i]  = mcuSN.GetAddressBytes()[i];
                tx[8 + 8 + i]  = mcuGW.GetAddressBytes()[i];
                tx[8 + 16 + i] = pcIP.GetAddressBytes()[i];
            }
            tx[28] = BytesOP.GetLowByte((ushort)numericUpDown2.Value);
            tx[29] = BytesOP.GetHighByte((ushort)numericUpDown2.Value);
            tx[30] = 0;
            tx[31] = 0;
            tx[32] = BytesOP.GetLowByte((ushort)numericUpDown1.Value);
            tx[33] = BytesOP.GetHighByte((ushort)numericUpDown1.Value);
            tx[34] = 0;
            tx[35] = 0;
            byte[] tx03 = CP1616_NoAddr_Packet.MakeCP1616_NoAddr_Packet(0x03, tx);
            tcpAsyncServer.Send(Form1.mcuClientContext, tx03);
            this.Invoke((EventHandler)(delegate
            {
                textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                textBox1.AppendText("发送到" + Form1.mcuClientContext.clientEndPoint.ToString() + ":");
                textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(tx03, " "));
                textBox1.AppendText("\r\n");
            }));
        }
コード例 #11
0
        private void tcpAsyncServer_ReceiveServerEvent(object sender, ReceiveServerEventArgs e)
        {
            IPEndPoint ip = (IPEndPoint)e.Client.ClientSocket.RemoteEndPoint;

            //if (ip.Address.Equals(Form1.mcuSocketIP.Address))
            if (ip.Address.ToString() == strIP)
            {
                this.Invoke((EventHandler)(delegate
                {
                    CP1616_NoAddr_Packet rx = (CP1616_NoAddr_Packet)e.netPacket;
                    if (rx.Header.Command == 0x02)//读取网络参数
                    {
                        tabControl1.Enabled = true;
                        timer1.Enabled = false;
                        textBox6.Text = WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, 0, 6, " ");
                        byte[] bIP = new byte[4];
                        for (int i = 0; i < 4; i++)
                        {
                            bIP[i] = rx.Data[8 + i];
                        }
                        IPAddress mcuIP = new IPAddress(bIP);
                        textBox2.Text = mcuIP.ToString();

                        for (int i = 0; i < 4; i++)
                        {
                            bIP[i] = rx.Data[12 + i];
                        }
                        mcuIP = new IPAddress(bIP);
                        textBox3.Text = mcuIP.ToString();

                        for (int i = 0; i < 4; i++)
                        {
                            bIP[i] = rx.Data[16 + i];
                        }
                        mcuIP = new IPAddress(bIP);
                        textBox4.Text = mcuIP.ToString();

                        for (int i = 0; i < 4; i++)
                        {
                            bIP[i] = rx.Data[24 + i];
                        }
                        mcuIP = new IPAddress(bIP);
                        textBox5.Text = mcuIP.ToString();

                        numericUpDown2.Value = BytesOP.MakeShort(rx.Data[0x1d], rx.Data[0x1c]);

                        numericUpDown1.Value = BytesOP.MakeShort(rx.Data[0x21], rx.Data[0x20]);
                    }
                    else if (rx.Header.Command == 0x03)//设定网络参数
                    {
                        tabControl1.Enabled = true;
                        timer1.Enabled = false;
                        MessageBox.Show("设定网络参数成功,请重启设备后生效");
                    }
                    else if (rx.Header.Command == 0x05)//设定备用IO成功
                    {
                        tabControl1.Enabled = true;
                        timer1.Enabled = false;
                        MessageBox.Show("设定IO状态成功");
                    }
                    else if (rx.Header.Command == 0x06)//设定备用IO成功
                    {
                        tabControl1.Enabled = true;
                        timer1.Enabled = false;
                        MessageBox.Show("设定重启成功,将重新连入服务器,确定后将退出系统设定界面!");
                        this.Close();
                    }
                    else if (rx.Header.Command != 0x50)
                    {
                        textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                        textBox1.AppendText("控制板数据为:" + e.Client.clientEndPoint.ToString() + ":");
                        textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " "));
                        textBox1.AppendText("\r\n");
                    }
                }));
            }
            else
            {
                this.Invoke((EventHandler)(delegate
                {
                    NetLog(DateTime.Now.ToLongTimeString() + "   :   ");
                    NetLog("收到未知IP数据" + e.Client.clientEndPoint.ToString() + ":");
                    NetLog("\r\n");
                }));
            }
        }
コード例 #12
0
        private void tcpAsyncServer_ReceiveServerEvent(object sender, ReceiveServerEventArgs e)
        {
            IPEndPoint ip = (IPEndPoint)e.Client.ClientSocket.RemoteEndPoint;

            //if (ip.Address.Equals(mcuSocketIP.Address))

            if (mcuClientContext != null && e.Client == mcuClientContext)
            {
                this.Invoke((EventHandler)(delegate
                {
                    CP1616_NoAddr_Packet rx = (CP1616_NoAddr_Packet)e.netPacket;
                    if (rx.Header.Command == 0x01)
                    {
                        double f;
                        byte[] fb = new byte[4];
                        int fbIndex;
                        string str;
                        bool bSave = false;
                        DateTime dt = DateTime.Now;
                        //温度
                        f = BytesOP.MakeShort(rx.Data[0], rx.Data[1]);
                        f = f / 100;
                        listView1.Items[0].SubItems[1].Text = f.ToString("F2");
                        string filePT;
                        filePT = System.Windows.Forms.Application.StartupPath + "\\TextLog\\PT" + String.Format("{0:D4}{1:D2}{2:D2}", dt.Year, dt.Month, dt.Day) + ".txt";
                        TextLog.AddTextLog(dt.ToString() + "      " + listView1.Items[0].SubItems[1].Text, filePT, false);
                        //电子罗盘
                        fbIndex = 2;
                        for (int i = 0; i < 4; i++)
                        {
                            fb[i] = rx.Data[fbIndex + 3 - i];
                        }
                        f = BitConverter.ToSingle(fb, 0);
                        listView1.Items[1].SubItems[1].Text = f.ToString("F2");

                        fbIndex = 6;
                        for (int i = 0; i < 4; i++)
                        {
                            fb[i] = rx.Data[fbIndex + 3 - i];
                        }
                        f = BitConverter.ToSingle(fb, 0);
                        listView1.Items[2].SubItems[1].Text = f.ToString("F2");

                        fbIndex = 10;
                        for (int i = 0; i < 4; i++)
                        {
                            fb[i] = rx.Data[fbIndex + 3 - i];
                        }
                        f = BitConverter.ToSingle(fb, 0);
                        listView1.Items[3].SubItems[1].Text = f.ToString("F2");
                        string fileTC;
                        fileTC = System.Windows.Forms.Application.StartupPath + "\\TextLog\\TC" + String.Format("{0:D4}{1:D2}{2:D2}", dt.Year, dt.Month, dt.Day) + ".txt";
                        TextLog.AddTextLog(dt.ToString() + "      " + listView1.Items[1].SubItems[1].Text
                                           + "      " + listView1.Items[2].SubItems[1].Text
                                           + "      " + listView1.Items[3].SubItems[1].Text, fileTC, false);
                        //授时
                        str = Encoding.UTF8.GetString(rx.Data, 14, 10);
                        listView1.Items[4].SubItems[1].Text = str;

                        str = Encoding.UTF8.GetString(rx.Data, 24, 6);
                        listView1.Items[5].SubItems[1].Text = str;

                        //MS8607
                        f = BitConverter.ToSingle(rx.Data, 30);
                        listView1.Items[6].SubItems[1].Text = f.ToString("F2");

                        f = BitConverter.ToSingle(rx.Data, 34);
                        listView1.Items[7].SubItems[1].Text = f.ToString("F2");

                        f = BitConverter.ToSingle(rx.Data, 38);
                        listView1.Items[8].SubItems[1].Text = f.ToString("F2");
                        string fileMS;
                        fileMS = System.Windows.Forms.Application.StartupPath + "\\TextLog\\MS" + String.Format("{0:D4}{1:D2}{2:D2}", dt.Year, dt.Month, dt.Day) + ".txt";
                        TextLog.AddTextLog(dt.ToString() + "      " + listView1.Items[6].SubItems[1].Text
                                           + "      " + listView1.Items[7].SubItems[1].Text
                                           + "      " + listView1.Items[8].SubItems[1].Text, fileMS, false);
                        //光源
                        f = BytesOP.MakeShort(rx.Data[44], rx.Data[45]);
                        f = f / 10;
                        rioCur = f;
                        listView1.Items[14].SubItems[1].Text = f.ToString("F1");
                        ushort rioStatus = BytesOP.MakeShort(rx.Data[42], rx.Data[43]);
                        listView1.Items[15].SubItems[1].Text = rioStatus.ToString("X04");
                        if (BytesOP.GetBit(rioStatus, 0))
                        {
                            listView1.Items[16].SubItems[1].Text = "是";
                            listView1.Items[16].BackColor = Color.Lime;
                        }
                        else
                        {
                            listView1.Items[16].SubItems[1].Text = "否";
                            listView1.Items[16].BackColor = Color.Red;
                        }
                        if (BytesOP.GetBit(rioStatus, 6))
                        {
                            listView1.Items[17].SubItems[1].Text = "是";
                            listView1.Items[17].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[17].SubItems[1].Text = "否";
                            listView1.Items[17].BackColor = Color.Lime;
                        }
                        if (BytesOP.GetBit(rioStatus, 7))
                        {
                            listView1.Items[18].SubItems[1].Text = "是";
                            listView1.Items[18].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[18].SubItems[1].Text = "否";
                            listView1.Items[18].BackColor = Color.Lime;
                        }
                        if (BytesOP.GetBit(rioStatus, 11))//当前为新数据,需要存储
                        {
                            string fileLA;
                            fileLA = System.Windows.Forms.Application.StartupPath + "\\TextLog\\LA" + String.Format("{0:D4}{1:D2}{2:D2}", dt.Year, dt.Month, dt.Day) + ".txt";
                            TextLog.AddTextLog(dt.ToString() + "      " + listView1.Items[14].SubItems[1].Text
                                               + "      " + listView1.Items[15].SubItems[1].Text, fileLA, false);
                        }
                        //通信错误帧统计
                        listView1.Items[9].SubItems[1].Text = rx.Data[46].ToString();
                        if (rx.Data[46] > 5)
                        {
                            bSave = true;
                            listView1.Items[9].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[9].BackColor = SystemColors.Window;
                        }

                        listView1.Items[10].SubItems[1].Text = rx.Data[47].ToString();
                        if (rx.Data[47] > 5)
                        {
                            bSave = true;
                            listView1.Items[10].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[10].BackColor = SystemColors.Window;
                        }

                        listView1.Items[11].SubItems[1].Text = rx.Data[48].ToString();
                        if (rx.Data[48] > 5)
                        {
                            bSave = true;
                            listView1.Items[11].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[11].BackColor = SystemColors.Window;
                        }

                        listView1.Items[12].SubItems[1].Text = rx.Data[49].ToString();
                        if (rx.Data[49] > 5)
                        {
                            bSave = true;
                            listView1.Items[12].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[12].BackColor = SystemColors.Window;
                        }

                        listView1.Items[13].SubItems[1].Text = rx.Data[50].ToString();
                        if (rx.Data[50] > 5)
                        {
                            bSave = true;
                            listView1.Items[13].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[13].BackColor = SystemColors.Window;
                        }

                        listView1.Items[19].SubItems[1].Text = rx.Data[51].ToString(); //漏水1
                        listView1.Items[20].SubItems[1].Text = rx.Data[52].ToString(); //漏水2
                        listView1.Items[21].SubItems[1].Text = rx.Data[53].ToString(); //备用IO1
                        listView1.Items[22].SubItems[1].Text = rx.Data[54].ToString(); //备用IO1
                        listView1.Items[23].SubItems[1].Text = rx.Data[55].ToString(); //备用IO1
                        listView1.Items[24].SubItems[1].Text = rx.Data[56].ToString(); //备用IO1
                        listView1.Items[25].SubItems[1].Text = rx.Data[57].ToString(); //备用IO1
                        listView1.Items[26].SubItems[1].Text = rx.Data[58].ToString(); //备用IO1

                        for (int i = 0; i < 6; i++)
                        {
                            bIO[i] = rx.Data[53 + i];
                        }

                        if (rx.Data[59] == 0)
                        {
                            listView1.Items[27].SubItems[1].Text = "打开";
                            bPower1 = true;
                        }
                        else
                        {
                            listView1.Items[27].SubItems[1].Text = "关闭";
                            bPower1 = false;
                        }

                        if (rx.Data[60] == 0)
                        {
                            listView1.Items[28].SubItems[1].Text = "打开";
                            bPower2 = true;
                        }
                        else
                        {
                            listView1.Items[28].SubItems[1].Text = "关闭";
                            bPower2 = false;
                        }

                        if (bSave)
                        {
                            NetLog(DateTime.Now.ToLongTimeString() + "   :   ");
                            NetLog("收到单片机采集板数据" + e.Client.clientEndPoint.ToString() + ":");
                            NetLog(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " "));
                            NetLog("\r\n");
                        }
                        else
                        {
                            textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                            textBox1.AppendText("收到单片机采集板数据" + e.Client.clientEndPoint.ToString() + ":");
                            textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " "));
                            textBox1.AppendText("\r\n");
                        }
                    }
                    else if (rx.Header.Command == 0x08)//读取光源状态
                    {
                        timer2.Enabled = false;
                        toolStrip1.Enabled = true;
                        timer1.Enabled = true;
                        textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                        textBox1.AppendText("收到手动读取光源数据" + e.Client.clientEndPoint.ToString() + ":");
                        textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " "));
                        textBox1.AppendText("\r\n");
                        if (rx.Header.Len == 0)//读取失败
                        {
                            MessageBox.Show("读取光源状态失败,请稍后重试!!");
                        }
                        else
                        {
                            //光源
                            double f;
                            f = BytesOP.MakeShort(rx.Data[2], rx.Data[3]);
                            f = f / 10;
                            rioCur = f;
                            listView1.Items[14].SubItems[1].Text = f.ToString("F1");
                            ushort rioStatus = BytesOP.MakeShort(rx.Data[0], rx.Data[1]);
                            listView1.Items[15].SubItems[1].Text = rioStatus.ToString("X04");
                            if (BytesOP.GetBit(rioStatus, 0))
                            {
                                listView1.Items[16].SubItems[1].Text = "是";
                                listView1.Items[16].BackColor = Color.Lime;
                            }
                            else
                            {
                                listView1.Items[16].SubItems[1].Text = "否";
                                listView1.Items[16].BackColor = Color.Red;
                            }
                            if (BytesOP.GetBit(rioStatus, 6))
                            {
                                listView1.Items[17].SubItems[1].Text = "是";
                                listView1.Items[17].BackColor = Color.Red;
                            }
                            else
                            {
                                listView1.Items[17].SubItems[1].Text = "否";
                                listView1.Items[17].BackColor = Color.Lime;
                            }
                            if (BytesOP.GetBit(rioStatus, 7))
                            {
                                listView1.Items[18].SubItems[1].Text = "是";
                                listView1.Items[18].BackColor = Color.Red;
                            }
                            else
                            {
                                listView1.Items[18].SubItems[1].Text = "否";
                                listView1.Items[18].BackColor = Color.Lime;
                            }
                            DateTime dt = DateTime.Now;
                            string fileLA;
                            fileLA = System.Windows.Forms.Application.StartupPath + "\\TextLog\\LA" + String.Format("{0:D4}{1:D2}{2:D2}", dt.Year, dt.Month, dt.Day) + ".txt";
                            TextLog.AddTextLog(dt.ToString() + "      " + listView1.Items[14].SubItems[1].Text
                                               + "      " + listView1.Items[15].SubItems[1].Text, fileLA, false);
                        }
                    }
                    else
                    {
                        textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                        textBox1.AppendText("收到单片机采集板数据" + e.Client.clientEndPoint.ToString() + ":");
                        textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " "));
                        textBox1.AppendText("\r\n");
                    }
                }));
            }
            else
            {
                this.Invoke((EventHandler)(delegate
                {
                    textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                    textBox1.AppendText("收到未知IP数据" + e.Client.clientEndPoint.ToString() + ":");
                    textBox1.AppendText("\r\n");
                    CP1616_NoAddr_Packet rx = (CP1616_NoAddr_Packet)e.netPacket;
                    if (rx.Header.Command == 0xf0)
                    {
                        if (rx.Data[0] == 0x01)                        //传感器采集板接入
                        {
                            toolStripButton2.Enabled = true;
                            toolStripButton3.Enabled = true;
                            mcuClientContext = e.Client;
                            //mcuSocketIP.Port = ip.Port;
                            this.Invoke((EventHandler)(delegate
                            {
                                NetLog(DateTime.Now.ToLongTimeString() + "   :   ");
                                NetLog("单片机采集板接入");
                                NetLog("\r\n");
                            }));
                        }
                    }
                }));
            }
        }
コード例 #13
0
ファイル: Form1.cs プロジェクト: wangf0228GitHub/ZXJ
        private void tcpAsyncServer_ReceiveServerEvent(object sender, ReceiveServerEventArgs e)
        {
            IPEndPoint ip = (IPEndPoint)e.Client.ClientSocket.RemoteEndPoint;

            //if (ip.Address.Equals(mcuSocketIP.Address))

            if (mcuClientContext != null && e.Client == mcuClientContext)
            {
                this.Invoke((EventHandler)(delegate
                {
                    CP1616_NoAddr_Packet rx = (CP1616_NoAddr_Packet)e.netPacket;
                    if (rx.Header.Command == 0x01)
                    {
                        textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                        textBox1.AppendText("收到水听器采集板数据" + e.Client.clientEndPoint.ToString());
                        textBox1.AppendText("\r\n");
                        int max = 102400;
                        if (chart1.Series[0].Points.Count > max)
                        {
                            chart1.Series[0].Points.Clear();
                        }
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < (rx.Data.Length - 2); i += 2)
                        {
                            ushort ad = BytesOP.MakeShort(rx.Data[i], rx.Data[i + 1]);
                            double v = (double)ad;
                            v = v * 0.000078125;
                            v = v - 2.56;
                            //chart1.Series[0].Points.Clear();
                            chart1.Series[0].Points.AddY(v);
                            sb.AppendLine(v.ToString("F6"));
//                           int max = 1024000;
//                           if (chart1.Series[0].Points.Count > max)
//                           {
//                               while (chart1.Series[0].Points.Count > (max - 1024))
//                               {
//                                   chart1.Series[0].Points.RemoveAt(0);
//                               }
//                           }
                        }
                        TextLog.AddTextLog(sb.ToString(), fileHY, false);
                    }
                }));
            }
            else
            {
                this.Invoke((EventHandler)(delegate
                {
                    textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                    textBox1.AppendText("收到未知IP数据" + e.Client.clientEndPoint.ToString() + ":");
                    textBox1.AppendText("\r\n");
                    CP1616_NoAddr_Packet rx = (CP1616_NoAddr_Packet)e.netPacket;
                    if (rx.Header.Command == 0xf0)
                    {
                        if (rx.Data[0] == 0x02)                         //矢量水听器采集板接入
                        {
                            mcuClientContext = e.Client;
                            //mcuSocketIP.Port = ip.Port;
                            this.Invoke((EventHandler)(delegate
                            {
                                NetLog(DateTime.Now.ToLongTimeString() + "   :   ");
                                NetLog("水听器采集板接入");
                                NetLog("\r\n");
                            }));
                        }
                    }
                }));
            }
        }
コード例 #14
0
        private void tcpAsyncServer_ReceiveServerEvent(object sender, ReceiveServerEventArgs e)
        {
            IPEndPoint ip = (IPEndPoint)e.Client.ClientSocket.RemoteEndPoint;

            //if (ip.Address.Equals(mcuSocketIP.Address))

            if (mcuClientContext != null && e.Client == mcuClientContext)
            {
                this.Invoke((EventHandler)(delegate
                {
                    CP1616_NoAddr_Packet rx = (CP1616_NoAddr_Packet)e.netPacket;
                    if (rx.Header.Command == 0x01)
                    {
                        double f;
                        byte[] fb = new byte[4];
                        int fbIndex;
                        string str;
                        bool bSave = false;
                        //温度
                        f = BytesOP.MakeShort(rx.Data[0], rx.Data[1]);
                        f = f / 100;
                        listView1.Items[0].SubItems[1].Text = f.ToString("F2");
                        //电子罗盘
                        fbIndex = 2;
                        for (int i = 0; i < 4; i++)
                        {
                            fb[i] = rx.Data[fbIndex + 3 - i];
                        }
                        f = BitConverter.ToSingle(fb, 0);
                        listView1.Items[1].SubItems[1].Text = f.ToString("F2");

                        fbIndex = 6;
                        for (int i = 0; i < 4; i++)
                        {
                            fb[i] = rx.Data[fbIndex + 3 - i];
                        }
                        f = BitConverter.ToSingle(fb, 0);
                        listView1.Items[2].SubItems[1].Text = f.ToString("F2");

                        fbIndex = 10;
                        for (int i = 0; i < 4; i++)
                        {
                            fb[i] = rx.Data[fbIndex + 3 - i];
                        }
                        f = BitConverter.ToSingle(fb, 0);
                        listView1.Items[3].SubItems[1].Text = f.ToString("F2");

                        //授时
                        str = Encoding.UTF8.GetString(rx.Data, 14, 10);
                        listView1.Items[4].SubItems[1].Text = str;

                        str = Encoding.UTF8.GetString(rx.Data, 24, 6);
                        listView1.Items[5].SubItems[1].Text = str;

                        //MS8607
                        f = BitConverter.ToSingle(rx.Data, 30);
                        listView1.Items[6].SubItems[1].Text = f.ToString("F2");

                        f = BitConverter.ToSingle(rx.Data, 34);
                        listView1.Items[7].SubItems[1].Text = f.ToString("F2");

                        f = BitConverter.ToSingle(rx.Data, 38);
                        listView1.Items[8].SubItems[1].Text = f.ToString("F2");
                        //光源
                        f = BytesOP.MakeShort(rx.Data[44], rx.Data[45]);
                        f = f / 10;
                        rioCur = f;
                        listView1.Items[14].SubItems[1].Text = f.ToString("F1");
                        ushort rioStatus = BytesOP.MakeShort(rx.Data[42], rx.Data[43]);
                        listView1.Items[15].SubItems[1].Text = rioStatus.ToString("X04");
                        if (BytesOP.GetBit(rioStatus, 0))
                        {
                            listView1.Items[16].SubItems[1].Text = "是";
                            listView1.Items[16].BackColor = Color.Lime;
                        }
                        else
                        {
                            listView1.Items[16].SubItems[1].Text = "否";
                            listView1.Items[16].BackColor = Color.Red;
                        }
                        if (BytesOP.GetBit(rioStatus, 6))
                        {
                            listView1.Items[17].SubItems[1].Text = "是";
                            listView1.Items[17].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[17].SubItems[1].Text = "否";
                            listView1.Items[17].BackColor = Color.Lime;
                        }
                        if (BytesOP.GetBit(rioStatus, 7))
                        {
                            listView1.Items[18].SubItems[1].Text = "是";
                            listView1.Items[18].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[18].SubItems[1].Text = "否";
                            listView1.Items[18].BackColor = Color.Lime;
                        }
                        //通信错误帧统计
                        listView1.Items[9].SubItems[1].Text = rx.Data[46].ToString();
                        if (rx.Data[46] > 5)
                        {
                            bSave = true;
                            listView1.Items[9].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[9].BackColor = SystemColors.Window;
                        }

                        listView1.Items[10].SubItems[1].Text = rx.Data[47].ToString();
                        if (rx.Data[47] > 5)
                        {
                            bSave = true;
                            listView1.Items[10].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[10].BackColor = SystemColors.Window;
                        }

                        listView1.Items[11].SubItems[1].Text = rx.Data[48].ToString();
                        if (rx.Data[48] > 5)
                        {
                            bSave = true;
                            listView1.Items[11].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[11].BackColor = SystemColors.Window;
                        }

                        listView1.Items[12].SubItems[1].Text = rx.Data[49].ToString();
                        if (rx.Data[49] > 5)
                        {
                            bSave = true;
                            listView1.Items[12].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[12].BackColor = SystemColors.Window;
                        }

                        listView1.Items[13].SubItems[1].Text = rx.Data[50].ToString();
                        if (rx.Data[50] > 5)
                        {
                            bSave = true;
                            listView1.Items[13].BackColor = Color.Red;
                        }
                        else
                        {
                            listView1.Items[13].BackColor = SystemColors.Window;
                        }

                        listView1.Items[19].SubItems[1].Text = rx.Data[51].ToString(); //漏水1
                        listView1.Items[20].SubItems[1].Text = rx.Data[52].ToString(); //漏水2
                        listView1.Items[21].SubItems[1].Text = rx.Data[53].ToString(); //备用IO1
                        listView1.Items[22].SubItems[1].Text = rx.Data[54].ToString(); //备用IO1
                        listView1.Items[23].SubItems[1].Text = rx.Data[55].ToString(); //备用IO1
                        listView1.Items[24].SubItems[1].Text = rx.Data[56].ToString(); //备用IO1
                        listView1.Items[25].SubItems[1].Text = rx.Data[57].ToString(); //备用IO1
                        listView1.Items[26].SubItems[1].Text = rx.Data[58].ToString(); //备用IO1

                        for (int i = 0; i < 6; i++)
                        {
                            bIO[i] = rx.Data[53 + i];
                        }

                        if (rx.Data[59] == 0)
                        {
                            listView1.Items[27].SubItems[1].Text = "打开";
                            bPower1 = true;
                        }
                        else
                        {
                            listView1.Items[27].SubItems[1].Text = "关闭";
                            bPower1 = false;
                        }

                        if (rx.Data[60] == 0)
                        {
                            listView1.Items[28].SubItems[1].Text = "打开";
                            bPower2 = true;
                        }
                        else
                        {
                            listView1.Items[28].SubItems[1].Text = "关闭";
                            bPower2 = false;
                        }

                        if (bSave)
                        {
                            NetLog(DateTime.Now.ToLongTimeString() + "   :   ");
                            NetLog("收到单片机采集板数据" + e.Client.clientEndPoint.ToString() + ":");
                            NetLog(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " "));
                            NetLog("\r\n");
                        }
                        else
                        {
                            textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                            textBox1.AppendText("收到单片机采集板数据" + e.Client.clientEndPoint.ToString() + ":");
                            textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " "));
                            textBox1.AppendText("\r\n");
                        }
                    }
                    else
                    {
                        textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                        textBox1.AppendText("收到单片机采集板数据" + e.Client.clientEndPoint.ToString() + ":");
                        textBox1.AppendText(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rx.Data, " "));
                        textBox1.AppendText("\r\n");
                    }
                }));
            }
            else
            {
                this.Invoke((EventHandler)(delegate
                {
                    textBox1.AppendText(DateTime.Now.ToLongTimeString() + "   :   ");
                    textBox1.AppendText("收到未知IP数据" + e.Client.clientEndPoint.ToString() + ":");
                    textBox1.AppendText("\r\n");
                    CP1616_NoAddr_Packet rx = (CP1616_NoAddr_Packet)e.netPacket;
                    if (rx.Header.Command == 0xf0)
                    {
                        if (rx.Data[0] == 0x01)                        //传感器采集板接入
                        {
                            mcuClientContext = e.Client;
                            //mcuSocketIP.Port = ip.Port;
                            this.Invoke((EventHandler)(delegate
                            {
                                NetLog(DateTime.Now.ToLongTimeString() + "   :   ");
                                NetLog("单片机采集板接入");
                                NetLog("\r\n");
                            }));
                        }
                    }
                }));
            }
        }