コード例 #1
0
        /// <summary>
        /// Sends the file.
        /// </summary>
        /// <param name="requestFile">The SML file.</param>
        /// <param name="isManufacturerMode">if set to <c>true</c> [is manufacturer mode].</param>
        /// <returns>
        /// True if file could be written down the serial interface.
        /// </returns>
        private async Task<object> SendFile(SmlFile requestFile, bool isManufacturerMode = false)
        {
            if (requestFile == null)
            {
                throw new ArgumentNullException("requestFile");
            }

            if (SerialPort.GetPortNames().Contains(portName) == false)
            {
                logger.Error("Port {0} is not existing.", portName);
                throw new ArgumentException("Port " + portName + " is not existing");
            }

            Core.Obis.ObisId reqObis;
            if (requestFile[1].GetProcParameterRequest != null)
            {
                reqObis = requestFile[1].GetProcParameterRequest.TreePath.First();
                logger.Warn("READ OBIS: {0}", reqObis.ToHexString());
            }
            else if (requestFile[1].SetProcParameterRequest != null)
            {
                reqObis = requestFile[1].SetProcParameterRequest.TreePath.First();
                logger.Warn("WRITE OBIS: {0}", reqObis.ToHexString());
            }
            else
            {
                throw new InvalidOperationException();
            }

            var sendBuff = new List<byte>();
            requestFile.Serialize(sendBuff);

            logger.Debug("Sending SML file of size {0}.", sendBuff.Count);

            using (await asyncLock.LockAsync().ConfigureAwait(false))
            //using (var token = new CancellationTokenSource(1000))
            {
                // try to read response from meter
                try
                {
                    // WriteAsync does not work with Advantech boxes
                    //await 
                    serialPort.Write(sendBuff.ToArray(), 0, sendBuff.Count);
                    //.WriteAsync(sendBuff.ToArray(), 0, sendBuff.Count, token.Token).ConfigureAwait(false);

                    logger.Trace("TX: {0}", BitConverter.ToString(sendBuff.ToArray()));
                    logger.Debug("File sent, waiting for response from meter.");

                    var receivedFile = await ReceiveSmlFile(TimeSpan.FromSeconds(8))
                                        .ConfigureAwait(false);

                    // process received file
                    var result = GetMeterResponse(requestFile, receivedFile);
                    return result;
                }
                catch (Exception ex)
                {
                    logger.ErrorException("ReceiveSmlFile failed: " + ex.StackTrace, ex);
                    throw;
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Sends the file.
        /// </summary>
        /// <param name="requestFile">The SML file.</param>
        /// <param name="isManufacturerMode">if set to <c>true</c> [is manufacturer mode].</param>
        /// <returns>
        /// True if file could be written down the serial interface.
        /// </returns>
        private async Task<object> SendFile(SmlFile requestFile, bool isManufacturerMode = false)
        {
            if (requestFile == null)
            {
                throw new ArgumentNullException(nameof(requestFile));
            }

            Core.Obis.ObisId reqObis;
            if (requestFile[1].GetProcParameterRequest != null)
            {
                reqObis = requestFile[1].GetProcParameterRequest.TreePath.First();
                logger.Warn("READ OBIS: {0}", reqObis.ToHexString());
            }
            else if (requestFile[1].SetProcParameterRequest != null)
            {
                reqObis = requestFile[1].SetProcParameterRequest.TreePath.First();
                logger.Warn("WRITE OBIS: {0}", reqObis.ToHexString());
            }
            else
            {
                throw new InvalidOperationException();
            }

            var sendBuff = new List<byte>();
            requestFile.Serialize(sendBuff);

            this.logger.Debug("Sending SML file of size {0}.", sendBuff.Count);

            using (await this.asyncLock.LockAsync())
            {
                // try to read response from meter
                try
                {
                    this.EnsureConnected();

                    // WriteAsync does not work with Advantech boxes
                    //await 
                    this.tcpPort.GetStream().Write(sendBuff.ToArray(), 0, sendBuff.Count);
                    //.WriteAsync(sendBuff.ToArray(), 0, sendBuff.Count, token.Token);

                    this.logger.Trace("TX: {0}", BitConverter.ToString(sendBuff.ToArray()));
                    this.logger.Debug("File sent, waiting for response from meter.");

                    var receivedFile = await this.ReceiveSmlFile(TimeSpan.FromSeconds(8));

                    // process received file
                    var result = this.GetMeterResponse(requestFile, receivedFile);
                    return result;
                }
                catch (Exception ex)
                {
                    this.logger.ErrorException("ReceiveSmlFile failed: " + ex.StackTrace, ex);
                    this.tcpPort.Close();
                    throw;
                }
            }
        }