示例#1
0
文件: Message.cs 项目: Saber-7/CBTC
        //发包
        public byte[] Pack(VobcToZCMessage VobcToZCMes, int size)
        {
            byte[] tempBytes = new byte[size];
            IntPtr structPtr = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(VobcToZCMes, structPtr, false);
            Marshal.Copy(structPtr, tempBytes, 0, size);
            Marshal.FreeHGlobal(structPtr);
            return(tempBytes);
        }
示例#2
0
        //DateTime RecT

        private void communicationToZC()
        {
            while (communicationToZCFlag)
            {
                while (confirmCommunicationFlag)
                {
                    if (recTime.AddSeconds(1.6) < DateTime.Now && firstFlag == false)
                    {
                        linkstatus.LinkStateShow(false);
                        breakCommunicationFlag = true;
                    }
                    else
                    {
                        breakCommunicationFlag = false;
                    }
                    if (breakCommunicationFlag == false && stopStatistics == false)
                    {
                        Message mes       = new Message();
                        bool    isNotlose = true;
                        if (loseNum != 0)
                        {
                            isNotlose = false;
                        }
                        TimeSpan ts;
                        ts = (DateTime.Now - recTime);
                        VobcToZCMessage VobcToZCMes = new VobcToZCMessage
                        {
                            cycle         = packageNumber++,
                            length        = 400,
                            trainID       = Convert.ToUInt64(trainNumber),
                            location      = (ulong)Math.Round(Si + k * 1500 + 24000 * m, 0),
                            isNotLosedata = isNotlose,
                            processTime   = DateTime.Now - recTime,
                            ZCT1          = socket.ZCT1
                        };
                        byte[] tempBytes = mes.Pack(VobcToZCMes, 400);
                        socket.Send(tempBytes, desIP, Convert.ToInt32(desPort));

                        using (StreamWriter sf = File.AppendText(Application.StartupPath + "\\SendData.txt"))
                        {
                            try
                            {
                                sf.WriteLine(DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss.fff") + " " + VobcToZCMes.cycle);
                            }
                            catch (Exception e)
                            { }
                        };
                    }
                    hip.Start();
                    //是否在200毫秒内返回成功
                    Thread.Sleep(180);
                    double timeDifference = 0.2 - hip.Duration;
                    if (timeDifference > 0)
                    {
                        hip.Interval(timeDifference * 1000);
                    }
                    else
                    {
                    }
                    if (breakCommunicationFlag == true)
                    {
                        break;
                    }
                }
                //中断通信后进入循环等待,等待着车的再次开启
                while (breakCommunicationFlag)
                {
                    if (breakCommunicationFlag == false)
                    {
                        break;
                    }
                }
            }
        }