HomeGenie Forum

Automation Program Plugins and Wizard Scripting => Raspberry Pi GPIO/SPI/I2C => Topic started by: Solar_Man on February 17, 2017, 09:45:41 PM

Title: SerialPort Action<byte[]> buffer not emptying
Post by: Solar_Man on February 17, 2017, 09:45:41 PM
Hello,

Great Program this is! Works flawlessly with MBED controller as Solar Domestic Hot Water system controller.  HG will also display in a custom Widget the volts/amps/etc of a Magnum MS4024PAE Solar Inverter. I'm having trouble with SerialPort Action<byte[]> buffer not emptying. I grabbed the string the inverter sends out every 100ms with Cutecom:

'40 00 01 4a 00 00 7a 00 01 00 33 0c 23 17 6B 00 00 00 02 58 00 FF' in Hex mode
or
"@\0x00\0x01J\0x00\0x00z\0x00\0x01\0x003\0x0c\0x1d\0x17k\0x00\0x00\0x00\0x02X\0x00\0xff" in ascii mode

It is sent in bytes and is terminated only by the '0xff'. In the byte[] handler, it does not seem to respond to the EndofLine character.  I am emulating this string with an MBED and am sending string every 4seconds, I'm seeing RxCount going up by by chunks to 22 bytes, at which point it changes the widget display fine. But RxCount  keeps rolling up to 44, 46 and higher, serial port connects and disconnects and I see huge strings of 00 E0 00 E0. I assume buffer is overflowing. I have try many methods to clear buffer.

I don't have access to try something like this: I'm sure this would clean it out.

while (this.serialPort.IsOpen)
{
    int b = this.serialPort.ReadByte();
    if (b != -1)
    {
        // data is good here
    }
}

Thanks for any ideas anyone might have!!

 
Code: [Select]

HandleMessageReceived = (byte[] message) => {
 
  int RxCount = message.Length;
  byte[] Rx_Buffer = new byte[RxCount];
  Buffer.BlockCopy(message, 0, Rx_Buffer, 0, RxCount);

  // try different ways to empty buffer
  //Rx_Buffer = message;
  //RxBuffer = message.ToArray();
  test++; // count # of times this HandleMessageReceived handler is called

  Program.Notify("Rx_B:" + BitConverter.ToString(Rx_Buffer), "test:" + test + "  RxCount:" + RxCount);   Pause(1); // message[21]
  if (Rx_Buffer.Length < 22 || Rx_Buffer.Length > 22){ return; }// || message[21] != 255
  string msgString = BitConverter.ToString(Rx_Buffer);
  string[] reqs = msgString.Split('-');
  string address = "40";
  //int add = Int32.Parse (reqs[1]);
  var module = Modules.InDomain(_domain).WithAddress(address).Get();
  if( module == null) return;

  // update Inverter.Status
  string dict_value = "";
  byte bite = byte.Parse(reqs[0], System.Globalization.NumberStyles.HexNumber);
  Inverter_Status.TryGetValue( bite, out dict_value);
  Program.RaiseEvent(module, "Sensor.Status", dict_value, "");
}

byte eol = 255;
SerialPort.EndOfLine = eol.ToString();