Exemplo n.º 1
0
 def read_all_tags(self):
     try:
         c = ModbusClient(host="192.168.2.11", port=502, debug=False)
         c.open()
         
         for name, tag in self.tag_db.items():
             mb0 = tag['modbus_start'] -1
             mb1 = tag['modbus_stop'] -1
             size = 1+mb1-mb0
             #print(name, mb0, mb1, size)
             #print(tag)
             if 0 <= mb0 < 100000:
                 val = c.read_coils(mb0)[0]
             elif 100000 <= mb0 < 200000:
                 val = c.read_discrete_inputs(mb0-100000)[0]
             elif 300000 <= mb0 < 400000:
                 val = c.read_input_registers(mb0-300000,  size)
                 if size == 1: val = val[0]
                 elif size == 2:
                     val = utils.word_list_to_long(val, big_endian=False)[0]
             elif 400000 <= mb0 < 500000:
                 val = c.read_holding_registers(mb0-400000,  size )
                 if size == 1: val = val[0]
                 elif size == 2:
                     val = utils.word_list_to_long(val, big_endian=False)[0]
             
             if tag['dtype'] == 'float32':
                 val = utils.decode_ieee(val)
             
             #print(name, val)
             self.settings[name] = val
                 
     except Exception as err:
         print("Error in read_all_tags", err)
         c.close()        
Exemplo n.º 2
0
 def read_float(self, address, number=1):
     reg_l = self.read_holding_registers(address, number * 2)
     if reg_l:
         return [
             utils.decode_ieee(f) for f in utils.word_list_to_long(reg_l)
         ]
     else:
         reg_l = [16000, 16000]
         return [
             utils.decode_ieee(f) for f in utils.word_list_to_long(reg_l)
         ]
def polling_thread():
    global ts, tm_1, vol_c_1
    # init modbus client
    c = FloatModbusClient(host=SERVER_HOST,
                          port=SERVER_PORT,
                          unit_id=0xFF,
                          auto_open=True)
    # polling loop
    while True:
        # read ts
        try:
            _ts = c.read_holding_registers(28)[0]
            with th_lock:
                ts = _ts
        except Exception as e:
            traceback.print_exc(file=sys.stderr)
        # read tm 1
        try:
            _tm_1 = c.read_float(512)[0]
            with th_lock:
                tm_1 = _tm_1
        except Exception as e:
            traceback.print_exc(file=sys.stderr)
        # read gas corrected volume (gas volume converter 1)
        try:
            _vol_c_1 = utils.word_list_to_long(
                c.read_holding_registers(2634, 2))[0]
            with th_lock:
                vol_c_1 = _vol_c_1
        except Exception as e:
            traceback.print_exc(file=sys.stderr)
        # 1s before next polling
        time.sleep(1.0)
Exemplo n.º 4
0
 def read_float(self, address, number=1):
     reg_1 = self.read_holding_registers(address, number * 2)
     if reg_1:
         print(reg_1)
         return [utils.decode_ieee(f) for f in utils.word_list_to_long(reg_1)]
     else:
         return None
Exemplo n.º 5
0
 def long_to_float(self,list_16):
     list_float=[]
     list_16.reverse()
     list_long=utils.word_list_to_long(list_16)
     for any_long in list_long:
         list_float.append(utils.decode_ieee(any_long))
     list_float.reverse()
     return list_float
Exemplo n.º 6
0
 def get_floats(cls, address, number=1):
     reg_l = cls.get_words(address, number * 2)
     if reg_l:
         return [
             utils.decode_ieee(f) for f in utils.word_list_to_long(reg_l)
         ]
     else:
         return None
Exemplo n.º 7
0
def make_summary():
    SERVER_HOST = "192.168.43.239"
    SERVER_PORT = 502
    SERVER_UNIT_ID = 2

    c = ModbusClient()
    c.host(SERVER_HOST)
    c.port(SERVER_PORT)
    c.unit_id(SERVER_UNIT_ID)
    if not c.is_open():
            if not c.open():
                print("cannot connect ....")

    if c.is_open():
    # read 54 registers at address 0, store result in regs list
            regs = c.read_input_registers(0,54)
    # if success change register value to float
            if regs:
                abc = [utils.decode_ieee(f) for f in utils.word_list_to_long(regs)]
                data = {
                "Power KWH" : "%0.3f"%abc[0],
                "Power KVAH" : "%0.3f"%abc[1],
                "Power KVArP" : "%0.3f"%abc[2],
                "Power KVArN" : "%0.3f"%abc[3],
                "Line Voltages V RY" : "%0.3f"%abc[4],
                "Line Voltages V YB" : "%0.3f"%abc[5],
                "Line Voltages V BR" : "%0.3f"%abc[6],
                "Line Current IR" : "%0.3f"%abc[7],
                "Line Current IY" : "%0.3f"%abc[8],
                "Line Current IB" : "%0.3f"%abc[9],
                "Active Power Consumed" : "%0.3f"%abc[10],
                "Reactive Power Consumed" : "%0.3f"%abc[11],
                "Apparent Power Consumed" : "%0.3f"%abc[12],
                "Phase Voltages VRN" : "%0.3f"%abc[13],
                "Phase Voltages VYN" : "%0.3f"%abc[14],
                "Phase Voltages VBN" : "%0.3f"%abc[15],
                "Power Factor" : "%0.3f"%abc[16],
                "Frequency" : "%0.3f"%abc[17],
                "Real Power on R" : "%0.3f"%abc[18],
                "Real Power on Y" : "%0.3f"%abc[19],
                "Real Power on B" : "%0.3f"%abc[20],
                "Reactive Power on R" : "%0.3f"%abc[21],
                "Reactive Power on Y" : "%0.3f"%abc[22],
                "Reactive Power on B" : "%0.3f"%abc[23],
                "Apparent Power on R" : "%0.3f"%abc[24],
                "Apparent Power on Y" : "%0.3f"%abc[25],
                "Apparent Power on B" : "%0.3f"%abc[26] 
                }
                mydate = datetime.datetime.now()
                date=datetime.datetime.strftime(mydate,'%Y/%m/%d--%H:%M:%S')
                abc.insert(27,date)
                myfile = open('data.csv','a')
                with myfile:
                    writer = csv.writer(myfile, delimiter=',', quoting=csv.QUOTE_ALL)
                    writer.writerow(abc)
                return data
Exemplo n.º 8
0
 def __read_32_bit(self, address, number=1):
     if not self.c.is_open():
         self.c.open()
     reg_l = self.c.read_holding_registers(address, number * 2)
     if reg_l:
         return [
             utils.decode_ieee(f) for f in utils.word_list_to_long(reg_l)
         ][0]
     else:
         return None
Exemplo n.º 9
0
def decimal_list_to_float_bigEndian(values):
    p1 = decoder.word_list_to_long(values, big_endian=True)
    l1 = list()
    for register in p1:
        tmp = decoder.decode_ieee(register)
        if str(tmp) != 'nan':
            tmp = round(float(tmp), 4)
            l1.append(tmp)
        else:
            l1.append(None)
    return l1
Exemplo n.º 10
0
 def test_word_list_to_long(self):
     # empty list, return empty list
     self.assertEqual(utils.word_list_to_long([]), [])
     # if len of list is odd ignore last value
     self.assertEqual(utils.word_list_to_long([0x1, 0x2, 0x3]), [0x10002])
     # test convert with big and little endian
     long_list = utils.word_list_to_long([0xdead, 0xbeef])
     self.assertEqual(long_list, [0xdeadbeef])
     long_list = utils.word_list_to_long([0xdead, 0xbeef, 0xdead, 0xbeef])
     self.assertEqual(long_list, [0xdeadbeef, 0xdeadbeef])
     long_list = utils.word_list_to_long([0xdead, 0xbeef], big_endian=False)
     self.assertEqual(long_list, [0xbeefdead])
     long_list = utils.word_list_to_long([0xdead, 0xbeef, 0xdead, 0xbeef], big_endian=False)
     self.assertEqual(long_list, [0xbeefdead, 0xbeefdead])
Exemplo n.º 11
0
 def test_word_list_to_long(self):
     # empty list, return empty list
     self.assertEqual(utils.word_list_to_long([]), [])
     # if len of list is odd ignore last value
     self.assertEqual(utils.word_list_to_long([0x1,0x2,0x3]), [0x10002])
     # test convert with big and little endian
     word_list = utils.word_list_to_long([0xdead, 0xbeef])
     self.assertEqual(word_list, [0xdeadbeef])
     word_list = utils.word_list_to_long([0xdead, 0xbeef, 0xdead, 0xbeef])
     self.assertEqual(word_list, [0xdeadbeef, 0xdeadbeef])
     word_list = utils.word_list_to_long([0xdead, 0xbeef], big_endian=False)
     self.assertEqual(word_list, [0xbeefdead])
     word_list = utils.word_list_to_long([0xdead, 0xbeef, 0xdead, 0xbeef],
                                         big_endian=False)
     self.assertEqual(word_list, [0xbeefdead, 0xbeefdead])
Exemplo n.º 12
0
    if not c.is_open():
        if not c.open():
            print("unable to connect to " + SERVER_HOST + ":" +
                  str(SERVER_PORT))

    # if open() is ok, read register (modbus function 0x03)
    if c.is_open():

        mydate = datetime.datetime.now()

        print(str(mydate))
        # read 54 registers at address 0, store result in regs list
        regs = c.read_input_registers(0, 54)
        # if success change register value to float
        if regs:
            abc = [utils.decode_ieee(f) for f in utils.word_list_to_long(regs)]
        csvwrite = abc
        l = len(csvwrite)
        #truncate the float value upto 3 decimal places

        #Display real time values on Screen
# 	print(abc)

# Time and Date Formatting
#csvstr = datetime.datetime.strftime(mydate, '%Y/%m/%d -- %H:%M:%S')
#abc[0]=csvstr
#print(abc)
#Send data to a excel file
#myFile = open('csvexample4.csv', 'a')
#with myFile:
#   writer = csv.writer(myFile,delimiter=',',quoting=csv.QUOTE_ALL)
Exemplo n.º 13
0
    c = ModbusClient(host="localhost", port=502)
except ValueError:
    print("Error with host or port params")

c = ModbusClient(host="localhost", auto_open=True)

if c.open():
    regs_list_1 = c.read_holding_registers(0, 10)
    regs_list_2 = c.read_holding_registers(55, 10)
    c.close()

while True:
    if c.is_open():
        regs_list_1 = c.read_holding_registers(0, 10)
        regs_list_2 = c.read_holding_registers(55, 10)
    else:
        c.open()
    time.sleep(1)

list_16_bits = [0x0123, 0x4567, 0x89ab, 0xcdef]

# big endian sample (default)
list_32_bits = utils.word_list_to_long(list_16_bits)
# display "['0x1234567', '0x89abcdef']"
print([hex(i) for i in list_32_bits])

# little endian sample
list_32_bits = utils.word_list_to_long(list_16_bits, big_endian=False)
# display "['0x45670123', '0xcdef89ab']"
print([hex(i) for i in list_32_bits])
Exemplo n.º 14
0
 def read_float(self, address, number=1):
     reg_l = self.read_holding_registers(address, number * 2)
     if reg_l:
         return [utils.decode_ieee(f) for f in utils.word_list_to_long(reg_l)]
     else:
         return None
# Reference: http://pythonhosted.org/pyModbusTCP/quickstart/index.html#utils-module-modbus-data-mangling
from pyModbusTCP.client import ModbusClient
from pyModbusTCP import utils

c = ModbusClient(host="192.168.1.35", port=502, unit_id=30, auto_open=True)
c.open()
if not c.is_open():
	raise Exception("could not connect")

list_16_bits = c.read_input_registers(211, 2)
list_32_bits = utils.word_list_to_long(list_16_bits)


c.close()
Exemplo n.º 16
0
         for regss in regs:
             regs = U8_conversion(float(regss),DIVIDENT)
     elif DTYPE == 'S16':
         for regss in regs:
             regs = S16_conversion(float(regss),DIVIDENT)
     elif DTYPE == 'STRING_NORM':
         for regss in regs:
             regs = STRING_NORM_conversion(float(regss),DIVIDENT)
     elif DTYPE == 'S32':
         if type(regs) == list:
             for regss in regs:
                 regs = S32_conversion(float(regss),DIVIDENT)
         else :
             regs = str(regs)
     elif DTYPE == 'U32':
         regs = utils.word_list_to_long(regs)
         for register in regs:
             regs = U32_conversion(float(register),DIVIDENT)
     elif DTYPE == 'U16':
         for register in regs:
             regs = U16_conversion(float(register), DIVIDENT)
         print(regs)
     else :
         regs = utils.word_list_to_long(regs)
         for register in regs:
             regs = register
     
     push_device_data(d, {ADDRESS:regs})
 except:
     push_device_data(d, {ADDRESS: "None Type"})
     print("error")
Exemplo n.º 17
0
 def read_float_inp(self, address, number=1):
     reg_l = self.read_input_registers(address, number * 2)
     if reg_l:
         return [utils.decode_ieee(f) for f in utils.word_list_to_long(reg_l)]
Exemplo n.º 18
0
import paho.mqtt.client as mqtt
import time

c = ModbusClient(host="192.168.1.222", port=502, auto_open=True)

client = mqtt.Client()
client.connect("127.0.0.1")
client.loop_start()

outputs = c.read_holding_registers(
    28680,
    4)  #PLC says address is 428681 so, 400001 must be subtracted from address
print outputs
if outputs:
    outputs = [
        utils.decode_ieee(f) for f in utils.word_list_to_long(outputs, False)
    ]
print outputs

while True:
    analogInputs = c.read_holding_registers(28672, 8)
    #print analogInputs
    if analogInputs:
        analogInputs = [
            utils.decode_ieee(f)
            for f in utils.word_list_to_long(analogInputs, False)
        ]
    i = 0

    scaleAI = [  #scaling of analog inputs from 4 - 20 mA to engineering units
        {