uri = "http://examples.freeopcua.github.io" idx = server.register_namespace(uri) # get Objects node, this is where we should put our custom stuff objects = server.get_objects_node() # Example 1 - create a basic object #------------------------------------------------------------------------------- myobj = objects.add_object(idx, "MyObject") #------------------------------------------------------------------------------- # Example 2 - create a new object type and a instance of the new object type #------------------------------------------------------------------------------- types = server.get_node(ua.ObjectIds.BaseObjectType) object_type_to_derive_from = server.get_root_node().get_child( ["0:Types", "0:ObjectTypes", "0:BaseObjectType"]) mycustomobj_type = types.add_object_type(idx, "MyCustomObjectType") mycustomobj_type.add_variable(0, "var_should_be_there_after_instantiate", 1.0) # demonstrates instantiate myobj = objects.add_object(idx, "MyCustomObjectA", mycustomobj_type.nodeid) #------------------------------------------------------------------------------- # Example 3 - import a new object from xml address space and create a instance of the new object type #------------------------------------------------------------------------------- # Import customobject type server.import_xml('customobject.xml') # get nodeid of custom object type by one of the following 3 ways: # 1) Use node ID # 2) Or Full path
async def main(): # setup our server print('【正在初始化OPC UA Server...】') server = Server() await server.init() server.default_timeout = 60 server.set_endpoint('opc.tcp://0.0.0.0:4840/') # server.set_endpoint('opc.tcp://192.168.100.170:4840/') # setup our own namespace, not really necessary but should as spec try: ################################# # # 设置加密和密钥后 prosys可以连接 None方式不行 await server.load_certificate("certificate.der") await server.load_private_key("private-key.pem") # # # set all possible endpoint policies for clients to connect through server.set_security_policy([ ua.SecurityPolicyType.NoSecurity, ua.SecurityPolicyType.Basic256Sha256_SignAndEncrypt, ua.SecurityPolicyType.Basic256Sha256_Sign ]) ######################################## except Exception as e: print(e) else: uri = 'http://su600.cn' idx = await server.register_namespace(uri) # 导入xml文件 await server.import_xml('kuka.xml') # 获取xml中的UA node # todo 待优化 root = server.get_root_node() actuator1 = await root.get_child( ["0:Objects", "0:Actuator", '0:actuator1']) actuator2 = await root.get_child( ["0:Objects", "0:Actuator", '0:actuator2']) actuator3 = await root.get_child( ["0:Objects", "0:Actuator", '0:actuator3']) actuator4 = await root.get_child( ["0:Objects", "0:Actuator", '0:actuator4']) degree1 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Degree', '0:degree1']) degree2 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Degree', '0:degree2']) degree3 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Degree', '0:degree3']) degree4 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Degree', '0:degree4']) degree5 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Degree', '0:degree5']) degree6 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Degree', '0:degree6']) degree7 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Degree_R', '0:degree7']) degree8 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Degree_R', '0:degree8']) degree9 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Degree_R', '0:degree9']) degree10 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Degree_R', '0:degree10']) degree11 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Degree_R', '0:degree11']) degree12 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Degree_R', '0:degree12']) x1 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Joint', '0:x1']) x2 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Joint_R', '0:x2']) y1 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Joint', '0:y1']) y2 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Joint_R', '0:y2']) z1 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Joint', '0:z1']) z2 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Joint_R', '0:z2']) a1 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Joint', '0:a1']) a2 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Joint_R', '0:a2']) b1 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Joint', '0:b1']) b2 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Joint_R', '0:b2']) c1 = await root.get_child( ["0:Objects", "0:Drilling_Robot", '0:Joint', '0:c1']) c2 = await root.get_child( ["0:Objects", "0:Riveting_Robot", '0:Joint_R', '0:c2']) normal1 = await root.get_child(["0:Objects", "0:Normals", '0:normal1']) normal2 = await root.get_child(["0:Objects", "0:Normals", '0:normal2']) normal3 = await root.get_child(["0:Objects", "0:Normals", '0:normal3']) data_name = [ x1, y1, z1, a1, b1, c1, degree1, degree2, degree3, degree4, degree5, degree6, x2, y2, z2, a2, b2, c2, degree7, degree8, degree9, degree10, degree11, degree12, actuator1, actuator2, actuator3, actuator4, normal1, normal2, normal3 ] # todo 设置可写 for i in data_name: await i.set_writable() print('\n') print('【OPC UA Server初始化完成】') # todo 错误处理 自动重连 print('***请确保机器人控制程序已启动,然后输入IP地址***:') ip = input() # print('请输入端口号(默认6008):') # try: # port = int(input()) # except Exception as e: # print(e, '端口号格式错误') # print('请重新输入端口号') # port = int(input()) port = 6008 print('请输入采集周期(单位ms):') try: cycle = int(input()) / 1000 except Exception as e: print(e, '周期输入有误 请重新输入') # print('请重新输入端口号') cycle = int(input()) / 1000 # socket通信 def socket_client(ip, port): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, port)) except socket.error as e: print(e) # sys.exit(1) else: # send_data = input('请输入发送命令: ').strip() send_data = '0' s.send(send_data.encode()) receive_data = s.recv(1024).decode() # kuka_data=data_analysis(send_data,receive_data) # print("收到信息", receive_data) # s.close() return receive_data # OPC UA 数据更新 async with server: # count = 0 print('【OPC UA Server启动完成开始转发数据】') while True: # cycle=0.05 await asyncio.sleep(cycle) # 数据更新周期 data = socket_client(ip, port) n = 0 for i in data_name: await i.write_value(data.split(',')[n]) n += 1
async def main(): # setup our server print('***请确保机器人控制程序已启动,输入任意键继续 ***:') aaa = input() print('【正在初始化OPC UA Server...】') server = Server() await server.init() server.default_timeout = 60 endpoint = 'opc.tcp://0.0.0.0:4840/' server.set_endpoint(endpoint) # server.set_endpoint('opc.tcp://192.168.100.173:4840/') # setup our own namespace, not really necessary but should as spec try: ################################# # # 设置加密和密钥后 prosys可以连接 None方式不行 await server.load_certificate("certificate.der") await server.load_private_key("private-key.pem") ## set all possible endpoint policies for clients to connect through server.set_security_policy([ ua.SecurityPolicyType.NoSecurity, ua.SecurityPolicyType.Basic256Sha256_SignAndEncrypt, ua.SecurityPolicyType.Basic256Sha256_Sign ]) ######################################## except Exception as e: print(e) else: uri = 'http://su600.cn' idx = await server.register_namespace(uri) # 导入xml文件 await server.import_xml('kuka.xml') # 获取xml中的UA node # todo 待优化 root = server.get_root_node() x1 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔TCP坐标', '3:x_Drilling']) y1 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔TCP坐标', '3:y_Drilling']) z1 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔TCP坐标', '3:z_Drilling']) a1 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔TCP坐标', '3:a_Drilling']) b1 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔TCP坐标', '3:b_Drilling']) c1 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔TCP坐标', '3:c_Drilling']) A1 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔关节角度', '3:A1_Drilling']) A2 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔关节角度', '3:A2_Drilling']) A3 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔关节角度', '3:A3_Drilling']) A4 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔关节角度', '3:A4_Drilling']) A5 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔关节角度', '3:A5_Drilling']) A6 = await root.get_child( ["0:Objects", "3:制孔机器人", '3:制孔关节角度', '3:A6_Drilling']) x2 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接TCP坐标', '3:x_Riveting']) y2 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接TCP坐标', '3:y_Riveting']) z2 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接TCP坐标', '3:z_Riveting']) a2 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接TCP坐标', '3:a_Riveting']) b2 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接TCP坐标', '3:b_Riveting']) c2 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接TCP坐标', '3:c_Riveting']) A7 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接关节角度', '3:A1_Riveting']) A8 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接关节角度', '3:A2_Riveting']) A9 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接关节角度', '3:A3_Riveting']) A10 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接关节角度', '3:A4_Riveting']) A11 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接关节角度', '3:A5_Riveting']) A12 = await root.get_child( ["0:Objects", "3:铆接机器人", '3:铆接关节角度', '3:A6_Riveting']) actuator1 = await root.get_child(["0:Objects", "3:末端执行器", '3:主轴']) actuator2 = await root.get_child(["0:Objects", "3:末端执行器", '3:压脚']) actuator3 = await root.get_child(["0:Objects", "3:末端执行器", '3:滑台1']) actuator4 = await root.get_child(["0:Objects", "3:末端执行器", '3:滑台2']) normal1 = await root.get_child(["0:Objects", "3:法向", '3:B']) normal2 = await root.get_child(["0:Objects", "3:法向", '3:C']) normal3 = await root.get_child(["0:Objects", "3:法向", '3:D']) data_name = [ x1, y1, z1, a1, b1, c1, A1, A2, A3, A4, A5, A6, x2, y2, z2, a2, b2, c2, A7, A8, A9, A10, A11, A12, actuator1, actuator2, actuator3, actuator4, normal1, normal2, normal3 ] # 设置可写 for i in data_name: await i.set_writable() print('\n') print('【OPC UA Server初始化完成】') print('【读取config.txt参数配置】') file = 'config.txt' try: with open(file) as f: lines = f.readlines() ip = lines[0].split(':')[1].strip() port = int(lines[1].split(':')[1]) cycle = int(lines[2].split(':')[1]) / 1000 except Exception as e: print(e) else: print(f'【开始与{ip}:{port}机器人程序建立连接】') # socket通信 def socket_client(ip, port): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, port)) except socket.error as e: print(f'Socket连接错误:{e} 请确认连接参数重新启动程序') # sys.exit(1) else: send_data = '0' s.send(send_data.encode()) receive_data = s.recv(1024).decode() # kuka_data=data_analysis(send_data,receive_data) # print("收到信息", receive_data) # s.close() return receive_data # OPC UA 数据更新 async with server: print(f'【OPC UA Server启动完成开始转发数据】') print(f'【OPC UA Server地址{endpoint} 采集周期{cycle * 1000}ms】') while True: await asyncio.sleep(cycle) # 数据更新周期 data = socket_client(ip, port) try: n = 0 for i in data_name: await i.write_value(data.split(',')[n]) n += 1 except Exception as e: # print(f'ERROR: {e}') pass else: pass
items.append(ua.LocalizedText(value.name)) return items if __name__ == "__main__": # setup our server server = Server() server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/") # setup our own namespace, not really necessary but should as spec uri = "http://examples.freeopcua.github.io" nsidx = server.register_namespace(uri) # -------------------------------------------------------- # create custom enum data type # -------------------------------------------------------- enums = server.get_root_node().get_child(["0:Types", "0:DataTypes", "0:BaseDataType", "0:Enumeration"]) # 1. # Create Enum Type myenum_type = enums.add_data_type(nsidx, 'MyEnum') # 2. # Add enumerations as EnumStrings (Not yet tested with EnumValues) # Essential to use namespace 0 for EnumStrings ! # By hand # es = myenum_type.add_variable(0, "EnumStrings" , [ua.LocalizedText("ok"), # ua.LocalizedText("idle")]) # Or convert the existing IntEnum MyEnum es = myenum_type.add_property(0, "EnumStrings" , enum_to_stringlist(MyEnum))