klaasdc / apcups-serial-test

Test application to communicate with an APC UPS over RS232 with the Microlink protocol
https://sites.google.com/site/klaasdc/apc-smartups-decode
GNU General Public License v2.0
18 stars 1 forks source link

'Set' commands #1

Open tfhomenet opened 2 years ago

tfhomenet commented 2 years ago

First of all, thank you for your amazing work, this really helps me. I've got a bunch of smc1500-2u's in remote locations, where it's impossible to use usb communications. So my solution is to use Moxa IA5150 and IA5450AI's for UPS monitoring and control. First i've checked communications with powershute, seemd ok. And, this unit was produced 9-2014. However, 'ups_status' string was strange, with nearly all possible statuses. 'Set' commands wasn't working - just nothing. So my next step was to update ups firmware, this is what i ended up with:

fw_version_1 = UPS 12.0
fw_version_2 = MCU 06.0
fw_version_3 = UBL 01.4
fw_version_4 = MBL 06.0

Now statuses and such are ok, but SET's still silent. The problem seems to be with thread and cli not synced - so when i set next_apc_msg, before it's executed, it's replaced with APC_CMD_NEXT, so nothing happens. I have to mention my test rig is relatively beefy, maybe this is the problem. So my hack is following:

  1. introduce new msg_q = set() variable
    def __init__(self, serial_port,mode='serial'):
        super(ApcComm, self).__init__()
        self.s = serial_port
        self.state = CommState.INIT
        self.prev_state = CommState.INIT
        self.next_apc_msg = APC_CMD_NEXT#Next message to be sent to the UPS, for internal use
        self.msg_q = set()
        self.running = True
        self.daemon = True
        self.ups_state = {"comm_state": "offline"}
        self.mode = mode
  2. sleep while it's set
    def send_apc_msg(self, raw_msg):
        '''
        Schedule the next message to be sent.
        Blocks until the message is succesfully sent.
        '''
        if self.state is not CommState.MODE1:
            return False
        self.next_apc_msg = raw_msg
        self.msg_q.add(raw_msg.hex())
        while self.msg_q:
            time.sleep(APC_RCV_TIMEOUT)
        return True
  3. Write pending cli commands on next run, and unset q

            elif self.state == CommState.MODE1:
                '''
                Normal communication flow with UPS according to MODE1
                '''
                self.next_apc_msg = bytearray.fromhex(self.msg_q.pop()) if self.msg_q else self.next_apc_msg
                # if self.mode == 'serial':
                self.s.write(self.next_apc_msg)
                # else:
                #     pl = self.next_apc_msg
                #     if not isinstance(pl,bytes):
                #         pl=bytearray(pl)
                #     self.s.send(pl)
                rcv_data = self.receive_msg()
    
                if not self.msg_q:
                    self.next_apc_msg = APC_CMD_NEXT

    Hope this helps. p.s One more thing, in my case i need to change battery replacedates, so here's the code:

    def convert_from_datetime(self,value):
        ''' Convert days from 1 Jan. 2000 to bytearray'''
        return (datetime.datetime(*value) - datetime.datetime(2000,1,1)).days.to_bytes(length=2, byteorder='big', signed=False)
    ...
        elif args[0] == 'battery_replace_date':
                try:
                    if len(args) == 3:
                        args = [int(args[idx]) if idx in (1, 2) else i for idx,i in enumerate(args)]
                        if 1 <= args[1] <= 12 and 2000 <= args[2] <= 9999:
                            data = apccomm.convert_from_datetime([args[2], args[1], 1])
                        else:
                            raise
                    else:
                        raise
                except:
                    print("Bad data")
                    return
                else:
                    raw_msg = apccomm.create_msg_data(msg_id=0x47, offset=0, msg_data=data)
                    self.send_msg(raw_msg)
klaasdc commented 2 years ago

Hi tfhomenet

Just now noticed your message/issue... oops

Thanks for your additions! I only used this code by hand with the CLI, and is not very developed. Hopefully you got it to work the way you wanted :)