예제 #1
0
        public async Task <bool> WriteRegsAsync(UInt16 reg_addr, params byte[] data)
        {
            byte[] buf = new byte[12 + data.Length];
            _cmd   = 0x0b;
            buf[0] = 0x5a;
            buf[1] = 0xa5;
            buf[2] = (byte)(buf.Length / 256);
            buf[3] = (byte)(buf.Length & 0xff);
            buf[4] = _cmd;
            buf[5] = _target;
            buf[6] = (byte)(reg_addr >> 8);
            buf[7] = (byte)(reg_addr & 0xff);
            buf[8] = (byte)(data.Length >> 8);
            buf[9] = (byte)(data.Length & 0xff);
            for (int i = 0; i < data.Length; i++)
            {
                buf[10 + i] = data[i];
            }
            bool   b;
            UInt16 CRC = HWCommandAnalyzer.CalcCRC16(0X0000, buf, buf.Length - 2);

            buf[buf.Length - 2] = (byte)(CRC >> 8);
            buf[buf.Length - 1] = (byte)(CRC & 0xff);
            _sbp.Send_Data(_channel, buf, buf.Length);

            b = await Task.Run <bool>(new Func <bool>(waitwriteresponse));;
            if (b)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #2
0
        public void OnResponseArrival(byte[] dataBuffer, int length)
        {
            UInt16 crc = HWCommandAnalyzer.CalcCRC16(0, dataBuffer, dataBuffer.Length - 2);

            if (dataBuffer[dataBuffer.Length - 2] * 256 + dataBuffer[dataBuffer.Length - 1] != crc)
            {
                return;
            }
            if (_cmd == (byte)~dataBuffer[4])
            {
                if (_cmd == 0x0b)
                {
                    _wr_event.Set();
                }
                if (_cmd == 0x0c)
                {
                    int size = dataBuffer[8] * 256 + dataBuffer[9];
                    _readbuf = new byte[size];
                    for (int i = 0; i < size; i++)
                    {
                        _readbuf[i] = dataBuffer[10 + i];
                    }
                    _rd_event.Set();
                }
            }
            else if (~dataBuffer[4] == 0x15)
            {
                // 通知信息到
                RxNotifyArrival?.Invoke(dataBuffer[6], dataBuffer[8]);
            }
        }
예제 #3
0
        public async Task <bool> ReadRegsAsync(UInt16 reg_addr, byte[] data)
        {
            byte[] buf = new byte[12 + data.Length];
            _cmd = 0x0c;
            /* 生成读数据帧 */
            buf[0] = 0x5a;
            buf[1] = 0xa5;
            buf[2] = (byte)(buf.Length / 256);
            buf[3] = (byte)(buf.Length & 0xff);
            buf[4] = _cmd;
            buf[5] = _target;
            buf[6] = (byte)(reg_addr >> 8);
            buf[7] = (byte)(reg_addr & 0xff);
            buf[8] = (byte)(data.Length >> 8);
            buf[9] = (byte)(data.Length & 0xff);
            UInt16 CRC = HWCommandAnalyzer.CalcCRC16(0X0000, buf, buf.Length - 2);

            buf[buf.Length - 2] = (byte)(CRC >> 8);
            buf[buf.Length - 1] = (byte)(CRC & 0xff);
            /*发送数据帧*/
            _sbp.Send_Data(_channel, buf, buf.Length);
            /*启动应答等待任务*/
            Task <bool> task = new Task <bool>(new Func <bool>(waitreadresponse));

            task.Start();
            /*等待任务完成返回*/
            bool b = await task;

            if (b)  //结果位True,收到应答帧
            {
                if (_readbuf.Length != data.Length)
                {
                    b = false;
                }
                else
                {
                    _readbuf.CopyTo(data, 0);
                }
            }
            else //结果为False,没有收到应答帧
            {
            }
            return(b);
        }