private void AssertPortOpenness()
        {
            if (!port.IsOpen)
            {
                try
                {
                    port.Open();

                    logger.info(
                        "Opened SerialMeterConnection with parameters {0}{1}{2}@{3}",
                        DataBits,
                        StopBits,
                        Handshake,
                        BaudRate);
                }
                catch (System.IO.IOException exception)
                {
                    throw new MeterException("Permission denied. Check permissions for " + PortName +
                                             ", fix with 'usermod -a -G dialout <USER_NAME>'");
                }

                System.Threading.Thread.Sleep(100);

                if (!port.IsOpen)
                {
                    throw new MeterException("Failed opening port " + PortName);
                }
            }
        }
示例#2
0
        public Dictionary <string, string> Fetch1()
        {
            byte[] responseComplete = SendCommand(FETCH1);

            byte[] responseBody = responseComplete.Subset(MAGICBYTE_LENGTH,
                                                          responseComplete.Length - MAGICBYTE_LENGTH - CHECKSUM_LENGTH);

            byte responseChecksum = FromKamstrupPair(responseComplete.Subset(
                                                         responseComplete.Length - CHECKSUM_LENGTH, CHECKSUM_LENGTH)
                                                     );

            byte calculatedChecksum = Checksum(FromKamstrupBytes(responseBody));

            if (calculatedChecksum == responseChecksum)
            {
                logger.info("Checksum validated: " + calculatedChecksum);
            }
            else
            {
                throw new MeterException("Invalid checksum. Calculated value of " + calculatedChecksum + " does not match response checksum value of " + responseChecksum);
            }

            Dictionary <string, string> result = new Dictionary <string, string>();

            result.Add("METER_TYPE", Encoding.ASCII.GetString(FromKamstrupBytes(responseBody.Subset(4, 20))));

            result.Add(
                "METER_ID", (
                    UInt32.Parse(
                        Encoding.ASCII.GetString(
                            responseBody.Subset(62, 8).ReverseInPairs()
                            ), NumberStyles.HexNumber
                        )
                    ).ToString()
                );

            result.Add(
                "CUSTOMER_NO", (
                    UInt32.Parse(
                        Encoding.ASCII.GetString(
                            responseBody.Subset(70, 8).ReverseInPairs()
                            ), NumberStyles.HexNumber
                        )
                    ).ToString()
                );


            // EXPERIMENTAL

            result.Add("TEST1", UInt32.Parse(Encoding.ASCII.GetString(responseBody.Subset(24, 10).ReverseInPairs()), NumberStyles.HexNumber).ToString());
            result.Add("TEST2", UInt32.Parse(Encoding.ASCII.GetString(responseBody.Subset(34, 6).ReverseInPairs()), NumberStyles.HexNumber).ToString());
            result.Add("TEST3", UInt32.Parse(Encoding.ASCII.GetString(responseBody.Subset(40, 4).ReverseInPairs()), NumberStyles.HexNumber).ToString());

            return(result);
        }
示例#3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="com.bangbits.metering.MeterProtocolKMP382"/> class.
 /// A Meter needs to have a MeterDriver passed to it, which is responsible for the physical connection.
 /// </summary>
 /// <param name='device'>
 /// Driver.
 /// </param>
 public MeterProtocolKMP382(IMeterConnection device, ILoggingBridge logger)
 {
     this.device = device;
     this.logger = logger;
     logger.info("Instantiated MeterProtocolKMP382");
 }