svalouch / python-rctclient

Python client for RCTs Serial Communication Protocol
https://rctclient.readthedocs.io
GNU General Public License v3.0
46 stars 27 forks source link

sporadically false data #26

Closed Pythonaire closed 9 months ago

Pythonaire commented 11 months ago

I take this excellent script to get the inverter data, controlled by a cron job each 5 minutes and write the data into a sql database for history. Sometime ( 3-5 per month) the data are false or corrupt. I modified the script slightly for my own use case. "if frame.completed()" starts directly the decoding and hand over the result to the variable "value". if frame.complete() is false, value is 0. For me it seems to be ok.

Did i make a mistake?

def get_rtc_values(rtc_value):
    try:
    # open the socket and connect to the remote device:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((RTCDEVICE, 8899))
        # query information about an object ID (here: battery.soc):
        #object_info = R.get_by_name('battery.soc')
        object_info = R.get_by_name(rtc_value)
        # construct a byte stream that will send a read command for the object ID we want, and send it
        send_frame = make_frame(command=Command.READ, id=object_info.object_id)
        sock.send(send_frame)
        # loop until we got the entire response frame
        frame = ReceiveFrame()
        while True:
            ready_read, _, _ = select.select([sock], [], [], 2.0)
            if sock in ready_read:
            # receive content of the input buffer
                buf = sock.recv(256)
                # if there is content, let the frame consume it
                if len(buf) > 0:
                    frame.consume(buf)
                    # if the frame is complete, we're done
                    if frame.complete():
                         # decode the frames payload    
                        value = decode_value(object_info.response_data_type, frame.data)
                        break
                else:
                 # the socket was closed by the device, exit
                    value=0
                    sys.exit(1)

    except Exception as e:
        logging.info("*** error to read from RTC DEVICE *** {0}".format(e))
        value = 0
    return value