Ejemplo n.º 1
0
        public byte[] ReadBytes(DeviceAddress address, ushort size)
        {
            var    func         = (byte)address.DBNumber;
            string tempStrg     = string.Empty;
            string buffReceiver = string.Empty;

            try
            {
                lock (_async)
                {
                    switch (func)
                    {
                    case 0:

                        byte[] frame = XGTCnetMessage.ReadCoilStatusMessage(Convert.ToByte(address.Area), "MB", Convert.ToString(address.Start), size);
                        _serialPort.Write(frame, 0, frame.Length);
                        Thread.Sleep(100);

                        break;

                    case 1:
                        byte[] frame1 = XGTCnetMessage.ReadInputStatusMessage(Convert.ToByte(address.Area), "PB", Convert.ToString(address.Start), size);
                        _serialPort.Write(frame1, 0, frame1.Length);
                        Thread.Sleep(100);
                        break;

                    case 4:

                        byte[] frame4 = XGTCnetMessage.ReadHoldingRegistersMessage(Convert.ToByte(address.Area), "DW", Convert.ToString(address.Start), size);
                        _serialPort.Write(frame4, 0, frame4.Length);
                        Thread.Sleep(100);

                        break;

                    default:
                        break;
                    }
                    if (this._serialPort.BytesToRead >= 10)
                    {
                        buffReceiver = this._serialPort.ReadExisting();
                        this._serialPort.DiscardInBuffer();
                    }
                    if (buffReceiver != null)
                    {
                        tempStrg = buffReceiver.Substring(1, buffReceiver.Length - 2);
                        tempStrg = tempStrg.Remove(0, 9);
                        return(Conversion.HexToBytes(tempStrg));
                    }
                }
                return(null);
            }
            catch (Exception e)
            {
                if (OnError != null)
                {
                    OnError(this, new IOErrorEventArgs(e.Message));
                }
                return(null);
            }
        }
Ejemplo n.º 2
0
        public int WriteSingleRegister(int id, int startAddress, byte[] values)
        {
            lock (_async)
            {
                byte[] frame = XGTCnetMessage.WriteSingleRegisterMessage(Convert.ToByte(id), Convert.ToString(startAddress), values);
                _serialPort.Write(frame, 0, frame.Length);
                Thread.Sleep(100);

                int numBytesRead = 0;
                var frameBytes   = new byte[5];
                while (numBytesRead != frameBytes.Length)
                {
                    numBytesRead += _serialPort.Read(frameBytes, numBytesRead, frameBytes.Length - numBytesRead);
                }
                var slave = frameBytes[0];
                var code  = frameBytes[1];
                if (code == 0x85)//错误则只需读5字节
                {
                    var errorcode = frameBytes[2];
                    if (OnError != null)
                    {
                        // OnError(this, new IOErrorEventArgs(Modbus.GetErrorString(errorcode)));
                    }
                    Thread.Sleep(10);
                    return(-1);
                }
                else//正确需8字节
                {
                    numBytesRead = 0;
                    while (numBytesRead < 3)
                    {
                        numBytesRead += _serialPort.Read(frameBytes, numBytesRead, 3 - numBytesRead);
                    }
                    Thread.Sleep(10);
                    return(0);
                }
            }
        }