Beispiel #1
0
        public byte[] McuSendRead(ushort addr, ushort len)
        {
            byte[] send_pack = ModbusUdp.MBReqConnect();
            McuSend(send_pack, send_pack.Length);

            //Timeout = 300;
            //while (timeout--)
            //{ //等待应答
            //    if(应答成功)
            //    {
            //        return get_data;
            //    }
            //    sleep(1);
            //}

            return(null);
        }
Beispiel #2
0
            private void ReceiveHandle()
            {
                //接收数据处理线程
                string msg;

                byte[] data = new byte[1024];
                byte[] RecData;

                while (true)
                {
                    if (mySocket == null || mySocket.Available < 1)
                    {
                        Thread.Sleep(10);
                        continue;
                    }
                    //跨线程调用控件
                    //接收UDP数据报,引用参数RemotePoint获得源地址
                    int rlen = mySocket.ReceiveFrom(data, ref RemotePoint);
                    if (UdpReceiveFun != null)
                    {
                        UdpReceiveFun(data, rlen);
                    }

                    if ((data[0] == 0xff) && (data[1] == 0x00))
                    {
                        UdpConnectCompleteFlag.Value = true;
                    }

                    if (UdpConnectCompleteFlag.Value)
                    {
                        if ((data[0] == 0xff) && (data[1] == 0x01))
                        {
                            MonitorTickAck.Value = data[2];
                            msg = data[2].ToString();
                            Debug.WriteLine(RemotePoint.ToString() + " : " + msg);
                        }
                        RecData = new byte[rlen];
                        Array.Copy(data, 0, RecData, 0, rlen);
                        RecData = ModbusUdp.MBRsp(RecData);
                        if (RecData != null)
                        {
                            Debug.WriteLine(ModbusUdp.ByteToHexStr(RecData));
                        }
                    }
                }
            }
Beispiel #3
0
 //MCU数据发送函数
 public void McuSendConnect()
 {
     byte[] send_pack = ModbusUdp.MBReqConnect();
     McuSend(send_pack, send_pack.Length);
 }
Beispiel #4
0
            private void UdpServerTask()
            {
                byte MonitorTick = 0;

                IPAddress ip;
                int       port;

                //得到本机IP,设置UDP端口号
                IPAddress.TryParse(GetLocalIP(), out ip);
                port         = 0; //自动分配端口
                ipLocalPoint = new IPEndPoint(ip, port);

                //定义网络类型,数据连接类型和网络协议UDP
                mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                //绑定网络地址
                try
                {
                    mySocket.Bind(ipLocalPoint);
                }
                catch
                {
                    MessageBox.Show("网络端口被占用");
                    //System.Environment.Exit(0);
                }

                //设置广播地址
                ip   = IPAddress.Broadcast;
                port = 1030;
                IPEndPoint ipep = new IPEndPoint(ip, port);
                EndPoint   BroadcastRemotePoint = (EndPoint)(ipep);

                //设置客户机IP,默认为广播地址
                RemotePoint = (EndPoint)(ipep);

                //允许广播
                mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);

                //启动一个新的线程,执行方法this.ReceiveHandle,
                //以便在一个独立的进程中执行数据接收的操作

                Thread thread = new Thread(new ThreadStart(this.ReceiveHandle));

                thread.IsBackground = true; //设置为后台线程
                thread.Start();

                //发送UDP数据包
                byte[] data;

                while (true)
                {
                    if (!UdpConnectCompleteFlag.Value)
                    {
                        data = ModbusUdp.MBReqConnect();
                        mySocket.SendTo(data, data.Length, SocketFlags.None, BroadcastRemotePoint);
                        Debug.WriteLine("Search udp server...");
                    }
                    else
                    {
                        if (MonitorTickAck.Value != MonitorTick)
                        {
                            if (MonitorTickTimeout.Value > 0)
                            {
                                MonitorTickTimeout.Value--;
                            }
                        }
                        else
                        {
                            MonitorTickTimeout.Value = MonitorTickTimeoutValue;
                        }

                        if (MonitorTickTimeout.Value == 0)
                        {
                            UdpConnectCompleteFlag.Value = false;
                            MonitorTick              = 0;
                            MonitorTickAck.Value     = 0;
                            MonitorTickTimeout.Value = MonitorTickTimeoutValue;
                            Debug.WriteLine("Connect lose...");
                            continue;
                        }
                        else
                        {
                            //发送UDP心跳包
                            if (MonitorTick < 0xff)
                            {
                                MonitorTick++;
                            }
                            else
                            {
                                MonitorTick = 0;
                            }
                            data = ModbusUdp.MBReqMonitor(MonitorTick);
                            mySocket.SendTo(data, data.Length, SocketFlags.None, RemotePoint);
                            Debug.WriteLine("Connect monitor...");
                        }
                    }
                    Thread.Sleep(1000);
                }
            }