コード例 #1
0
def write_value(data_type, result, data, byte_index=0, bool_index=0):
    # 'FLOAT'
    if data_type == 1:
        set_real(result, byte_index, data)

    # 'INT'
    elif data_type == 2:
        set_int(result, byte_index, data)

    # 'DINT'
    elif data_type == 3:
        set_dint(result, byte_index, data)

    # 'WORD'
    elif data_type == 4:
        set_word(result, byte_index, data)

    # 'DWORD'
    elif data_type == 5:
        set_dword(result, byte_index, data)

    # 'BYTE'
    elif data_type == 6:
        set_byte(result, byte_index, data)

    # 'BOOL'
    elif data_type == 7:
        set_bool(result, byte_index, bool_index, data)
コード例 #2
0
ファイル: test_plc.py プロジェクト: palmarytech/pythonPLC
 def test_db_write_real(self):
     byte_data = self.client.db_read(11, 22, 4)
     data = get_real(byte_data, 0)
     print(data)
     set_real(byte_data, 0, 33)
     self.client.db_write(11, 22, byte_data)
     byte_data = self.client.db_read(11, 22, 4)
     data = get_real(byte_data, 0)
     print(data)
コード例 #3
0
    def write(self,datatype,*args):
        self.pc2plc_data=self.plc.db_read(self.db_number,self.db_write_start,self.db_write_size)
        if datatype=='bool':
            util.set_bool(self.pc2plc_data,args[0],args[1],args[2])
        elif datatype=='int':
            util.set_int(self.pc2plc_data,args[0],args[1])
        elif datatype==('dint' or 'dword'):
            util.set_dword(self.pc2plc_data,args[0],args[1])
        elif datatype=='real':
            util.set_real(self.pc2plc_data,args[0],args[1])
        elif datatype=='string':
            util.set_string(self.pc2plc_data,args[0],args[1],args[2])

        self.plc.db_write(self.db_number,self.db_write_start,self.pc2plc_data)
コード例 #4
0
def write_value(data_type, result, data, byte_index=0, bool_index=0):
    if data_type == 'FLOAT':
        set_real(result, byte_index, data)

    elif data_type == 'INT':
        set_int(result, byte_index, data)

    elif data_type == 'DINT':
        set_dint(result, byte_index, data)

    elif data_type == 'WORD':
        set_word(result, byte_index, data)

    elif data_type == 'BYTE':
        set_byte(result, byte_index, data)

    elif data_type == 'BOOL':
        set_bool(result, byte_index, bool_index, data)

    elif data_type == 'DWORD':
        set_dword(result, byte_index, data)
コード例 #5
0
 def writeFloat(self, area, dbnumber, start, value):
     try:
         #### lock shared resources #####
         self.COMsemaphore.acquire(1)
         self.connect()
         if self.plc is not None and self.plc.get_connected():
             with suppress_stdout_stderr():
                 ba = self.plc.read_area(self.areas[area], dbnumber, start,
                                         4)
                 from snap7.util import set_real
                 set_real(ba, 0, float(value))
                 self.plc.write_area(self.areas[area], dbnumber, start, ba)
         else:
             self.adderror(
                 (QApplication.translate("Error Message", "S7 Error:", None)
                  + " connecting to PLC failed"))
     except Exception as ex:
         self.adderror(
             QApplication.translate("Error Message",
                                    "S7 Communication Error", None))
     finally:
         if self.COMsemaphore.available() < 1:
             self.COMsemaphore.release(1)
コード例 #6
0
def create_bytearray(size, lst):
    """
        Create bytearray for plc
        :param size: int
        :param lst: list
        :return holder: bytearray
        :raises
            TypeError
            plc_exception.DatabaseError
        """
    if not isinstance(lst, list):
        raise TypeError("create_bytearray takes list.")

    lst = sorted(lst, key=lambda x: x['Offset'])
    holder = bytearray([0 for _ in range(size)])

    for foo in lst:
        _offset = foo["Offset"]
        _value = foo["Value"]
        if foo['Data_type'] == 'Bool':
            byte, bit = str(float(_offset)).split('.')
            byte, bit = int(byte), int(bit)

            set_bool(holder, byte, bit, _value)

        elif foo['Data_type'] == 'String':
            set_string(holder, int(_offset), _value, 256)

        elif foo['Data_type'] == 'Int':
            set_int(holder, int(_offset), _value)

        elif foo['Data_type'] == 'Real':
            set_real(holder, int(_offset), _value)
        else:
            raise plc_exception.DatabaseError("Data is not correct.")

    return holder
コード例 #7
0
    item.DBNumber = ctypes.c_int32(db_number)
    item.Start = ctypes.c_int32(start)
    item.Amount = ctypes.c_int32(amount)
    array_class = ctypes.c_uint8 * len(data)
    cdata = array_class.from_buffer_copy(data)
    item.pData = ctypes.cast(cdata, ctypes.POINTER(array_class)).contents
    return item


int_values = [10, 20, 30, 40]
ints = bytearray(len(int_values) * 2)
for i, value in enumerate(int_values):
    set_int(ints, i * 2, value)

real = bytearray(4)
set_real(real, 0, 42.5)

counters = 0x2999.to_bytes(2, 'big') + 0x1111.to_bytes(2, 'big')

item1 = set_data_item(area=Areas.DB,
                      word_len=S7WLWord,
                      db_number=1,
                      start=0,
                      amount=4,
                      data=ints)
item2 = set_data_item(area=Areas.DB,
                      word_len=S7WLReal,
                      db_number=1,
                      start=8,
                      amount=1,
                      data=real)
コード例 #8
0
server = snap7.server.Server()
size = SIZE

globaldata = (wordlen_to_ctypes[S7WLByte]*size)()
outputs = (wordlen_to_ctypes[S7WLByte]*size)()
inputs = (wordlen_to_ctypes[S7WLByte]*size)()
dbs = (wordlen_to_ctypes[S7WLByte]*size)()

server.register_area(srvAreaPA, 0, outputs)
server.register_area(srvAreaMK, 0, globaldata)
server.register_area(srvAreaPE, 0, inputs)
server.register_area(srvAreaDB, 0, dbs)

server.start(tcpport=PORT)

util.set_real(outputs, 0, 1.234)      # srvAreaPA
util.set_real(globaldata, 0, 2.234)   # srvAreaMK
util.set_real(inputs, 0, 3.234)       # srvAreaPE
util.set_real(dbs, 0, 31.1)
util.set_bool(dbs, 5, 0, True)
util.set_bool(dbs, 5, 1, True)
util.set_int(dbs, 6, 12323)
while True:
    while True:
        event = server.pick_event()
        if event:
            print (server.event_text(event))
        else:
            break
        time.sleep(.01)
server.stop()