Example #1
0
        private bool SendPosition(Socket tcp)
        {
            HeadPack head = new HeadPack()
            {
                SeqNO = GetNextSeqNum(), MessageId = (ushort)MessageIds.PositionReport, BodyProp = (ushort)0
            };

            head.SetDeviceId(this.deviceId);

            double lat;
            double lon;
            int    speed = 10 + r.Next(90);

            latlonBuilder.GetNextLatlon(speed, out lat, out lon);

            PositionReportPack pack = new PositionReportPack()
            {
                AlermFlags = 0,
                Speed      = (ushort)(speed * 10),
                State      = 0,
                Latitude   = Convert.ToUInt32(lat * 1000000),
                Longitude  = Convert.ToUInt32(lon * 1000000),
                Altitude   = 200,
                Direction  = 0,
                Time       = DateTime.Now.ToString("yyMMddHHmmss")
            };

            byte[] bytesSend = RawFormatter.Instance.Struct2Bytes(pack);

            BodyPropertyHelper.SetMessageLength(ref head.BodyProp, (ushort)bytesSend.Length);

            byte[] headBytes = RawFormatter.Instance.Struct2Bytes(head);
            byte[] fullBytes = headBytes.Concat(bytesSend).ToArray();
            byte   checkByte = PackHelper.CalcCheckByte(fullBytes, 0, fullBytes.Length);

            bytesSend = (new byte[] { 0x7e }
                         .Concat(PackHelper.EncodeBytes(fullBytes.Concat(new byte[] { checkByte })))
                         .Concat(new byte[] { 0x7e })).ToArray();

            //Console.WriteLine("{0} {1}",head.SeqNO, bytesSend.ToHexString());

            //发送消息
            SendBytes(tcp, bytesSend);
            //控制台打印日志cpu占用太高
            if (sendLogPrint)
            {
                Console.WriteLine("{0} {1}, LatLon:{2:0.000000},{3:0.000000}", head.GetDeviceId(), DateTime.Now.ToString(), lat, lon);
            }
            //等待接收服务端返回值
            var success = true;

            if (waitReceive)
            {
                success = RecvBytes(tcp);
                //var success = true;
            }
            return(success);
        }