jorticus / pymate

Outback MATE python interface
https://jared.geek.nz/pymate
GNU General Public License v2.0
28 stars 9 forks source link

Error receiving mate packet - Invalid checksum #24

Open lluiscab opened 3 years ago

lluiscab commented 3 years ago

Hi,

I'm trying to use this library with a FlexMax 60 charge controller.

Unfortunately I seem to be stuck on an infinite "Invalid checksum" error cycle which I assume might be related to #22

This is the code that I'm currently running

from pymate.matenet import MateNET, MateDevice, MateMXDevice, MateFXDevice, MateDCDevice
import logging

log = logging.getLogger('mate')
log.setLevel(logging.DEBUG)
log.addHandler(logging.StreamHandler())

mate_bus = MateNET('/dev/ttyUSB0') # Linux
mate_mx = MateMXDevice(mate_bus, port=0) # 0: No hub. 1-9: Hub port
mate_mx.scan()  # This will raise an exception if the device isn't found

status = mate_mx.get_status()
print(status)

And this is the output I'm getting

Send [Port0, Type=0x02, Addr=0x0000, Param=0x0000]
TX: [1] 00
TX: [0] 02 00 00 00 00 00 02
RX: 02 00 60 f0
RETRY
TX: [1] 00
TX: [0] 02 00 00 00 00 00 02
RX: 02 00 03 00 f0
RETRY
TX: [1] 00
TX: [0] 02 00 00 00 00 00 02
RX: 02 00 60 f0
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    mate_mx.scan()  # This will raise an exception if the device isn't found
  File "pymate/matenet/mx.py", line 154, in scan
    devid = super(MateMXDevice, self).scan()
  File "pymate/matenet/matedevice.py", line 38, in scan
    return self.matenet.scan(self.port)
  File "pymate/matenet/matenet.py", line 169, in scan
    result = self.query(0x00, port=port)
  File "pymate/matenet/matenet.py", line 134, in query
    resp = self.send(MateNET.TYPE_QUERY, addr=reg, param=param, port=port)
  File "pymate/matenet/matenet.py", line 87, in send
    rxbuf = self.port.recv()
  File "pymate/matenet/matenet_ser.py", line 159, in recv
    return MateNETSerial._parse_packet(rawdata)
  File "pymate/matenet/matenet_ser.py", line 118, in _parse_packet
    % (expected_chksum, actual_chksum))
RuntimeError: Error receiving mate packet - Invalid checksum (Expected:60f0, Actual:0002)

As you can see in the second attempt, I get the response 02 00 03 00 f0 which to me looks to be what I'd expect (from reading the protocol spec), 02 indicating a read command response, 00 03 indicating an MX unit and 00 f0 as the checksum, even tho it seems to be invalid as I'd expect it to be 00 05 from what I can see from the docs, but neither the checksum from the unit nor the expected checksum seem to be valid.

I would appreciate any help on this.

Thanks

jorticus commented 3 years ago

I think your serial connection might not be 100% reliable. For the same TX command, you receive 3 different responses:

RX: 02 00 60 f0       0010 0000 .... .0000 0110 0000 ... 1111 0000
RX: 02 00 03 00 f0    0010 0000 0000 0000 0011 0000 0000 1111 0000
RX: 02 00 60 f0       0010 0000 .... .0000 0110 0000 ... 1111 0000

The second one looks mostly correct, but the other two don't.

For clarity I've put the binary encoding next to each packet, and you can see roughly 8 bits have been dropped (NOTE: I haven't shown the 9th bit in each byte)

Do you have a logic analyzer or oscilloscope handy?

If you loop back RX & TX, can you send and receive the same data? (Disconnect the MATE from the opto-isolator, connect RX & TX together, provide 24V, and open the serial port in a terminal program and try typing - do you see any dropped chars?)

jorticus commented 3 years ago

Also just realised I'd never merged in the fix for issue 22, but should be there now. I don't believe that's the cause of what you're seeing however...

lluiscab commented 3 years ago

I unfortunately do not have any logic analyzer or oscilloscope to use, I'm not quite there yet.

I'll try to test the circuit looping back RX & TX and I'll get back to you once that test is complete.

lluiscab commented 3 years ago

Hi,

Finally got time to try this out

If you loop back RX & TX, can you send and receive the same data? (Disconnect the MATE from the opto-isolator, connect RX & TX together, provide 24V, and open the serial port in a terminal program and try typing - do you see any dropped chars?)

I have looped the corresponding RX & TX cables on the ethernet cable that I'm using and provided 24V. By running the test script in debug mode I can see that any data that's send is correctly received back.

Send [Port0, Type=0x02, Addr=0x0000, Param=0x0000] TX: [1] 00 TX: [0] 02 00 00 00 00 00 02 RX: 00 02 00 00 00 00 00 02

I have also used the linux screen utility to further try with all sorts of characters and everything seems to be working correctly (I event sent a paragraph of lorem ipsum and got it back perfectly)

I think this removes the adapter not properly working as a possible cause, but I'm not 100% sure as I'm testing on my "lab" and not on the install location, there might be something there that's causing interferences that I'm not aware of.

If that's not the case, then the only other option I can think of is that my MATE unit has a different protocol or something along this line.