The Adams Command Server is an Adams View (or Adams Car) component that manages communication between Adams View and external software. Examples of external software include user-written applications created in Microsoft Visual Basic, Python, C, Java or similar. The server listens for either commands or queries from an external application and manages the command or query interaction with the Adams model.
The server has a simple interface that is accessible from other programming languages that implement the TCP/IP communication protocol. The server also contains an interface for Microsoft Visual Basic that simplifies the communication protocol.
多體動力學仿真軟件ADAMS可以通過Adams Command Server與外部程序進行TCP通信。首先需要打開ADAMS中的服務端,在Tool→Command Navigator中找到command_server,點擊show會彈出一個圖形界面的對話框。可以在對話框上點擊Start Server開啟服務器。如下圖所示5002端口已經開啟,可以通過TCP socket接收字符串指令:

由於控制或查詢指令通過TCP以字符串形式發送給command server,因此客戶端程序可以采用Python、C++等語言編寫,只要指令是合法有效的Adams View Commands(具體語法可以參考官方文檔中的Adams View Command Language Help)。外部程序發送的字符串指令主要分為兩種,一種是以cmd開頭的控制指令,另一種是以query開頭的查詢指令:
-
Issuing Commands
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 point create point_name = POINT__1 location = 10 , 15 , 10 relative_to = ground " client_socket.send(cmd) # receives feedback from the server. The server responds with the string "cmd: 0" for successful command processing response = client_socket.recv(1024) print response
# Returns: cmd: 0 (on success) cmd: 1 (error detected in View command)
cmd指令在相對於ground的(10, 15, 10)位置處創建了一個點,命名為POINT_1:
發送指令后收到cmd:0,說明指令執行成功

連續發送多條控制指令時必須通過新建的socket,下面代碼創建了一個整型設計變量integer_numbers,然后又將其指改為16:
import socket import time cmds = ["cmd variable create variable_name=integer_numbers integer_value=12 range=10,20", "cmd variable set variable_name=integer_numbers integer_value=16"] # Each command must be sent to the server over a new socket for cmd 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 "Connected to socket, sending cmd: %s" % cmd client_socket.send(cmd) data = client_socket.recv(1024) print "Response from cmd was: %s" % data
-
Issuing Queries
下面代碼查詢了零件PART_2的位置信息:
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 the_query = "query part_2.location" client_socket.send(the_query) # server replies with a description of the data query_description = client_socket.recv(1024) # Description of result: query: float : 3 : 12 # server waits for an "OK" command before sending the actual data client_socket.send("OK") query_data = client_socket.recv(1024) # accepts the actual server data # parse query data based on type: description_list = query_description.split(':') data_type = description_list[1] data_length = int(description_list[2]) data_bytes = int(description_list[3]) print "Query returned %i values of data type %s" % (data_length, data_type) print "Query data as a string is: %s" % query_data
輸出如下:

參考:
Adams/2017/help/adams_view/Exchanging Data in Adams / Adams Command Server
Command Language Help