示例#1
0
        private void SerialDataIntBUSResponse(string addingMessage)
        {
            this.IntPort.DiscardInBuffer();
            this.IntPort.DiscardOutBuffer();
            List <byte> preambule    = currentDevice.CalculatePreambule();
            string      strPreambule = BitConverter.ToString(preambule.ToArray()).Replace('-', ' ');

            string message = BitConverter.ToString(this.intbusRecievedBuffer.ToArray()).Replace('-', ' ');

            if (this.MbPort != null && this.MbPort.IsOpen)
            {
                //Подмена модбас адреса для скады(исходного модбас мастера)
                if (this.intbusRecievedBuffer.Count > preambule.Count())
                {
                    this.intbusRecievedBuffer[preambule.Count()] = (byte)this.currentDevice.VirtualModbusAddress;
                }
                this.MbPort.Write(this.intbusRecievedBuffer.ToArray(), 0, this.intbusRecievedBuffer.Count);
            }

            this.WriteLog($"{this.currentDevice.Name}  RX: [{strPreambule}] {message} {addingMessage}");

            this.intbusRecievedBuffer.Clear();
        }
示例#2
0
        private void MbPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int byteRecieved = 0;

            byte[]      receivedBytes = null;
            int         mbAddress     = 0xFFFF;
            string      message       = null;
            List <byte> intbusFrame;

            try
            {
                byteRecieved  = (sender as SerialPort).BytesToRead;
                receivedBytes = new byte[byteRecieved];
                (sender as SerialPort).Read(receivedBytes, 0, byteRecieved);

                mbAddress = receivedBytes.First();
                if (!this.modbusAddressDictionary.ContainsKey(mbAddress))
                {
                    throw new Exception($"Ошибка запрашиваемый адрес({mbAddress}) modbus не существует " +
                                        $"в Intbus пространстве");
                }

                this.currentDevice = this.modbusAddressDictionary[mbAddress];

                intbusFrame = this.currentDevice.ConvertToIntbus(receivedBytes.ToList());
                if (intbusFrame == null)
                {
                    throw new Exception($"Exception: {this.currentDevice.Name} ConvertToIntbus: " +
                                        $"{BitConverter.ToString(intbusFrame.ToArray())}");
                }
            }
            catch (Exception ex)
            {
                this.WriteLog(ex.Message);
                this.WriteLog(ex.StackTrace);
                return;
            }


            if (this.IntPort != null && this.IntPort.IsOpen)
            {
                message = null;

                List <byte> currentPreambule = this.currentDevice.CalculatePreambule();
                for (int i = currentPreambule.Count; i < intbusFrame.Count; i++)
                {
                    message += String.Format("{0:X2} ", intbusFrame[i]);
                }
                /////////////////////////////////
                receivedBytes = intbusFrame.ToArray();
                this.IntPort.DiscardOutBuffer();
                this.IntPort.DiscardInBuffer();
                this.IntPort.Write(receivedBytes, 0, receivedBytes.Length);

                while (this.IntPort.BytesToWrite != 0)
                {
                    ;
                }
                this.sendTime = DateTime.Now;

                if (this.timeoutTask != null)
                {
                    cancelTimeoutToken.Cancel();
                    this.timeoutTask.Wait();
                    cancelTimeoutToken.Dispose();
                    cancelTimeoutToken    = new CancellationTokenSource();
                    this.timeoutTaskToken = cancelTimeoutToken.Token;
                }
                ;

                this.timeoutTask = Task.Factory.StartNew(() =>
                {
                    while (true)
                    {
                        if (this.timeoutTaskToken.IsCancellationRequested)
                        {
                            return;
                        }
                        if (DateTime.Now - sendTime >= TimeSpan.FromMilliseconds(this.responseTimeout))
                        {
                            byteRecieved = this.IntPort.BytesToRead;

                            byte[] responseBytes = new byte[byteRecieved];

                            this.IntPort.Read(responseBytes, 0, byteRecieved);
                            this.intbusRecievedBuffer.AddRange(responseBytes);
                            if (this.intbusRecievedBuffer.Count == 0)
                            {
                                this.SerialDataIntBUSResponse("TIMEOUT, NO RESPONSE");
                            }
                            else
                            {
                                this.SerialDataIntBUSResponse("TIMEOUT, CRC ERROR");
                            }

                            return;
                        }
                    }
                });
                string preambule = BitConverter.ToString(currentDevice.CalculatePreambule().ToArray()).Replace('-', ' ');
                this.WriteLog($"{this.currentDevice.Name}  TX: " +
                              $"[{preambule}] {message}");
            }
        }