コード例 #1
0
 public static byte[] GetTxData(ushort BaudRate)
 {
     byte[] ret = new byte[2];
     ret[0] = BytesOP.GetLowByte(BaudRate);
     ret[1] = BytesOP.GetHighByte(BaudRate);
     return(ret);
 }
コード例 #2
0
 public static byte[] MakeCP1616_NoAddr_Packet(byte com, byte[] data)
 {
     byte[] txbuffer;
     if (data != null)
     {
         txbuffer    = new byte[CP1616_NoAddr_PacketHead.HEAD_SIZE + data.Length + 2];
         txbuffer[0] = 0x16;
         txbuffer[1] = 0x16;
         txbuffer[2] = com;
         txbuffer[3] = BytesOP.GetHighByte((UInt16)data.Length);
         txbuffer[4] = BytesOP.GetLowByte((UInt16)data.Length);
         for (int i = 0; i < data.Length; i++)
         {
             txbuffer[i + CP1616_NoAddr_PacketHead.HEAD_SIZE] = data[i];
         }
         txbuffer[CP1616_NoAddr_PacketHead.HEAD_SIZE + data.Length]     = Verify.GetVerify_byteSum(txbuffer, CP1616_NoAddr_PacketHead.HEAD_SIZE + data.Length);
         txbuffer[CP1616_NoAddr_PacketHead.HEAD_SIZE + data.Length + 1] = 0x0d;
     }
     else
     {
         txbuffer    = new byte[CP1616_NoAddr_PacketHead.HEAD_SIZE + 2];
         txbuffer[0] = 0x16;
         txbuffer[1] = 0x16;
         txbuffer[2] = com;
         txbuffer[3] = 0x00;
         txbuffer[4] = 0x00;
         txbuffer[CP1616_NoAddr_PacketHead.HEAD_SIZE]     = Verify.GetVerify_byteSum(txbuffer, CP1616_NoAddr_PacketHead.HEAD_SIZE);
         txbuffer[CP1616_NoAddr_PacketHead.HEAD_SIZE + 1] = 0x0d;
     }
     return(txbuffer);
 }
コード例 #3
0
        public static List <byte> PPPDataEncode(List <byte> RxList, bool bLCP)
        {
            ushort crc = Verify.GetVerify_CRC16_CCITT(RxList.ToArray(), RxList.Count);

            RxList.Add(BytesOP.GetLowByte(crc));
            RxList.Add(BytesOP.GetHighByte(crc));
            List <byte> temp = new List <byte>();

            temp.Add(0x7e);
            byte x;

            foreach (byte b in RxList)
            {
                x = IsNeedTrans(b, bLCP);
                if (x != 0x00)
                {
                    temp.Add(0x7d);
                    temp.Add(x);
                }
                else
                {
                    temp.Add(b);
                }
            }
            temp.Add(0x7e);
            return(temp);
        }
コード例 #4
0
 public static DeviceState UDPCommand_03(byte xy, byte dir, int nCount)//位移平台控制
 {
     byte[] tx = new byte[34];
     tx[0] = 0x03;
     tx[1] = xy;
     tx[2] = dir;
     tx[3] = BytesOP.GetLowByte((ushort)nCount);
     tx[4] = BytesOP.GetHighByte((ushort)nCount);
     return(udpProc(tx, 5000));
 }
コード例 #5
0
        public static CP1616Packet CP1616ComProc(ref SerialPort serialPort, byte com, ushort addr, uint b)
        {
            ushort b1, b2;

            b1 = BytesOP.GetHighShort(b);
            b2 = BytesOP.GetLowShort(b);
            return(CP1616ComProc(ref serialPort, com, addr, new byte[4] {
                BytesOP.GetHighByte(b1), BytesOP.GetLowByte(b1), BytesOP.GetHighByte(b2), BytesOP.GetLowByte(b2)
            }, 5));
        }
コード例 #6
0
        public static void CP1616ComSend(ref SerialPort serialPort, byte com, ushort addr, uint b)
        {
            ushort b1, b2;

            b1 = BytesOP.GetHighShort(b);
            b2 = BytesOP.GetLowShort(b);
            CP1616ComSend(ref serialPort, com, addr, new byte[4] {
                BytesOP.GetHighByte(b1), BytesOP.GetLowByte(b1), BytesOP.GetHighByte(b2), BytesOP.GetLowByte(b2)
            });
        }
コード例 #7
0
        public List <byte> ToArray()
        {
            byte x;

            TotalLength = SetTotalLength();
            List <byte> ret = new List <byte>();

            ret.Add(BytesOP.MakeByte(Version, HeaderLength));
            ret.Add(TypeOfService);
            ret.Add(BytesOP.GetHighByte(TotalLength));
            ret.Add(BytesOP.GetLowByte(TotalLength));
            ret.Add(BytesOP.GetHighByte(Identification));
            ret.Add(BytesOP.GetLowByte(Identification));
            x  = (byte)(Flags << 5);
            x += (byte)(FragmentOffset >> 8);
            ret.Add(x);
            ret.Add(BytesOP.GetLowByte(FragmentOffset));
            ret.Add(TimeToLive);
            ret.Add(Protocol);
            ret.Add(0x00);//校验和
            ret.Add(0x00);
            ret.Add(SourceAddress[0]);
            ret.Add(SourceAddress[1]);
            ret.Add(SourceAddress[2]);
            ret.Add(SourceAddress[3]);
            ret.Add(DastinationAddress[0]);
            ret.Add(DastinationAddress[1]);
            ret.Add(DastinationAddress[2]);
            ret.Add(DastinationAddress[3]);
            if (HeaderLength > 0x05)
            {
                for (int i = 0; i < ((HeaderLength - 0x05) * 4); i++)
                {
                    ret.Add(Options[i]);
                }
            }
            HeaderVerify = Verify.GetVerify_IP(ret.ToArray());
            ret[10]      = BytesOP.GetHighByte(HeaderVerify);
            ret[11]      = BytesOP.GetLowByte(HeaderVerify);
            switch (Protocol)
            {
            case 0x06:    //TCP
                FakeTcpHeader fh = new FakeTcpHeader(SourceAddress, DastinationAddress);
                fh.count = (ushort)(TotalLength - HeaderLength * 4);
                TCPPacket   tcp     = (TCPPacket)oProtocolContent;
                List <Byte> TCPList = tcp.ToArray(fh);
                foreach (byte b in TCPList)
                {
                    ret.Add(b);
                }
                break;
            }
            Identification++;
            return(ret);
        }
コード例 #8
0
 public byte RNum;//总线电阻序号
 public byte[] GetTxData()
 {
     byte[] ret = new byte[7];
     ret[0] = BytesOP.GetLowByte(Prescaler);
     ret[1] = BytesOP.GetHighByte(Prescaler);
     ret[2] = BS1;
     ret[3] = BS2;
     ret[4] = SJW;
     ret[5] = Param;
     ret[6] = RNum;
     return(ret);
 }
コード例 #9
0
 public static DeviceState UDPCommand_02(int Tex, int nCount)//图像采集控制帧
 {
     byte[] tx = new byte[34];
     tx[0] = 0x02;
     tx[1] = BytesOP.GetLowByte(BytesOP.GetLowShort((uint)Tex));
     tx[2] = BytesOP.GetHighByte(BytesOP.GetLowShort((uint)Tex));
     tx[3] = BytesOP.GetLowByte(BytesOP.GetHighShort((uint)Tex));
     tx[4] = BytesOP.GetHighByte(BytesOP.GetHighShort((uint)Tex));
     tx[5] = BytesOP.GetLowByte((ushort)nCount);
     tx[6] = BytesOP.GetHighByte((ushort)nCount);
     return(udpProc(tx, 1000));
 }
コード例 #10
0
 public byte[] Data;//[UART_WithPC_BUFMAX-6];
 public byte[] GetTxData()
 {
     byte[] ret = new byte[Data.Length + ParamLen];
     ret[0] = BytesOP.GetLowByte(Retry);
     ret[1] = BytesOP.GetHighByte(Retry);
     ret[2] = BytesOP.GetLowByte(RxTimeOut);
     ret[3] = BytesOP.GetHighByte(RxTimeOut);
     for (int i = 0; i < Data.Length; i++)
     {
         ret[ParamLen + i] = Data[i];
     }
     return(ret);
 }
コード例 #11
0
 public static DeviceState UDPCommand_01()//图像采集板配置帧
 {
     byte[] tx = new byte[34];
     tx[0] = 0x01;
     tx[1] = BytesOP.GetLowByte((ushort)SystemParam.CCD_M);
     tx[2] = BytesOP.GetHighByte((ushort)SystemParam.CCD_M);
     tx[3] = BytesOP.GetLowByte((ushort)SystemParam.CCD_N);
     tx[4] = BytesOP.GetHighByte((ushort)SystemParam.CCD_N);
     tx[5] = (byte)SystemParam.CCD_phi;
     tx[6] = 0x67;
     tx[7] = 0x00;
     tx[8] = (byte)(SystemParam.CCD_PGA + 0x0b);
     return(udpProc(tx, 1000));
 }
コード例 #12
0
        public static byte[] ReTransPicDatas(ushort[,] ya, int row, int col)
        {
            int k;

            byte[] ret = new byte[row * col * 2];
            for (int m = 0; m < row; m++)
            {
                for (int n = 0; n < col; n++)
                {
                    k = n * 2;
                    ret[k + 2 * col * m + 1] = BytesOP.GetHighByte(ya[m, n]);
                    ret[k + 2 * col * m]     = BytesOP.GetLowByte(ya[m, n]);
                }
            }
            return(ret);
        }
コード例 #13
0
ファイル: Form1.cs プロジェクト: dongdong-2009/LiChunYu
        static byte ReadFshData(byte c)
        {
            int addr;

            addr = FlashAddr + c - 0x1e14;
            int i = addr / 2;
            int j = addr % 2;

            if (j == 0)
            {
                return(BytesOP.GetLowByte(TCodeKey[i]));
            }
            else
            {
                return(BytesOP.GetHighByte(TCodeKey[i]));
            }
        }
コード例 #14
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");
     }));
 }
コード例 #15
0
        public static byte[] MakePacket(UInt16 mbapIndex, byte subAddr, FunctionCode fc, byte[] bs)
        {
            UInt16 len = (UInt16)(TcpModbusPacketHead.HEAD_SIZE + bs.Length + 2);

            byte[] ret = new byte[len];
            ret[0] = BytesOP.GetHighByte(mbapIndex);
            ret[1] = BytesOP.GetLowByte(mbapIndex);
            ret[2] = BytesOP.GetHighByte(TcpModbusPacketHead.ProtocolID);
            ret[3] = BytesOP.GetLowByte(TcpModbusPacketHead.ProtocolID);
            len   -= TcpModbusPacketHead.HEAD_SIZE;
            ret[4] = BytesOP.GetHighByte(len);
            ret[5] = BytesOP.GetLowByte(len);
            ret[6] = subAddr;
            ret[7] = (byte)fc;
            Array.Copy(bs, 0, ret, 8, bs.Length);
            return(ret);
        }
コード例 #16
0
ファイル: Form4.cs プロジェクト: wangf0228GitHub/WYP
        private void button1_Click(object sender, EventArgs e)
        {
            CP1616Packet Rx1616 = new CP1616Packet(100, 1);
            bool         bOK    = false;

            for (int i = 0; i < 3; i++)
            {
                try
                {
                    byte[] deviceID = new byte[2];
                    deviceID[0] = BytesOP.GetHighByte((ushort)numericUpDown1.Value);
                    deviceID[1] = BytesOP.GetLowByte((ushort)numericUpDown1.Value);
                    byte[] tx = CP1616Packet.MakeCP1616Packet(100, 1, deviceID);
                    try
                    {
                        COMPort1.Write(tx, 0, tx.Length);
                    }
                    catch { continue; }
                    while (true)
                    {
                        try
                        {
                            if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                            {
                                MessageBox.Show("设定成功");
                                bOK = true;
                                return;
                            }
                        }
                        catch
                        { break; }
                    }
                    if (bOK)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                catch
                {
                    break;
                }
            }
            MessageBox.Show("设定失败");
        }
コード例 #17
0
//         public static byte[] MakeCP1616Packet(ushort com, byte b)
//         {
//             return MakeCP1616Packet(com, new byte[] { b });
//         }
        public static byte[] MakeCP1616Packet(ushort com, byte[] data)
        {
            byte[] txbuffer;
            if (data != null)
            {
                txbuffer    = new byte[stm32f4_TxPacketHead.HEAD_SIZE + data.Length + 2];
                txbuffer[0] = 0x16;
                txbuffer[1] = 0x16;
                txbuffer[2] = BytesOP.GetLowByte(com);
                txbuffer[3] = BytesOP.GetHighByte(com);
                txbuffer[4] = BytesOP.GetLowByte((UInt16)data.Length);
                txbuffer[5] = BytesOP.GetHighByte((UInt16)data.Length);
                txbuffer[6] = 0x00;
                txbuffer[7] = 0x00;
//                 txbuffer[6] = BytesOP.GetLowByte(retry);
//                 txbuffer[7] = BytesOP.GetHighByte(retry);
//                 txbuffer[8] = BytesOP.GetLowByte(timeout);
//                 txbuffer[9] = BytesOP.GetHighByte(timeout);
                for (int i = 0; i < data.Length; i++)
                {
                    txbuffer[i + stm32f4_TxPacketHead.HEAD_SIZE] = data[i];
                }
                txbuffer[stm32f4_TxPacketHead.HEAD_SIZE + data.Length]     = Verify.GetVerify_byteSum(txbuffer, stm32f4_TxPacketHead.HEAD_SIZE + data.Length);
                txbuffer[stm32f4_TxPacketHead.HEAD_SIZE + data.Length + 1] = 0x0d;
            }
            else
            {
                txbuffer    = new byte[stm32f4_TxPacketHead.HEAD_SIZE + 2];
                txbuffer[0] = 0x16;
                txbuffer[1] = 0x16;
                txbuffer[2] = BytesOP.GetLowByte(com);
                txbuffer[3] = BytesOP.GetHighByte(com);
                txbuffer[4] = 0;
                txbuffer[5] = 0;
                txbuffer[6] = 0;
                txbuffer[7] = 0;
//                 txbuffer[6] = BytesOP.GetLowByte(retry);
//                 txbuffer[7] = BytesOP.GetHighByte(retry);
//                 txbuffer[8] = BytesOP.GetLowByte(timeout);
//                 txbuffer[9] = BytesOP.GetHighByte(timeout);
                txbuffer[stm32f4_TxPacketHead.HEAD_SIZE]     = Verify.GetVerify_byteSum(txbuffer, stm32f4_TxPacketHead.HEAD_SIZE);
                txbuffer[stm32f4_TxPacketHead.HEAD_SIZE + 1] = 0x0d;
            }
            return(txbuffer);
        }
コード例 #18
0
 public static byte[] MakeCP1616Packet(byte com, UInt16 addr, byte[] data)
 {
     byte[] txbuffer = new byte[CP1616PacketHead.HEAD_SIZE + data.Length + 2];
     txbuffer[0] = 0x16;
     txbuffer[1] = 0x16;
     txbuffer[2] = BytesOP.GetHighByte(addr);
     txbuffer[3] = BytesOP.GetLowByte(addr);
     txbuffer[4] = com;
     txbuffer[5] = BytesOP.GetHighByte((UInt16)data.Length);
     txbuffer[6] = BytesOP.GetLowByte((UInt16)data.Length);
     for (int i = 0; i < data.Length; i++)
     {
         txbuffer[i + CP1616PacketHead.HEAD_SIZE] = data[i];
     }
     txbuffer[CP1616PacketHead.HEAD_SIZE + data.Length]     = Verify.GetVerify_byteSum(txbuffer, CP1616PacketHead.HEAD_SIZE + data.Length);
     txbuffer[CP1616PacketHead.HEAD_SIZE + data.Length + 1] = 0x0d;
     return(txbuffer);
 }
コード例 #19
0
        public static bool SerialCommand3(ushort Fn, uint nLs)
        {
            byte[] t = new byte[6];
            if (nLs < 0x300)
            {
                nLs = 0x300;
            }
            //if (nLs > 0xffff00)
            //    nLs = 0xffff00;
            t[0] = BytesOP.GetHighByte(Fn);
            t[1] = BytesOP.GetLowByte(Fn);
            t[2] = BytesOP.GetHighByte(BytesOP.GetHighShort(nLs));
            t[3] = BytesOP.GetLowByte(BytesOP.GetHighShort(nLs));
            t[4] = BytesOP.GetHighByte(BytesOP.GetLowShort(nLs));
            t[5] = BytesOP.GetLowByte(BytesOP.GetLowShort(nLs));
            CP0314Packet Rx0314 = new CP0314Packet(3);

            byte[] tx = CP0314Packet.MakeCP0314Packet(3, t);
            mySerialPort.Write(tx, 0, tx.Length);
            while (true)
            {
                try
                {
                    if (Rx0314.DataPacketed((byte)mySerialPort.ReadByte()))
                    {
                        ushort x = BytesOP.MakeShort(Rx0314.Data[0], Rx0314.Data[1]);
                        if (x == 0x4f4b)
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
                catch
                {
                    //MessageBox.Show("读取数据失败");
                    break;
                }
            }
            return(false);
        }
コード例 #20
0
        public static List <byte> PPPDataEncode(pppPocket ppp, bool bLCP)
        {
            List <byte> temp = new List <byte>();

            temp.Add(0xff);
            temp.Add(0x03);
            temp.Add(BytesOP.GetHighByte(ppp.Protocol));
            temp.Add(BytesOP.GetLowByte(ppp.Protocol));
            temp.Add(ppp.Code);
            temp.Add(ppp.ID);
            ushort len = (ushort)(ppp.Datas.Count + 4);

            temp.Add(BytesOP.GetHighByte(len));
            temp.Add(BytesOP.GetLowByte(len));
            foreach (byte b in ppp.Datas)
            {
                temp.Add(b);
            }
            ushort crc = Verify.GetVerify_CRC16_CCITT(temp.ToArray(), temp.Count);

            temp.Add(BytesOP.GetLowByte(crc));
            temp.Add(BytesOP.GetHighByte(crc));
            List <byte> ret = new List <byte>();
            //ret.Add(0x7e);
            byte x;

            foreach (byte b in temp)
            {
                x = IsNeedTrans(b, bLCP);
                if (x != 0x00)
                {
                    ret.Add(0x7d);
                    ret.Add(x);
                }
                else
                {
                    ret.Add(b);
                }
            }
            ret.Add(0x7e);
            return(ret);
        }
コード例 #21
0
        public byte[] GetTxData()
        {
            byte[] ret = new byte[24 + 12];
            ret[0]  = BytesOP.GetLowByte(TxStdId);
            ret[1]  = BytesOP.GetHighByte(TxStdId);
            ret[2]  = BytesOP.GetLowByte(TxBak1);
            ret[3]  = BytesOP.GetHighByte(TxBak1);
            ret[4]  = BytesOP.GetLowByte(BytesOP.GetLowShort(TxExtId));
            ret[5]  = BytesOP.GetHighByte(BytesOP.GetLowShort(TxExtId));
            ret[6]  = BytesOP.GetLowByte(BytesOP.GetHighShort(TxExtId));
            ret[7]  = BytesOP.GetHighByte(BytesOP.GetHighShort(TxExtId));
            ret[8]  = TxIDE;
            ret[9]  = TxRTR;
            ret[10] = TxDLC;
            ret[11] = TxBak2;//
            for (int i = 0; i < 8; i++)
            {
                ret[12 + i] = TxData[i];
            }
            ret[20] = BytesOP.GetLowByte(TxRetry);
            ret[21] = BytesOP.GetHighByte(TxRetry);
            ret[22] = BytesOP.GetLowByte(TxTimeOut);
            ret[23] = BytesOP.GetHighByte(TxTimeOut);


            ret[24] = bTxOnly;
            ret[25] = RxIDE;
            ret[26] = RxRetry;
            ret[27] = NeedRxPocket;
            ret[28] = BytesOP.GetLowByte(RxTimeOut);
            ret[29] = BytesOP.GetHighByte(RxTimeOut);
            ret[30] = BytesOP.GetLowByte(NeedStdId);
            ret[31] = BytesOP.GetHighByte(NeedStdId);
            ret[32] = BytesOP.GetLowByte(BytesOP.GetLowShort(NeedExtId));
            ret[33] = BytesOP.GetHighByte(BytesOP.GetLowShort(NeedExtId));
            ret[34] = BytesOP.GetLowByte(BytesOP.GetHighShort(NeedExtId));
            ret[35] = BytesOP.GetHighByte(BytesOP.GetHighShort(NeedExtId));
            return(ret);
        }
コード例 #22
0
        public static List <byte> PPPDataList(pppPocket ppp)
        {
            List <byte> temp = new List <byte>();

            temp.Add(0xff);
            temp.Add(0x03);
            temp.Add(BytesOP.GetHighByte(ppp.Protocol));
            temp.Add(BytesOP.GetLowByte(ppp.Protocol));
            temp.Add(ppp.Code);
            temp.Add(ppp.ID);
            ushort len = (ushort)(ppp.Datas.Count + 4);

            temp.Add(BytesOP.GetHighByte(len));
            temp.Add(BytesOP.GetLowByte(len));
            foreach (byte b in ppp.Datas)
            {
                temp.Add(b);
            }
//             ushort crc = Verify.GetVerify_CRC16_CCITT(temp.ToArray(), temp.Count);
//             temp.Add(BytesOP.GetLowByte(crc));
//             temp.Add(BytesOP.GetHighByte(crc));
            return(temp);
        }
コード例 #23
0
        public bool ProcCommand05(ushort RegAddr, ushort data)
        {
            int retry = RetryTimes;

            while (retry != 0)
            {
                byte[] txList = new byte[8];
                txList[0] = TargetAddr;
                txList[1] = 0x05;
                txList[2] = BytesOP.GetHighByte(RegAddr);
                txList[3] = BytesOP.GetLowByte(RegAddr);
                txList[4] = BytesOP.GetHighByte(data);
                txList[5] = BytesOP.GetLowByte(data);
                ushort crc = Verify.GetVerify_CRC16(txList, 6);
                txList[6] = BytesOP.GetHighByte(crc);
                txList[7] = BytesOP.GetLowByte(crc);
                Com.Write(txList, 0, txList.Length);
                Com.DiscardInBuffer();
                Com.ReadTimeout = RxTimeOut;
                byte[] rxList  = new byte[8];
                int    rxCount = 0;
                byte   rx      = 0;
                while (true)
                {
                    try
                    {
                        rx = (byte)Com.ReadByte();
                    }
                    catch
                    {
                        break;
                    }
                    rxList[rxCount++] = rx;
                    if (rxCount == 8)
                    {
                        //校验
                        ushort rxcrc  = Verify.GetVerify_CRC16(rxList, 6);
                        ushort rxcrc1 = BytesOP.MakeShort(rxList[6], rxList[7]);
                        if (rxcrc == rxcrc1)
                        {
                            return(true);
                        }
                        else
                        {
                            break;
                        }
                    }
                    else if (rxCount > 8)
                    {
                        break;
                    }
                    else if (rxCount == 2)//命令号
                    {
                        if (rx != 0x05)
                        {
                            break;
                        }
                    }
                    else if (rxCount == 1)//从机地址
                    {
                        if (rx != TargetAddr)
                        {
                            break;
                        }
                    }
                }
                Thread.Sleep(RetryInterval);
                retry--;
            }
            return(false);
        }
コード例 #24
0
 public static void CP1616ComSend(ref SerialPort serialPort, byte com, ushort addr, ushort b)
 {
     CP1616ComSend(ref serialPort, com, addr, new byte[2] {
         BytesOP.GetHighByte(b), BytesOP.GetLowByte(b)
     });
 }
コード例 #25
0
        public static byte[] MakeCP1616Packet(byte com, UInt16 addr, byte[] data)
        {
            int nIndex = 0;

            byte[] txbuffer;
            if (data != null)
            {
                txbuffer           = new byte[CP1616PacketHead.HEAD_SIZE + data.Length + 2];
                txbuffer[nIndex++] = 0x16;
                txbuffer[nIndex++] = 0x16;
                if (CP1616PacketHead.Addr_SIZE == 2)
                {
                    txbuffer[nIndex++] = BytesOP.GetHighByte(addr);
                    txbuffer[nIndex++] = BytesOP.GetLowByte(addr);
                }
                else if (CP1616PacketHead.Addr_SIZE == 1)
                {
                    txbuffer[nIndex++] = BytesOP.GetLowByte(addr);
                }
                txbuffer[nIndex++] = com;
                if (CP1616PacketHead.DataLen_SIZE == 2)
                {
                    txbuffer[nIndex++] = BytesOP.GetHighByte((UInt16)data.Length);
                    txbuffer[nIndex++] = BytesOP.GetLowByte((UInt16)data.Length);
                }
                else
                {
                    txbuffer[nIndex++] = BytesOP.GetLowByte((UInt16)data.Length);
                }
                for (int i = 0; i < data.Length; i++)
                {
                    txbuffer[i + CP1616PacketHead.HEAD_SIZE] = data[i];
                }
                txbuffer[CP1616PacketHead.HEAD_SIZE + data.Length]     = Verify.GetVerify_byteSum(txbuffer, CP1616PacketHead.HEAD_SIZE + data.Length);
                txbuffer[CP1616PacketHead.HEAD_SIZE + data.Length + 1] = 0x0d;
            }
            else
            {
                txbuffer           = new byte[CP1616PacketHead.HEAD_SIZE + 2];
                txbuffer[nIndex++] = 0x16;
                txbuffer[nIndex++] = 0x16;
                if (CP1616PacketHead.Addr_SIZE == 2)
                {
                    txbuffer[nIndex++] = BytesOP.GetHighByte(addr);
                    txbuffer[nIndex++] = BytesOP.GetLowByte(addr);
                }
                else if (CP1616PacketHead.Addr_SIZE == 1)
                {
                    txbuffer[nIndex++] = BytesOP.GetLowByte(addr);
                }
                txbuffer[nIndex++] = com;
                if (CP1616PacketHead.DataLen_SIZE == 2)
                {
                    txbuffer[nIndex++] = 0;
                    txbuffer[nIndex++] = 0;
                }
                else
                {
                    txbuffer[nIndex++] = 0;
                }
                txbuffer[CP1616PacketHead.HEAD_SIZE]     = Verify.GetVerify_byteSum(txbuffer, CP1616PacketHead.HEAD_SIZE);
                txbuffer[CP1616PacketHead.HEAD_SIZE + 1] = 0x0d;
            }
            return(txbuffer);
        }
コード例 #26
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");
            }));
        }
コード例 #27
0
 public static CP1616Packet CP1616ComProc(ref SerialPort serialPort, byte com, ushort addr, ushort b)
 {
     return(CP1616ComProc(ref serialPort, com, addr, new byte[2] {
         BytesOP.GetHighByte(b), BytesOP.GetLowByte(b)
     }, 5));
 }
コード例 #28
0
        public List <byte> ToArray(FakeTcpHeader fh)
        {
            List <Byte> FakeTCP = new List <Byte>();

            //构建TCP的伪首部
            FakeTCP.Add(fh.SourceAddress[0]);
            FakeTCP.Add(fh.SourceAddress[1]);
            FakeTCP.Add(fh.SourceAddress[2]);
            FakeTCP.Add(fh.SourceAddress[3]);
            FakeTCP.Add(fh.DastinationAddress[0]);
            FakeTCP.Add(fh.DastinationAddress[1]);
            FakeTCP.Add(fh.DastinationAddress[2]);
            FakeTCP.Add(fh.DastinationAddress[3]);
            FakeTCP.Add(0x00);
            FakeTCP.Add(0x06);
            FakeTCP.Add(BytesOP.GetHighByte(fh.count));
            FakeTCP.Add(BytesOP.GetLowByte(fh.count));
            //伪首部完成
            FakeTCP.Add(BytesOP.GetHighByte(SourcePort));
            FakeTCP.Add(BytesOP.GetLowByte(SourcePort));
            FakeTCP.Add(BytesOP.GetHighByte(DastinationPort));
            FakeTCP.Add(BytesOP.GetLowByte(DastinationPort));
            Union_UInt32 u32 = new Union_UInt32();

            u32.ofs_32 = InitialSeqNumber;
            FakeTCP.Add(u32.ofs_h.ofs_h);
            FakeTCP.Add(u32.ofs_h.ofs_l);
            FakeTCP.Add(u32.ofs_l.ofs_h);
            FakeTCP.Add(u32.ofs_l.ofs_l);
            u32.ofs_32 = AckSeqNumber;
            FakeTCP.Add(u32.ofs_h.ofs_h);
            FakeTCP.Add(u32.ofs_h.ofs_l);
            FakeTCP.Add(u32.ofs_l.ofs_h);
            FakeTCP.Add(u32.ofs_l.ofs_l);
            FakeTCP.Add((byte)(DataOffset << 4));
            FakeTCP.Add(TCPFlags);
            FakeTCP.Add(BytesOP.GetHighByte(Window));
            FakeTCP.Add(BytesOP.GetLowByte(Window));
            int sumaddr = FakeTCP.Count;

            FakeTCP.Add(0x00);
            FakeTCP.Add(0x00);
            FakeTCP.Add(BytesOP.GetHighByte(UrgentPoint));
            FakeTCP.Add(BytesOP.GetLowByte(UrgentPoint));
            if (DataOffset > 0x05)
            {
                for (int i = 0; i < ((DataOffset - 0x05) * 4); i++)
                {
                    FakeTCP.Add(TCPOptions[i]);
                }
            }
            foreach (byte b in Datas)
            {
                FakeTCP.Add(b);
            }
            CheckSum             = Verify.GetVerify_IP(FakeTCP.ToArray());
            FakeTCP[sumaddr]     = BytesOP.GetHighByte(CheckSum);
            FakeTCP[sumaddr + 1] = BytesOP.GetLowByte(CheckSum);
            FakeTCP.RemoveRange(0, 12);
            if (Datas.Count == 0)
            {
                if (TCPFlags == 0x02)
                {
                    InitialSeqNumber++;
                }
            }
            else
            {
                InitialSeqNumber += (uint)Datas.Count;
            }
            return(FakeTCP);
        }
コード例 #29
0
        private void button1_Click(object sender, EventArgs e)
        {
            Rx1616 = new CP1616Packet(100, 1);
            string[] ports = SerialPort.GetPortNames();
            PortName           = "";
            progressBar1.Value = 0;
            int perCount, per;

            perCount = ports.Length * 3;
            per      = 100 / perCount;
            bool bOK = false;

            foreach (string com in ports)
            {
                COMPort1.PortName = com;
                for (int i = 0; i < 3; i++)
                {
                    progressBar1.Value += per;
                    try
                    {
                        COMPort1.Open();
                        byte[] deviceID = new byte[2];
                        deviceID[0] = BytesOP.GetHighByte((ushort)numericUpDown1.Value);
                        deviceID[1] = BytesOP.GetLowByte((ushort)numericUpDown1.Value);
                        byte[] tx = CP1616Packet.MakeCP1616Packet(100, 1, deviceID);
                        try
                        {
                            COMPort1.Write(tx, 0, tx.Length);
                        }
                        catch { continue; }
                        while (true)
                        {
                            try
                            {
                                if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                                {
                                    MessageBox.Show("设定成功");
                                    bOK = true;
                                    break;
                                }
                            }
                            catch
                            { break; }
                        }
                        if (bOK)
                        {
                            break;
                        }
                        COMPort1.Close();
                        Thread.Sleep(100);
                    }
                    catch
                    {
                        break;
                    }
                }
                if (bOK)
                {
                    break;
                }
            }
            if (!bOK)
            {
                progressBar1.Value = 100;
                MessageBox.Show("未找到设备");
            }
            COMPort1.Close();
        }
コード例 #30
0
ファイル: CP68.cs プロジェクト: wangf0228GitHub/WFLib
 public static CP68Packet CP68ComProc(ref SerialPort serialPort, byte com, ushort b)
 {
     return(CP68ComProc(ref serialPort, com, CP68Packet.DeviceAddr, new byte[2] {
         BytesOP.GetHighByte(b), BytesOP.GetLowByte(b)
     }, 5));
 }