def pay(request, oid): order = OrderInfo.objects.get(oid=oid) order.oIsPay = True order.save() context = {'order': order} try: #连接PLC plc = c.Client() plc.connect('192.168.18.17', 0, 2) #获取V区域数据 writePLCstore(plc, '100', 1, S7WLBit, 1) #向V100.0,表示出库; position = StoreInfo.objects() #实例化库位模型对象 p = position.aggregate(Min('times')).filter(isPossess=1) #选择时间最短,有货的库位 #判断订单是金属还是塑料 if order.types() == 1: X = 1 elif order.types() == 2: X = 2 Y = P.slie Z = P.sceng writePLCstore(plc, '101', 0, S7WLByte, X) #分配出库排号; writePLCstore(plc, '102', 0, S7WLByte, Y) #分配出库列号; writePLCstore(plc, '103', 0, S7WLByte, Z) #分配出库层号; # 断开连接 client.disconnect() client.destroy() except: pass return render(request, 'df_order/pay.html', context)
def __init__(self, **kwargs): """ PLC object constructor :param kwargs: dict :raise plc_exception.DatabaseError """ super().__init__() self.__new = None self.__current = None self.__old_data = None self.__plc_name = kwargs['plc_name'] self.__plc_uid = kwargs['plc_uid'] self.__db_read = client.Client() if 'new' in kwargs: self.__new = Version_Model(_name='new', plc_name=kwargs['plc_name'], **kwargs['new']) else: raise plc_exception.DatabaseError("new does not exist in the database") self.__parameters = ( kwargs['plc_parameters']['Ip_Address'], kwargs['plc_parameters']['Rack'], kwargs['plc_parameters']['Slot'], kwargs['plc_parameters']['Port'] ) self.__plc_connection() self.__upload_new_data()
def process_finished(): plc = c.Client() plc.connect('172.21.2.1',0,1) WriteMemory(plc,99,0,S7WLBit,1) rospy.sleep(4.0) WriteMemory(plc,99,0,S7WLBit,0) plc.disconnect
def data_reader(): plc = s7.Client() try: result, data_items = plc.read_multi_vars(data_items) return result, data_items except Exception as e: print('warning:>>{}'.format(e))
def main(): while True: plc = c.Client() plc.connect('172.21.2.1',0,1) return "Yup" result = plc.read_area(0x81,0,1,1) return get_bool(result,0,4) rospy.sleep(1.0)
def s7connect(ip, rack, slot): try: plc = client.Client() # print(ip,rack,slot) plc.connect(ip, rack, slot) except Exception as e: flash("连接失败,请确认IP或网络连通性", "connect0") else: state = plc.get_cpu_state() flash(ip + " 连接成功", "connect1") return plc
def main(): parser = optparse.OptionParser('Use: ' + sys.argv[0] + ' -c country -t city -p page search\ -o output file') parser.add_option('-c', dest='coption', type='string', help='Country to locate PLCs') parser.add_option('-t', dest='toption', type='string', help='City to locate PLCs') parser.add_option('-o', dest='foption', type='string', help='File to return data') parser.add_option('-p', dest='poption', type='int', help='Page number of SHODAN search') (options, args) = parser.parse_args() if options.foption == None: print(parser.usage) exit(0) else: file_name = options.foption try: f_out = open(file_name, 'wb') except: print("Unable to save file: ", file_name) exit(0) if options.poption == None: page_s = 1 else: page_s = options.poption plcs = lookup(country=options.coption, city=options.toption, page=page_s) for plc_c in plcs.results: # For each PLC try: plc = s7.Client() plc.connect(plc_c.ip, 0, 0) state = plc.get_cpu_state() info = plc.get_cpu_info() name = info.ModuleName.decode('utf8') cpu = info.ModuleTypeName.decode('utf8') plc.disconnect() cadena = plc_c.ip + '||' + name + '||' + cpu + '||' + state + '\n' print(cadena) f_out.write(cadena.encode('utf8')) except: pass f_out.close()
def main(): plc_ip = "" plc_port = 102 with open('plc_ip') as plc_ip_file: plc_ip_read_data = plc_ip_file.read().split(":") plc_ip = plc_ip_read_data[0] plc_port = int(plc_ip_read_data[len(plc_ip_read_data) - 1]) logging.info("connecting to PLC: %s, port: %s", plc_ip, plc_port) plc = c.Client() plc.connect(plc_ip, 11, 11, plc_port) logging.info("plc connected: %s", plc.get_connected()) for x in range(10): WriteOutput(plc, '0.0', x % 2 == 0) # turns true every other iteration sleep(1)
def main(): s7400 = client.Client() conncet(s7400, '192.168.0.20', 0, 3) while True: try: # WQ(s7400, '1.3', 1) # sleep(2) for byte in range(2): for bit in range(8): byte_bit = str(byte) + '.' + str(bit) WQ(s7400, byte_bit, 1) sleep(1) for byte in range(2): for bit in range(8): byte_bit = str(byte) + '.' + str(bit) WQ(s7400, byte_bit, 0) sleep(1) except Snap7Exception as e: conncet(s7400, '192.168.0.20', 0, 3)
def plc_connect(ip, rack, slot, stop_tries, freq): plc = s7.Client() tries = 1 while tries < stop_tries and not plc.get_connected(): try: print('trying for connecting to PLC ...') sleep(freq) plc.connect(ip, rack, slot) return True except Exception as e: logger.error("warning in PLC connection >>{}".format(e)) sleep(freq) if tries == (stop_tries - 1): print('error in plc connection') return False tries += 1 return False
def main(): plc = c.Client() plc.connect('192.168.0.180', 0, 1) # ser = serial.Serial('COM4', 9600) while True: print " ____ ________ ___ _ __ " print " / __ )/ ____/ / |__ \ _ _____ (_)___ _/ /_ ___ _____" print " / __ / / / / __/ / | | /| / / _ \/ / __ `/ __ \/ _ \/ ___/" print " / /_/ / /___/ /___/ __/ | |/ |/ / __/ / /_/ / / / / __/ / " print "/_____/\____/_____/____/ |__/|__/\___/_/\__, /_/ /_/\___/_/ " print " /____/ " inv1 = ReadMemory(plc, 402, 0, S7WLReal) inv2 = ReadMemory(plc, 406, 0, S7WLReal) inv3 = ReadMemory(plc, 410, 0, S7WLReal) inv4 = ReadMemory(plc, 414, 0, S7WLReal) inv5 = ReadMemory(plc, 418, 0, S7WLReal) inv6 = ReadMemory(plc, 422, 0, S7WLReal) inv7 = ReadMemory(plc, 426, 0, S7WLReal)
def connectPLC(ipadress): """ Connects via OPC to plc. args: ipadress: ip adress of the plc's network card returns: plc: object with connection to plc status: Message with status of the connection to plc """ plc = c.Client() try: plc.connect(ipadress, 0, 1) except: return (plc, "Connection to PLC failed") else: if plc.get_connected(): return (plc, "Connected") else: return (plc, "Error: Unknown")
def __init__(self, ip, rack, slot, reconnect_period, collect_period, index): # 复位变量 self.buttonStatus = ButtonStatus() self.lampStatus = LampStatus() self.powerMeter = PowerMeter() self.sensor = Sensor() self.fanStatus = False # client self.ip = ip self.rack = rack self.slot = slot self.client = s7client.Client() self.reconnect_period = reconnect_period self.collect_period = collect_period self.index = index self.name = "iDesk" + str(self.index) self.isFinished = False self.lesson = 0 # mqtt interface self.interface = interface.iMQTT('IR829/' + str(self.index) + '/TX', self.name)
def serialfromplc(): array=[] plc = c.Client() plc.connect('192.168.0.7',0,1) dbnum=6 offset = 2 length = 16 for offset in range(0,21,2): value= plc.db_read(dbnum,offset,length) value=(ReadMemory(value,S7WLWord)) if value!=0: value=hex(value) value=value[2:] value1=value[2:] value2=value[:2] value=value1+value2 #print(value) data=bytes.fromhex(value).decode('utf-8') array.append(data) else: data=str(value) data=convert(array) data=str(data) return data
elif datatype == S7WLByte or datatype == S7WLWord: return get_int(result, 0) elif datatype == S7WLReal: return get_real(result, 0) elif datatype == S7WLDWord: return get_dword(result, 0) else: return None def WriteMemory(plc, byte, bit, datatype, value): result = plc.read_area(areas['PE'], 0, byte, datatype) if datatype == S7WLBit: set_bool(result, 0, bit, value) elif datatype == S7WLByte or datatype == S7WLWord: set_int(result, 0, value) elif datatype == S7WLReal: set_real(result, 0, value) elif datatype == S7WLDWord: set_dword(result, 0, value) plc.write_area(areas["PE"], 0, byte, result) if __name__ == "__main__": plc = c.Client() plc.connect('192.168.1.3', 0, 1) print("Input value before writing:", ReadMemory(plc, 0, 0, S7WLBit)) WriteMemory(plc, 0, 0, S7WLBit, 1) print("Input value after writing:", ReadMemory(plc, 0, 0, S7WLBit)) #DONE!!
import snap7.client as client from snap7.util import * def write_output(dev, byte, bit, cmd): data = dev.read_area(0x82, 0, byte, 1) set_bool(data, byte, bit, cmd) dev.write_area(0x82, 0, byte, data) s71200 = client.Client() s71200.connect('192.168.2.110', 0, 1) connectionIsOk = s71200.get_connected() print(connectionIsOk) #write_output(s71200, 0, 0, True)
def ReadOutput(plc,byte,bit,datatype = S7WLBit): result = plc.read_area(areas['PA'],0,byte,datatype) if datatype==S7WLBit: return get_bool(result,0,bit) elif datatype==S7WLByte or datatype==S7WLWord: return get_int(result,0) elif datatype==S7WLReal: return get_real(result,0) elif datatype==S7WLDWord: return get_dword(result,0) else: return None # Setup (If you want to modify retentiveTag defualt values you need to erase db file) sch6 = c.Client() ready = False try: sch6.connect('192.168.10.1', 0, 2) except: print("PLC not reachable") else: print("Connected") ready = True vPLC = plc.PLC("S7 300") counter1 = vPLC.createCounter("cont1", 10000) timer1 = vPLC.createTimer("timer1", 5) pulse = vPLC.Pulse() # Main
#print tag.get_valor() check_group_time(grupos) return True except Exception as e: print e return False #log_error("PLC Siemens falha de leitura na db:",db,cur) if __name__ == "__main__": tags = loadTags() dbs = loadDBs() mss = loadMeasur(tags) grupos = loadGrupos(mss) plc = client.Client() erro = 0 while True: if conection(ipSiemens): try: read_tags() time.sleep(1) erro = 0 except: erro += 1 else: erro += 1 if erro >= 1: restart_program() erro = 0
def ConnectToPLC(ip, board, place): plc = c.Client() plc.connect(ip, board, place) return plc
def main(): plc = c.Client() plc.connect('192.168.0.180', 0, 1) ser = serial.Serial('COM4', 9600) while True: print " ____ ________ ___ _ __ " print " / __ )/ ____/ / |__ \ _ _____ (_)___ _/ /_ ___ _____" print " / __ / / / / __/ / | | /| / / _ \/ / __ `/ __ \/ _ \/ ___/" print " / /_/ / /___/ /___/ __/ | |/ |/ / __/ / /_/ / / / / __/ / " print "/_____/\____/_____/____/ |__/|__/\___/_/\__, /_/ /_/\___/_/ " print " /____/ " inv1 = ReadMemory(plc, 402, 0, S7WLReal) inv2 = ReadMemory(plc, 406, 0, S7WLReal) inv3 = ReadMemory(plc, 410, 0, S7WLReal) inv4 = ReadMemory(plc, 414, 0, S7WLReal) inv5 = ReadMemory(plc, 418, 0, S7WLReal) inv6 = ReadMemory(plc, 422, 0, S7WLReal) inv7 = ReadMemory(plc, 426, 0, S7WLReal) print "" print "===================================================================" print "" print " Inverter 1", round(inv1, 1), "Hz" print " Inverter 2", round(inv2, 1), "Hz" print " Inverter 3", round(inv3, 1), "Hz" print " Inverter 4", round(inv4, 1), "Hz" print " Inverter 5", round(inv5, 1), "Hz" print " Inverter 6", round(inv6, 1), "Hz" print " Inverter 7", round(inv7, 1), "Hz" print "" print "" print "" print " kirim perintah :" print "" # inv1 = 5 # inv2 = 10 # inv3 = 15 # inv4 = 20 # inv5 = 25 # inv6 = 30 # inv7 = 35 v1 = round(inv1 / 10, 2) v2 = round(inv2 / 10, 2) v3 = round(inv3 / 10, 2) v4 = round(inv4 / 10, 2) v5 = round(inv5 / 10, 2) v6 = round(inv6 / 10, 2) v7 = round(inv7 / 10, 2) print " ", (v1, v2, v3, v4, v5, v6, v7) ser.write(str(v1)) ser.write(";") ser.write(str(v2)) ser.write(";") ser.write(str(v3)) ser.write(";") ser.write(str(v4)) ser.write(";") ser.write(str(v5)) ser.write(";") ser.write(str(v6)) ser.write(";") ser.write(str(v7)) ser.writelines("") time.sleep(1) ser.flushInput() system('cls')
def connect(): global plc_client plc_client = s7client.Client() plc_client.connect('10.0.0.3', 0, 1)
# also print something to console #print('\n' + thisIO.returnName() + " event logged") # reset the edge flag thisIO.resetEdgeFlag() # From falling edge set the edge flag if value1 == False or value2 == False: thisIO.setEdgeFlag() # MAIN LOOP if __name__ == "__main__": # Create client plc1 = c.Client() plc2 = c.Client() # read the tags and create objects readTags() # ONLINE: lets connect if True: # arguments are for rack 0, slot 2 plc1.connect(ipaddress1, 0, 2) plc2.connect(ipaddress2, 0, 2) # print all the IO objects to console just to make sure print "These " + str( IOObject.IOCount) + " memory addresses will be logged: " for x in names: print " " + str(names[x])
def belt_off(): plc = c.Client() plc.connect('172.21.2.1',0,1) result = plc.read_area(0x82,0,1,1) return get_bool(result,0,4)
def workpiece_avaiable(): plc = c.Client() plc.connect('172.21.2.1',0,1) result = plc.read_area(0x81,0,1,1) return get_bool(result,0,4)
def process_ongoing(): plc = c.Client() plc.connect('172.21.2.1',0,1) WriteMemory(plc,99,0,S7WLBit,0)
def main(): plc = c.Client() plc.connect('192.168.0.180', 0, 1) # ser = serial.Serial('COM4', 9600) if sys.argv[1] == "0": pass elif sys.argv[1] == "1": weight1 = ReadMemory(plc, 88, 0, S7WLReal) ratio1 = ReadMemory(plc, 216, 0, S7WLReal) sf1 = ReadMemory(plc, 244, 0, S7WLReal) af1 = ReadMemory(plc, 120, 0, S7WLReal) inv1 = ReadMemory(plc, 402, 0, S7WLReal) bs1 = ReadMemory(plc, 692, 0, S7WLReal) # print round(weight1,2) # print round(ratio1,2) # print round(sf1,2) # print round(af1,2) # print round(inv1,2) print round(weight1, 2), round(ratio1, 2), round(sf1, 2), round(af1, 2), round(bs1, 2), round(inv1, 2) pass elif sys.argv[1] == "2": weight2 = ReadMemory(plc, 92, 0, S7WLReal) ratio2 = ReadMemory(plc, 220, 0, S7WLReal) sf2 = ReadMemory(plc, 248, 0, S7WLReal) af2 = ReadMemory(plc, 124, 0, S7WLReal) inv2 = ReadMemory(plc, 406, 0, S7WLReal) bs2 = ReadMemory(plc, 696, 0, S7WLReal) # print round(weight2,2) # print round(ratio2,2) # print round(sf2,2) # print round(af2,2) # print round(bs2,2) # print round(inv2,2) print round(weight2, 2), round(ratio2, 2), round(sf2, 2), round(af2, 2), round(bs2, 2), round(inv2, 2) pass elif sys.argv[1] == "3": weight3 = ReadMemory(plc, 96, 0, S7WLReal) ratio3 = ReadMemory(plc, 224, 0, S7WLReal) sf3 = ReadMemory(plc, 252, 0, S7WLReal) af3 = ReadMemory(plc, 128, 0, S7WLReal) inv3 = ReadMemory(plc, 410, 0, S7WLReal) bs3 = ReadMemory(plc, 700, 0, S7WLReal) # print round(weight3,2) # print round(ratio3,2) # print round(sf3,2) # print round(af3,2) # print round(bs3,2) # print round(inv3,2) print round(weight3, 2), round(ratio3, 2), round(sf3, 2), round(af3, 2), round(bs3, 2), round(inv3, 2) pass elif sys.argv[1] == "4": weight4 = ReadMemory(plc, 100, 0, S7WLReal) ratio4 = ReadMemory(plc, 228, 0, S7WLReal) sf4 = ReadMemory(plc, 256, 0, S7WLReal) af4 = ReadMemory(plc, 132, 0, S7WLReal) inv4 = ReadMemory(plc, 414, 0, S7WLReal) bs4 = ReadMemory(plc, 704, 0, S7WLReal) # print round(weight4,2) # print round(ratio4,2) # print round(sf4,2) # print round(af4,2) # print round(bs4,2) # print round(inv4,2) print round(weight4, 2), round(ratio4, 2), round(sf4, 2), round(af4, 2), round(bs4, 2), round(inv4, 2) pass elif sys.argv[1] == "5": weight5 = ReadMemory(plc, 104, 0, S7WLReal) ratio5 = ReadMemory(plc, 232, 0, S7WLReal) sf5 = ReadMemory(plc, 260, 0, S7WLReal) af5 = ReadMemory(plc, 136, 0, S7WLReal) inv5 = ReadMemory(plc, 418, 0, S7WLReal) bs5 = ReadMemory(plc, 708, 0, S7WLReal) # print round(weight5,2) # print round(ratio5,2) # print round(sf5,2) # print round(af5,2) # print round(bs5,2) # print round(inv5,2) print round(weight5, 2), round(ratio5, 2), round(sf5, 2), round(af5, 2), round(bs5, 2), round(inv5, 2) pass elif sys.argv[1] == "6": weight6 = ReadMemory(plc, 108, 0, S7WLReal) ratio6 = ReadMemory(plc, 236, 0, S7WLReal) sf6 = ReadMemory(plc, 264, 0, S7WLReal) af6 = ReadMemory(plc, 140, 0, S7WLReal) inv6 = ReadMemory(plc, 422, 0, S7WLReal) bs6 = ReadMemory(plc, 712, 0, S7WLReal) # print round(weight6,2) # print round(ratio6,2) # print round(sf6,2) # print round(af6,2) # print round(bs6,2) # print round(inv6,2) print round(weight6, 2), round(ratio6, 2), round(sf6, 2), round(af6, 2), round(bs6, 2), round(inv6, 2) pass elif sys.argv[1] == "7": weight7 = ReadMemory(plc, 112, 0, S7WLReal) ratio7 = ReadMemory(plc, 240, 0, S7WLReal) sf7 = ReadMemory(plc, 268, 0, S7WLReal) af7 = ReadMemory(plc, 144, 0, S7WLReal) inv7 = ReadMemory(plc, 426, 0, S7WLReal) bs7 = ReadMemory(plc, 716, 0, S7WLReal) # print round(weight7,2) # print round(ratio7,2) # print round(sf7,2) # print round(af7,2) # print round(bs7,2) # print round(inv7,2) print round(weight7, 2), round(ratio7, 2), round(sf7, 2), round(af7, 2), round(bs7, 2), round(inv7, 2) pass elif sys.argv[1] == "8": weight8 = ReadMemory(plc, 116, 0, S7WLReal) pass