Skip to content
gman8a edited this page Apr 14, 2017 · 1 revision

I am using pycomm with AB-Control Logix.

My question is: How can I do an "atomic" bit write using the write_tag() method in the user interface (UI) ?

I am presently doing this in 2 steps; see following code:

  elif cmd == "BWRITE": # Bit write request
        #
        # bit write, requires read-modify-write; 
        #
        # example:   bWrite, 2, tag_4, bitNo, 0/1; note, here 2 is the adapter number
        #
        err, tag_resp = self.adaptor.read_tag( [tag] ) # as an array to pickup data type

        if err == 0:
            bitNo = value_cnt  # pickup the bitNo
            mask = 1 << bitNo  # make bit mask

            # pickup data type
            r = tag_resp[0] # (tag name, value, value_type)
            tag_name = r[0] # tag name
            tag_value= r[1] # value
            tag_type = r[2] # value_type

            if   tag_type == 'SINT': mask &= 0xff       # 8 bit
            elif tag_type == 'INT':  mask &= 0xffff     # 16 bit
            elif tag_type == 'DINT': mask &= 0xffffffff # 32 bit
            elif tag_type == 'BOOL': mask &= 0x01       # a BOOL is always bit 0 
            else:
                err = 11 # error: can not determine the Word length

            if not err:
                if tag_type == 'BOOL': # 1 bit
                    # can use the regular write_tag for BOOLeans
                    # just ignore the bit mask as it does not apply to BOOL writes
                    # here value_to_write = 0 or 1; also >1 is ok for boolean
                    self.adaptor.write_tag(tag_name, value_to_write, tag_type)

                else:
                    new_value_to_write = int(tag_value)  # pickup present value
                    new_value_to_write &= ~mask          # clear bit
                    if value_to_write: new_value_to_write |= mask # set if required
                    self.adaptor.write_tag( tag_name, new_value_to_write, tag_type)
        else:
            pass # no action taken
Clone this wiki locally