基于Python的Adams二次开发——通过Command Server实现Adams和Python的通讯

# 参考file:///D:/ProgramFiles/MSC.Software/Adams/2017_2/help/wwhelp/wwhimpl/common/html/wwhelp.htm#href=adams_view/command_server.html&single=true

# 参考https://blog.csdn.net/weixin_33698043/article/details/94562370

'''

先在Adams中打开Command_Server(tool→Command Navigator→Command_Server)。

需要注意的是,指令如果没有发送成功,会造成通讯崩溃。

Command Server - Language

    1. Commands: 

    command strings beginning with the string "cmd" are assumed to be valid Adams View  

    Example:  "cmd file command read file='load_model.cmd'"

    Returns: “cmd: 0” (on success)

            “cmd: 1” (error detected in View command)

            

    2. Queries: 

    command strings beginning with the string "query" are assumed to be valid Adams View Expressions; 

    the Command Server attempts to evaluate these expressions and return the result to the client. 

    Example:  "query part_9.mass"

 

    3. Binary mode: on or off. 

    Commands beginning with the string "binary" toggle binary mode for queries on and off. 

    See section Detailed Query Response Handling for binary vs text queries. 

    Example: "binary on"

'''

###################################################################

####Issuing Commands - Python Example

import socket 

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

client_socket.connect(("localhost", 5002)) 

 

# formulate valid Adams View command language that starts with the string "cmd"

cmd = 'cmd acar toolkit message  message = ("TCP Connected")'

client_socket.send(cmd.encode())

# receives feedback from the server. The server responds with the string "cmd: 0" for successful command processing 

response = client_socket.recv(1024) 

#print(response)

 

#################################################################

#####Issuing Multiple Commands - Python Example

import socket 

import time

cmds = list()

mname = 'test'

cmds.append('acar interface switch template_builder')

cmds.append('acar files template close template_name = ._test')

cmds.append('acar files template new template_name = '+mname)

cmds.append('defaults model model=._'+mname)

cmds.append('acar template_builder hardpoint create \

    hardpoint_name=new_1 \

    location=100,-100,0 \

    type=left ')

 

for cmdi in cmds:

    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

    start_time = time.time()

    dt = time.time() - start_time

    while dt < 60:    # wait for a new server connection:

        dt = time.time() - start_time

        try:

            client_socket.connect(("localhost", 5002))

            break

        except socket.error:

             pass

    print(cmdi)

    client_socket.send(('cmd '+cmdi).encode())

    response = client_socket.recv(1024) 

    #print(response.decode())

 

########################################################################

####Issuing Queries

import socket 

# create a socket & connect to the proper port/host

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

client_socket.connect(("localhost", 5002)) 

# queries must always start with the string "query" followed by an Adams View expression

query = "query hpl_new_1.location" 

client_socket.send(query.encode())

# server replies with a description of the data 

query_description = client_socket.recv(1024)

# server waits for an "OK" command before sending the actual data

client_socket.send("OK".encode()) 

query_data = client_socket.recv(1024)# accepts the actual server data


 

#print(query_description)

description_list = query_description.decode().split(':') 

data_type = description_list[1] 

data_length = int(description_list[2]) 

data_bytes = int(description_list[3]) 

 

print("Query returned {} values of data type {}".format(data_length, data_type))

print("Query data as a string is: {}".format(query_data.decode()))

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值