Beispiel #1
0
        private Char[] SendData(Char type, int startAdd, Char cmd, char[] inChar) //(S、D、M、X、Y、T、C)   (E00/1000/100/80/A0/C0/1C0)   (0、1、w/W、r/R)
        {
            Char[] outChar = null;

            //报文长度
            int n = 0;

            if (cmd == '0' || cmd == '1') //  0、1复位置位
            {
                n = 9;
            }
            else if (cmd == 'R' || cmd == 'r') // R/r读单双字
            {
                n = 11;
            }
            else if (cmd == 'w' || cmd == 'W') // w写单字
            {
                n = 11 + inChar.Length;
            }

            char[] subuffer = new char[n];

            //报文首
            subuffer[0] = (char)2;

            //命令代码
            if (cmd == '0')
            {
                subuffer[1] = (char)0x38;
            }
            else if (cmd == '1')
            {
                subuffer[1] = (char)0x37;
            }
            else if (n == 11)
            {
                subuffer[1] = (char)0x30;
            }
            else
            {
                subuffer[1] = (char)0x31;
            }

            //首地址
            int Addr = 0;

            if (type == 'S' || type == 's') // S、s
            {
                Addr = 0xE00;
            }
            else if (type == 'D' || type == 'd') // D、d
            {
                Addr = 0x1000;
            }
            else if (type == 'M' || type == 'm') // M、m
            {
                Addr = 0x100;
            }
            else if (type == 'X' || type == 'x') // X、x
            {
                Addr = 0x80;
            }
            else if (type == 'Y' || type == 'y') // Y、y
            {
                Addr = 0xA0;
            }
            else if (type == 'T' || type == 't') // T、t
            {
                Addr = 0xC0;
            }
            else if (type == 'C' || type == 'c') // C、c
            {
                Addr = 0x1C0;
            }

            //计算首地址
            Char[] Addrs = new Char[4];
            if (n == 9)
            {
                Addr  = Addr * 8 + startAdd;
                Addrs = HTC(Addr, 4);
            }
            else
            {
                Addr += (startAdd * 2);
                Addrs = StartData(Addr);
            }
            Addrs.CopyTo(subuffer, 2);

            //字节长度
            if (circle)
            {
                if (cmd == 'R' || cmd == 'W') // R/W  读写双字
                {
                    subuffer[7] = Tran2(m_sendLen % 0x10);
                    subuffer[6] = Tran2(m_sendLen / 0x10);
                }
                else if (cmd == 'r' || cmd == 'w') // r/w  读写单字
                {
                    subuffer[7] = Tran2(m_recieveLen % 0x10);
                    subuffer[6] = Tran2(m_recieveLen / 0x10);
                }
            }
            else
            {
                subuffer[6] = (char)0x30;
                if (cmd == 'R' || cmd == 'W') // R/W  读写双字
                {
                    subuffer[7] = (char)0x34;
                }
                else if (cmd == 'r' || cmd == 'w') // r/w  读写单字
                {
                    subuffer[7] = (char)0x32;
                }
            }

            //数据内容
            if (cmd == 'w' || cmd == 'W')
            {
                inChar.CopyTo(subuffer, 8);
            }


            //报文尾
            subuffer[n - 3] = (char)3;

            //校验码
            Char[] T = new Char[2];
            T = CCD(subuffer, n - 3);
            T.CopyTo(subuffer, n - 2);

            if (m_sp.IsOpen)
            {
                m_sp.Write(subuffer, 0, n);
                //MessageBox.Show("已发送成功!");
                for (int i = 0; i < 5; i++)
                {
                    Thread.Sleep(100);
                    int Len = m_sp.BytesToRead;
                    if (Len > 0)
                    {
                        i = 5;  //跳出循环
                        char[] RecieveBuf = new char[Len];
                        m_sp.Read(RecieveBuf, 0, Len);

                        if (Len == 1)   //非写入或出错状态
                        {
                            outChar = RecieveBuf;
                        }
                        else                              //写入状态,读取返回数据
                        {
                            T = CCD(RecieveBuf, Len - 3); //验证接收信息校验码
                            if (T[0] == RecieveBuf[Len - 2] && T[1] == RecieveBuf[Len - 1])
                            {
                                Char[] outChar2 = new Char[Len - 4];
                                Array.Copy(RecieveBuf, 1, outChar2, 0, Len - 4);
                                outChar = outChar2;
                            }
                        }
                    }
                }
            }
            return(outChar);
        }