Skip to content

Instantly share code, notes, and snippets.

@toxicantidote
Created April 25, 2022 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toxicantidote/8f0aaede5480977b8c05af37f04dd39a to your computer and use it in GitHub Desktop.
Save toxicantidote/8f0aaede5480977b8c05af37f04dd39a to your computer and use it in GitHub Desktop.
MPP solar PIP-1012-MSE interfacing
#!/usr/bin/python3
## which serial port the MPP Solar inverter is connected to
serial_port = '/dev/ttyS0'
###
import serial ## provided by 'pyserial' from pip
import crc16 ## provided by 'crc16' from pip
import sys ## inbuilt library
def send(ser, command) :
## convert the command to bytes
command_bytes = command.encode('utf-8')
## calculate the CRC16 checksum of the command
crc = crc16.crc16xmodem(command_bytes).to_bytes(2, 'big')
## add the CRC and a carrige-return to the command
command_send = command_bytes + crc + b'\r'
## send the command
print('Sending: ' + str(command_send))
ser.write(command_send)
## flush the serial buffer to make sure the command is sent
ser.flush()
## open the serial port as 2400-8-N-1
serial_connection = serial.Serial(serial_port, 2400, timeout = 1)
## send the command passed as the first command-line argument
send(serial_connection, sys.argv[1])
## get the response
response = serial_connection.readline()[:-1]
## get the response checksum
checksum_received = response[-2:]
## get the response data and convert it to a string
data = response[1:-2].decode('utf-8')
## calculate the response checksum
checksum_actual = crc16.crc16xmodem(response[:-2]).to_bytes(2, 'big')
## print the response if the checksum is ok
if checksum_received == checksum_actual:
print('Data: ' + str(data))
else:
print('ERROR: Checksum mismatch!')
@toxicantidote
Copy link
Author

For example, use the QPIGS command to get current inverter status.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment