Difference between revisions of "KMAPI/zh-hans"

From SoftMC-Wiki
Jump to: navigation, search
(Created page with "{{Languages}} =Overview= The KMAPI software package allows you to communicate with softMC from popular programming languages, such as Visual Basic over TCP/IP and UDP. The KMA...")
 
Line 1: Line 1:
 
{{Languages}}
 
{{Languages}}
=Overview=
+
=概述=
The KMAPI software package allows you to communicate with softMC from popular programming languages, such as Visual Basic over TCP/IP and UDP. The KMAPI provides complete access to the command line interface of your system across a TCP/IP connection. Fast access to frequently changing data, such as axis set-points and feedback, is provided via a UDP connection, also called UDP fast data.
+
KMAPI软件包允许您使用流行的编程语言(例如基于TCP / IP和UDP的Visual Basic)与softMC进行通信。 KMAPI通过TCP / IP连接完全访问系统的命令行界面。通过UDP连接(也称为UDP快速数据)提供快速访问频繁更改的数据(如轴设定点和反馈)。
  
You can find the full KMAPI documentation [[:file: APIRef5.pdf|here]]
+
您可以在[[:file: APIRef5.pdf|here]]查看完整的KMAPI文档。
 
==Introduction==
 
  
The KMAPI addresses several concerns when interfacing your program with KMAPI drives and controllers, including:
+
==介绍==
* sending commands (and receiving responses)
+
当KMAPI驱动器和控制器连接您的程序时,KMAPI解决了几个问题,包括:
* reading and setting amplifier or controller variables
+
* 发送命令(和接收响应)
* sending and retrieving files
+
* 读取和设置放大器或控制器变量
* error handling
+
* 发送和检索文件
The KMAPI addresses these concerns by providing a library of functions which allow the user to describe their system in terms of devices (amplifiers, controllers, axes, groups) and then communicate with these devices individually. <br> Central to the KMAPI is the concept of a ''device table''. The device table is a data store (database) which is managed by the KMAPI for the user.<br>
+
* 错误处理
The device table is populated with devices by the programmer, allowing the programmer to execute commands on any device in the device table. Most details of the communication techniques used are hidden allowing
 
the programmer to focus on high level programming instead of communication protocols.
 
  
==General Information==
+
KMAPI通过提供功能库来解决这些问题,允许用户根据设备(放大器,控制器,轴,组)来描述系统,然后单独与这些设备进行通信。 <br> KMAPI的核心是''设备表''的概念。 设备表是由KMAPI为用户管理的数据存储(数据库。<br>
 +
设备表由程序员填充设备,允许程序员在设备表中的任何设备上执行命令。 使用的通信技术的大部分细节是隐藏的,使程序员专注于高级编程而不是通信协议。
  
The KMAPI is available for Windows NT and is compatible with a wide variety of 32-bit programming tools including Microsoft Visual Basic and Microsoft Visual C++.<br>  
+
==一般信息==
The KMAPI is provided as a Windows DLL which makes it accessible from most Windows programming languages. <br>
+
KMAPI可用于Windows NT,并兼容各种32位编程工具,包括Microsoft Visual Basic和Microsoft Visual C ++<br>  
Most KMAPI functions return an error code which indicates the success of the action requested by the programmer. Full text descriptions of the error are often associated with the error and can be accessed via KMAPI calls.
+
KMAPI作为Windows DLL提供,可以被大多数Windows编程语言访问。<br>
 +
大多数KMAPI函数返回一个错误代码,指示程序员请求的操作成功。 错误的全文描述通常与错误相关联,可以通过KMAPI调用进行访问。
  
==Getting started==
+
==入门==
The following C# code is a minimal console application which performs the following tasks:
+
以下C#代码是执行以下任务的最小控制台应用程序:
* Initialize the library
+
* 初始化库
* Create 'controller' - the software component used for communication
+
* 创建'控制器' - 用于通信的软件组件
* send a command and read the response
+
* 发送命令并读取响应
* shut down the communication
+
* 关闭通信
  
The software is written in C# and uses the KMAPI.DLL to comunicate with the KMTCPIP.EXE communication server, an equivalent C++/C program will look pretty much the same but will have to use the KMAPI32.DLL.
+
该软件以C#编写,并使用KMAPI.DLL与KMTCPIP.EXE通信服务器进行通信,等效的C ++ / C程序将看起来几乎相同,但必须使用KMAPI32.DLL。
  
Note: to use this:
+
 
# Add a reference to kmapi.dll to your project
+
注意:使用这些:
# argument 1 should be of the form: "'''IP:10.4.20.55,5004'''" - 'IP:' as is, the IP address set to your mc, port can be 5001/4/
+
# 添加对kmapi.dll引用到您的项目
# argument 2 is an MC command, try first with a simple command like "?ver"
+
# 参数1的形式应为:"'''IP:10.4.20.55,5004'''" - 'IP:',IP设置为mc地址,端口可以为5001/4 /
 +
# 参数2是一个MC命令,首先尝试一个简单的命令,如"?ver"
  
 
<pre>
 
<pre>
Line 120: Line 119:
 
-->
 
-->
  
==Error Messages==
+
==错误信息==
 
 
Most of the KMAPI functions return an error code. Successful  KMAPI functions return '''KM_ERR_OK'''. Errors originate in the device itself or the KMAPI. <br> When the message originates within the KMAPI, an error number is returned which can be compared against the list of errors in the KMAPI header files (C/C++), or the global files (Visual Basic). <br>
 
In addition the KMAPI also assigns a text message to the error which can be retrieved by calling '''KMErrorGetMessage'''. When the error originates in the device (drive or controller) the KMAPI “parses” the message and stores the relevant information including the error message and the error number as given by the device. <br>
 
This information can be retrieved for the last error via '''KMErrorGetDeviceMessage''' similar to the Win32 KMAPI GetLastError function. <br>
 
Finally, the text of the error message, as the device originally sent, can be retrieved via '''KMErrorGetOriginalDeviceMessage'''.
 
 
 
==Sending/Retrieving Files==
 
 
 
For the sending and retrieval of files the KMAPI provides users with two functions, '''KMPutFile''' and '''KMGetFile''', respectively.<br>
 
In addition to the device handle parameter, the path of the file and the actual command must be sent when calling either of these functions.
 
 
 
==Advanced asynchronous message handling==
 
 
 
Some controllers and drives generate messages not related to a specific command called ''asynchronous messages''. <br>
 
Some examples of asynchronous messages are overspeed warnings, limit switch closures and runtime error messages from softMC tasks.<br>
 
When these messages are received the KMAPI converts them into Windows messages (events) which the programmer can request to have delivered to their application.
 
  
 +
大多数KMAPI函数返回错误代码。 成功的KMAPI函数返回'''KM_ERR_OK'''。 错误发生在设备本身或KMAPI中。<br>当消息发生在KMAPI内时,返回错误代号,可以将其与KMAPI头文件(C / C ++)或全局文件(Visual Basic)中的错误列表进行比较。 <br>
 +
此外,KMAPI还会为可以通过调用'''KMErrorGetMessage'''检索的错误分配一条消息。当错误发生在设备(驱动器或控制器)中时,KMAPI“解析”消息,并存储包括设备给出的错误消息和错误代号的相关信息。<br>
 +
可以通过类似于Win32 KMAPI GetLastError函数的'''KMErrorGetDeviceMessage'''检索最后一个错误信息。<br>
 +
最后,可以通过'''KMErrorGetOriginalDeviceMessage'''检索作为最初发送错误消息的文本的设备。
  
By default the KMAPI displays each asynchronous message in a modal dialog box (via the Win32 MessageBox function). <br>
+
==发送/检索文件==
If the programmer wants to handle these messages differently the '''KMAsyncSetHandler''' KMAPI function allows a window to be registered as the destination for these messages. <br>
+
为了发送和检索文件,KMAPI分别为用户提供'''KMPutFile''''''KMGetFile'''两种功能。<br>
'''KMAsyncGetHandler''' can be used to save the previous error handler to that the programmer can restore it on demand.
+
除了设备句柄参数之外,当调用这些函数之一时,必须发送文件的路径和实际命令。
  
 +
==高级异步消息处理==
 +
一些控制器和驱动器生成与特定命令无关的消息,称为''异步消息''。<br>
 +
异步消息的一些示例是超速警告,限制开关闭合和来自softMC任务的运行时错误消息。<br>
 +
收到这些消息后,KMAPI将它们转换成Windows消息(事件),程序员可以请求将其发送到其应用程序。
 +
默认情况下,KMAPI在模态对话框(通过Win32 MessageBox功能)中显示每个异步消息。<br>
 +
如果程序员想要不同地处理这些消息,则'''KMAsyncSetHandler'''KMAPI功能允许将窗口注册为这些消息的目的地。<br>
 +
可以使用'''KMAsyncGetHandler'''保存以前的错误处理程序,程序员可以根据需要恢复它。
 +
像所有Windows消息一样,'''WM_KM_ASYNC'''有两个参数:wParam和lParam。<br>
 +
lParam包含缓冲区的句柄,其中包含KMAPI接收到的异步消息。<br>
 +
程序员可以使用'''KMAsyncGetMessage'''从KMAPI获取异步消息。
  
Like all Windows messages, '''WM_KM_ASYNC''' has two parameters, wParam and lParam. <br>
+
===异步消息和Microsoft Visual Basic===
lParam contains a handle to the buffer which contains the asynchronous message as received by the KMAPI.<br>
+
Visual Basic不提供直接处理Windows消息的简单机制。 <br>
The programmer can use '''KMAsyncGetMessage''' to get the asynchronous message from the KMAPI.
+
为了克服这个限制,提供了一种称为KMAPI的自定义控件,其具有表示异步消息的接收的事件。<br>
 +
窗体上放置该控制自动初始化KMAPI('''KMInitialize''')并注册为异步消息的形式。
  
 
+
=库参考=
===Asynchronous messages and Microsoft Visual Basic===
+
==KMAPI初始化==
 
 
Visual Basic does not provide an easy mechanism for directly handling Windows messages. <br>
 
In order to overcome this limitation a custom control called KMAPI has been provided which has an event representing the receipt of an asynchronous message.<br>
 
Placing this control on your form automatically initializes the KMAPI ('''KMInitialize''') and registers the form for asynchronous messages.
 
 
 
=Library Reference=
 
==KMAPI Initialization==
 
 
===KMInitialize===
 
===KMInitialize===
  
  
Prepares the KMAPI for use:
+
准备使用KMAPI:
  
 
{| border = "1" cellpadding = "2" cellspacing = "0"  
 
{| border = "1" cellpadding = "2" cellspacing = "0"  
Line 181: Line 172:
 
{| border="0" cellpadding="10" cellspacing="1"
 
{| border="0" cellpadding="10" cellspacing="1"
  
|'''Return Value'''  
+
|'''返回值'''  
|KM_ERR_OK if KMAPI is successfully initialized. KM_ERR_WINDOWS_API if the asynchronous message window can’t be created. <br> KM_ERR_MEM_LOCK if the application context was not successfully created.
+
|如果成功初始化KMAPI,KM_ERR_OK。如果不能创建异步消息窗口,KM_ERR_WINDOWS_API。<br>如果应用程序上下文未成功创建, KM_ERR_MEM_LOCK。
 
|-
 
|-
|'''Remarks''' ||The KMInitialize function must be called before any other KMAPI functions are called.
+
|'''备注''' ||在调用任何其他KMAPI函数之前,必须调用KMInitialize函数。
 
|-
 
|-
|'''See Also''' || KMTerminate
+
|'''参见''' || KMTerminate
 
|-
 
|-
|'''Example'''|| See Example #1
+
|'''例子'''|| 参考 Example #1
 
|}
 
|}
  
 
===KMTerminate===
 
===KMTerminate===
  
Performs KMAPI cleanup required for proper application termination.
+
为执行正确的应用程序终止清理所需的KMAPI。
  
  
Line 215: Line 206:
 
{| border="0" cellpadding="10" cellspacing="1"
 
{| border="0" cellpadding="10" cellspacing="1"
  
|'''Return Value'''  
+
|'''返回值'''  
|KM_ERR_OK if KMAPI is successfully  terminated. KM_ERR_MEM_LOCK if the application context cannot be locked or removed. <br> KM_ERR_NO_CONTEXT if the application context cannot be found in the application context registry.
+
|如果KMAPI成功终止,KM_ERR_OK。如果应用程序上下文不能被锁定或删除,KM_ERR_MEM_LOCK。 <br>如果在应用程序上下文注册表中找不到应用程序上下文,KM_ERR_NO_CONTEXT。
 
|-
 
|-
|'''Remarks''' ||The KMTerminate function must be called before the application using the KMAPI terminates.
+
|'''备注''' ||必须在使用KMAPI终止的应用程序前调用KMTerminate函数。
 
|-
 
|-
|'''See Also''' ||  KMInitialize
+
|'''参见''' ||  KMInitialize
 
|-
 
|-
|'''Example'''|| See Example #1
+
|'''例子'''|| 参考 Example #1
 
|}
 
|}
  
==Device Management==
+
==设备管理==
 
===KMCreateController===
 
===KMCreateController===
  
Line 249: Line 240:
 
{| border="0" cellpadding="10" cellspacing="1"
 
{| border="0" cellpadding="10" cellspacing="1"
  
|''controllerProduct'' ||Type of controller to create
+
|''controllerProduct'' ||要创建的控制器的类型
 
|-
 
|-
|''controllerName'' || String name for the controller
+
|''controllerName'' || 控制器的字符串名称
 
|-
 
|-
|''controllerCommType'' ||How the API should communicate with the controller (see communication type description)
+
|''controllerCommType'' ||API如何与控制器通信(请参阅通讯类型说明)
 
|-
 
|-
|''controllerID'' || What the controller’s address is
+
|''controllerID'' ||控制器的地址
 
|-
 
|-
|commOptions || COM port used by the controller
+
|commOptions || 控制器使用的COM端口
 
|-
 
|-
  
|'''Return Value'''  
+
|'''返回值'''  
|A handle referring to the controller.
+
|指向控制器的句柄。
 
|-
 
|-
|'''Remarks''' ||See product type description for ''controllerProduct''. <br> ''controllerName'' is provided for user convenience and is not used internally by the KMAPI. <br> See communication type description for ''controllerCommType''. <br> ''commOptions'' is a general parameter that is intended for many uses. Presently, only connections of type COMM_TCPIP use this parameter. If COMM_TCPIP is selected then the ''commOptions'' parameter allows the user to specify what controller to connect to by IP address, controller name, serial number or DIP switch setting. <br> The format for ''commOptions'' is as follows: <br>  
+
|'''备注''' ||''controllerProduct''查看产品类型说明. <br> ''controllerName''是为了方便用户而提供的,KMAPI不在内部使用 <br> ''controllerCommType''查看通信类型说明. <br> ''commOptions''是一个通用参数,用于许多用途。 目前,只有类型COMM_TCPIP的连接使用此参数。 如果选择了COMM_TCPIP,则''commOptions''参数允许用户通过IP地址,控制器名称,序列号或DIP开关设置指定要连接的控制器。 <br> ''commOptions''的格式如下: <br>  
* “IP: xx.xx.xx.xx” where xx.xx.xx.xx is an IP address (e.g. “192.10.34.6”).
+
* “IP: xx.xx.xx.xx”其中xx.xx.xx.xx是IP地址(例如“192.10.34.6”)。
* “NM: yyyyy” where yyyyy is a name assigned by the user to the Sys.Name property of the controller.
+
* “NM: yyyyy”其中yyyyy是由用户分配给控制器的Sys.Name属性的名称
* “SN: XXXXX-XXX” where XXXXX-XXX is the serial number assigned to the controller at the factory and printed on the model number label.
+
* “SN:XXXXX-XXX”,其中XXXXX-XXX是出厂时分配给控制器的序列号,并打印在型号标签上。
* “DP: XXXXXX” where XXXXXX is the value (in decimal) of the DIP switch setting on controllers that support. This is useful for user configuration of addresses.
+
* “DP:XXXXXX”,其中XXXXXX是支持的控制器上DIP开关设置的值(十进制)。 这对用户配置地址很有用。
When connecting via serial the IP address should is always be specified as 91.0.0.2. <br> If ''commOptions'' is not used, the parameter should be an empty string (“”). <br> '''KMCreateController''' allocates memory on behalf of the user. This memory must be freed before program termination via '''KMDestroyDevice'''. <br> The range for ''controllerDevice'' is 1 - 9.
+
当通过串口连接时,IP地址总是被指定为91.0.0.2。 <br>如果不使用''commOptions'',则该参数应为空字符串(“”)。 <br> '''KMCreateController'''代表用户分配内存。 必须通过'''KMDestroyDevice'''在程序终止前释放该内存。<br> ''controllerDevice''的范围为1 - 9
 
|-
 
|-
|'''See Also''' ||  ____
+
|'''参见''' ||  ____
 
|-
 
|-
|'''Example'''|| See Example #1
+
|'''例子'''|| 参考 Example #1
 
|}
 
|}
  
==Device Access==
+
==设备访问==
 
===KMExecuteCmd===
 
===KMExecuteCmd===
  
  
Sends a command to a device.
+
向设备发送命令。
  
  
Line 298: Line 289:
 
{| border="0" cellpadding="10" cellspacing="1"
 
{| border="0" cellpadding="10" cellspacing="1"
  
|''device'' || Handle for the device the command is being sent to.
+
|''device'' || 处理正在发送命令的设备
 
|-
 
|-
|''cndStr || The command to send.
+
|''cndStr || 发送命令。
 
|-
 
|-
|'''Return Value''' ||KM_ERR_OK if command is successfully transmitted
+
|'''Return Value''' ||如果命令成功发送,KM_ERR_OK
 
|-
 
|-
|'''Remarks''' ||Sends the command ''cmdStr'' unmodified to the device specified by ''device''. Should be used with commands that do not have responses.
+
|'''Remarks''' ||将未修改命令''cmdStr''发送到由''设备''指定的设备。 应该与没有响应的命令一起使用。
 
|-
 
|-
 
|'''See Also''' ||  KMExecuteCmdResponse
 
|'''See Also''' ||  KMExecuteCmdResponse
Line 314: Line 305:
  
  
Sends a command to a device and waits for a response.
+
向设备发送命令并等待响应。
 
 
  
 
{| border = "1" cellpadding = "2" cellspacing = "0"  
 
{| border = "1" cellpadding = "2" cellspacing = "0"  
Line 333: Line 323:
 
{| border="0" cellpadding="10" cellspacing="1"
 
{| border="0" cellpadding="10" cellspacing="1"
  
|''device'' || Handle for the device the command is being sent to.
+
|''device'' || 处理正在发送命令的设备。
 
|-
 
|-
|''cndStr || The command to send.
+
|''cndStr || 发送的命令
 
|-
 
|-
|''outStr'' || A buffer for the response.
+
|''outStr'' ||缓冲区的响应。
 
|-
 
|-
|''outSize'' || Size of the outStr buffer.
+
|''outSize'' || outStr缓冲区的大小。
 
|-
 
|-
|'''Return Value''' ||KM_ERR_OK if command is successfully transmitted
+
|'''Return Value''' ||如果命令成功发送,KM_ERR_OK
 
|-
 
|-
|'''Remarks''' ||Sends the command ''cmdStr'' unmodified to the device specified by ''device''. Should be used with commands that have responses.
+
|'''Remarks''' ||将未修改的命令''cmdStr''发送到 ''设备 ''指定的设备。 应该与具有响应的命令一起使用。
 
|-
 
|-
 
|'''See Also''' ||  KMExecuteCmd
 
|'''See Also''' ||  KMExecuteCmd
Line 351: Line 341:
  
 
===Example #1===
 
===Example #1===
 
+
该程序显示如何初始化KMAPI,在每个设备上创建设备和访问变量。
This program shows how to initialize the KMAPI, create devices and access variables on each of the devices.
 
  
 
{|  
 
{|  
Line 364: Line 353:
  
  
Retrieve a file from a device.
+
从设备检索文件。
  
  
Line 383: Line 372:
 
{| border="0" cellpadding="10" cellspacing="1"
 
{| border="0" cellpadding="10" cellspacing="1"
  
|''device'' || Handle for the device the file is being retrieved from.
+
|''device'' || 处理正在检索的设备文件。
 
|-
 
|-
|''cmdStr'' || The command string to cause the device to send a file.
+
|''cmdStr'' || 用于使设备发送文件的命令字符串。
 
|-
 
|-
|''filename'' || Filename to save file to on host computer.
+
|''filename'' ||将文件保存到主机上的文件名。
 
|-
 
|-
|'''Return Value''' ||KM_ERR_OK if command is successfully transmitted
+
|'''Return Value''' ||如果命令成功发送,KM_ERR_OK
 
|-
 
|-
|'''Remarks''' ||The filename (if there is one) in ''cmdStr'' and ''filename'' parameters do not have to match.
+
|'''Remarks''' ||cmdStr和filename参数中的文件名(如果有的话)不一定要匹配。
 
|-
 
|-
 
|'''See Also''' ||  KMPutFile
 
|'''See Also''' ||  KMPutFile
Line 400: Line 389:
 
===KMPutFile===
 
===KMPutFile===
  
Sends a file to a device.
+
将文件发送到设备。
  
  
Line 419: Line 408:
 
{| border="0" cellpadding="10" cellspacing="1"
 
{| border="0" cellpadding="10" cellspacing="1"
  
|''device'' || Handle for the device the file is being sent to.
+
|''device'' || 处理正在发送文件的设备
 
|-
 
|-
|''cmdStr || The command string to cause the device to retrieve a file.
+
|''cmdStr || 用于使设备检索文件的命令字符串
 
|-
 
|-
|''filename'' || Filename of file on host computer..
+
|''filename'' || 主机上文件的文件名
 
|-
 
|-
|'''Return Value''' ||KM_ERR_OK if command is successfully transmitted
+
|'''Return Value''' ||如果命令成功发送,KM_ERR_OK
 
|-
 
|-
|'''Remarks''' ||The filename (if there is one) in ''cmdStr'' and ''filename'' parameters do not have to match.
+
|'''Remarks''' ||cmdStr和filename参数中的文件名(如果有的话)不一定要匹配。
 
|-
 
|-
 
|'''See Also''' ||  KMGetFile
 
|'''See Also''' ||  KMGetFile
 
|-
 
|-
|'''Example'''|| See Example #2
+
|'''Example'''|| 参考 Example #2
 
|}
 
|}
  
 
===Example #2===
 
===Example #2===
 
+
该程序显示如何通过KMAPI向/从设备(控制器或放大器)发送和检索文件。
This program shows how to send and retrieve files to/from a device (controller or amplifier) via the KMAPI.
 
  
 
{|
 
{|
Line 447: Line 435:
 
|}
 
|}
  
==Error Handling==
+
==错误处理==
 
 
Error handling is one of the most important elements to creating a robust application. <br>
 
The KMAPI provides comprehensive error handling which allows the programmer to properly handle all situations. While using the KMAPI errors can occur at the following levels:
 
* Internal to the KMAPI
 
** host computer runs out of memory
 
* Communication between the API and the device
 
** device fails to respond in time
 
* Internal to the device
 
** a move cannot be made due to a limit switch being open
 
  
 +
错误处理是创建健壮性应用程序的最重要元素之一。 <br>
 +
KMAPI提供全面的错误处理,允许程序员正确处理所有情况。使用KMAPI错误可能会发生在以下阶段:
 +
* KMAPI内部
 +
** 主机耗尽内存
 +
* API和设备之间的通信
 +
** 设备无法及时响应
 +
* 设备内部
 +
** 由于限位开关打开,不能进行移动
  
Every function in the KMAPI returns a result, which is almost always of the type KMErrorCode (Long for Visual Basic users). Each error type is well identified in the error code. <br>
+
KMAPI中的每个功能都返回一个结果,几乎总是类型为KMErrorCode(Visual Basic用户的Long)。 每个错误类型都在错误代码中很好地识别。 <br>
The following is an example of the proper method of handling errors returned from the KMAPI: <br>
+
以下是处理从KMAPI返回的错误的正确方法的示例: <br>
  
 
{|
 
{|
Line 471: Line 458:
 
|}
 
|}
  
===Error Codes===
+
===错误代码===
  
The following is a list of the error codes that can be returned by the KMAPI.
+
以下是KMAPI可以返回的错误代码的列表。
  
  
Line 699: Line 686:
 
===KMErrorGetMessage===
 
===KMErrorGetMessage===
  
Get text of the error message related to an error code.
+
获取与错误代码相关的错误消息的文本。
  
  
Line 718: Line 705:
 
{| border="0" cellpadding="10" cellspacing="1"
 
{| border="0" cellpadding="10" cellspacing="1"
  
|''errCode'' || Error code to get the message for
+
|''errCode'' || 获取消息的错误代码
 
|-
 
|-
|''buf'' || Character array buffer to store error message in
+
|''buf'' || 用于存储错误消息的字符数组缓冲区
 
|-
 
|-
|''bufLen'' ||Length of ''buf'' array.
+
|''bufLen'' ||buf数组长度
 
|-
 
|-
|'''Return Value''' ||KM_ERR_OK if command is successfully transmitted
+
|'''Return Value''' ||如果命令成功发送,KM_ERR_OK
 
|-
 
|-
|'''Remarks''' ||The text returned is device dependent and is not translated in any way. <br> If more than one line of text is returned the lines will be separated with newline characters (‘\n’).
+
|'''Remarks''' ||返回的文本是依赖于设备的,不会以任何方式进行转换。 <br> 如果返回多行文本,将用换行符('\ n')分隔行。
 
|-
 
|-
 
|'''See Also''' ||  KMErrorGetDeviceMessage, KMErrorGetOriginalDeviceMessage
 
|'''See Also''' ||  KMErrorGetDeviceMessage, KMErrorGetOriginalDeviceMessage
 
|-
 
|-
|'''Example'''|| See Example #3
+
|'''Example'''|| 参考 Example #3
 
|}
 
|}
  
Line 736: Line 723:
  
  
Get original text and error number of last device error.
+
获取最后设备错误的原始文本和错误编号。
  
 
{| border = "1" cellpadding = "2" cellspacing = "0"  
 
{| border = "1" cellpadding = "2" cellspacing = "0"  
Line 754: Line 741:
 
{| border="0" cellpadding="10" cellspacing="1"
 
{| border="0" cellpadding="10" cellspacing="1"
  
|''pErrCode'' || Pointer to variable to store last error number in
+
|''pErrCode'' || 指向变量存储最后一个错误编号的指针
 
|-
 
|-
|''buf'' || Character array buffer for device error message
+
|''buf'' || 字符阵列缓冲区用于设备错误消息
 
|-
 
|-
|''bufLen'' ||Length of ''buf'' array.
+
|''bufLen'' ||buf数组长度
 
|-
 
|-
|'''Return Value''' ||KM_ERR_OK if command is successfully transmitted
+
|'''Return Value''' ||如果命令成功发送,KM_ERR_OK
 
|-
 
|-
|'''Remarks''' ||Last device error is stored on a per application context basis.<br> The text returned is device dependent and is not translated in any way. <br> If more than one line of text is returned the lines will be separated with newline characters (‘\n’).
+
|'''Remarks''' ||最后的设备错误存储在每个应用程序上下文的基础上。<br>返回的文本是依赖于设备的,不会以任何方式进行转换。<br> 如果返回多行文本,将用换行符('\ n')分隔行。
 
|-
 
|-
 
|'''See Also''' ||  KMErrorGetMessage, KMErrorGetOriginalDeviceMessage
 
|'''See Also''' ||  KMErrorGetMessage, KMErrorGetOriginalDeviceMessage
Line 772: Line 759:
  
  
Retrieves entire original error message from buffer.
+
从缓冲区检索整个原始错误消息。
  
  
Line 791: Line 778:
 
{| border="0" cellpadding="10" cellspacing="1"
 
{| border="0" cellpadding="10" cellspacing="1"
  
|''buf'' || Character array buffer to store error message in
+
|''buf'' || 用于存储错误消息的字符数组缓冲区
 
|-
 
|-
|''bufLen'' ||Length of ''buf'' array.
+
|''bufLen'' ||''buf''数组长度
 
|-
 
|-
|'''Return Value''' ||KM_ERR_OK if command is successfully transmitted
+
|'''Return Value''' ||如果命令成功发送,KM_ERR_OK
 
|-
 
|-
|'''Remarks''' ||The text returned is the original error message that was generated and stored in the buffer
+
|'''Remarks''' ||返回的文本是生成并存储在缓冲区中的原始错误消息
 
|-
 
|-
 
|'''See Also''' ||  KMErrorGetMessage, KMErrorGetDeviceMessage
 
|'''See Also''' ||  KMErrorGetMessage, KMErrorGetDeviceMessage
 
|-
 
|-
|'''Example'''|| See Example #3
+
|'''Example'''|| 参考 Example #3
 
|}
 
|}
  
 
===Example #3===
 
===Example #3===
  
This program shows how to get the text descriptions associated with any error number or the last error that occurred.
+
此程序显示如何获取与任何错误编号或发生的最后一个错误相关联的文本说明。
  
 
{|
 
{|

Revision as of 09:03, 6 July 2017

语言: [[::KMAPI|English]]  • [[::KMAPI/zh-hans|中文(简体)‎]]

概述

KMAPI软件包允许您使用流行的编程语言(例如基于TCP / IP和UDP的Visual Basic)与softMC进行通信。 KMAPI通过TCP / IP连接完全访问系统的命令行界面。通过UDP连接(也称为UDP快速数据)提供快速访问频繁更改的数据(如轴设定点和反馈)。

您可以在here查看完整的KMAPI文档。

介绍

当KMAPI驱动器和控制器连接您的程序时,KMAPI解决了几个问题,包括:

  • 发送命令(和接收响应)
  • 读取和设置放大器或控制器变量
  • 发送和检索文件
  • 错误处理

KMAPI通过提供功能库来解决这些问题,允许用户根据设备(放大器,控制器,轴,组)来描述系统,然后单独与这些设备进行通信。
KMAPI的核心是设备表的概念。 设备表是由KMAPI为用户管理的数据存储(数据库。
设备表由程序员填充设备,允许程序员在设备表中的任何设备上执行命令。 使用的通信技术的大部分细节是隐藏的,使程序员专注于高级编程而不是通信协议。

一般信息

KMAPI可用于Windows NT,并兼容各种32位编程工具,包括Microsoft Visual Basic和Microsoft Visual C ++。
KMAPI作为Windows DLL提供,可以被大多数Windows编程语言访问。
大多数KMAPI函数返回一个错误代码,指示程序员请求的操作成功。 错误的全文描述通常与错误相关联,可以通过KMAPI调用进行访问。

入门

以下C#代码是执行以下任务的最小控制台应用程序:

  • 初始化库
  • 创建'控制器' - 用于通信的软件组件
  • 发送命令并读取响应
  • 关闭通信

该软件以C#编写,并使用KMAPI.DLL与KMTCPIP.EXE通信服务器进行通信,等效的C ++ / C程序将看起来几乎相同,但必须使用KMAPI32.DLL。


注意:使用这些:

  1. 添加对kmapi.dll引用到您的项目
  2. 参数1的形式应为:"IP:10.4.20.55,5004" - 'IP:',IP设置为mc地址,端口可以为5001/4 /
  3. 参数2是一个MC命令,首先尝试一个简单的命令,如"?ver"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KMAPIDemo {
  class Program {
    private static IntPtr device = IntPtr.Zero;

    static void Main(string[] args) {
      if (args.Length != 2)
        return;
      string ipAddress = args[0];
      string cmdToExecute = args[1];
      // *******************************
      // *** initialize and connect ***
      // *******************************
      int retVal = KMAPI.IKMInitialize(); // Initialize the API
      if (retVal != 0) {// if fail
        Console.WriteLine(string.Format("Error: Connection error (IKMInitialize): {0}", retVal));
        return;     // fail
      }
      uint connectionType = 7;  // COMM_TCPIP_DEDICATED = 7 - dedicated device => pre defined ip address
      try {
        device = KMAPI.IKMCreateController(KMAPI.PROD_GPMAC,
            "someName", // dummy
            connectionType,
            1,
            ipAddress);
      } catch (Exception e) {
        KMAPI.IKMTerminate();
        Console.WriteLine(string.Format("Error: Connection error (IKMCreateController): {0}", e.Message));
        return;
      }
      if (device == IntPtr.Zero) {  // if fail
        KMAPI.IKMTerminate();
        Console.WriteLine("Error: Connection error (IKMCreateController): device = 0");
        return;
      }
      // *******************************
      // *** Execute some commands ***
      // *******************************
      const int BuffSz = 100000;
      char[] responseBuff = new char[BuffSz];
      retVal = KMAPI.IKMExecuteCmdResponse(device, cmdToExecute, responseBuff, BuffSz);
      if (retVal != 0)  // fail
        Console.WriteLine("failed");
      else {
        string response = new string(responseBuff);
        int idx0 = response.IndexOf("\0");
        if (idx0 > 0)
          response = response.Substring(0, idx0);
        Console.WriteLine(string.Format("Command '{0}' => response '{1}'", cmdToExecute, response));
      }
      // *****************
      // *** shut down ***
      // *****************
      retVal = KMAPI.IKMDestroyDevice(device); // terminate KMAPI
      if (retVal != 0) {  // if fail
        Console.WriteLine("Error when destroying device");
        return;   // fail
      }
      device = IntPtr.Zero;
      retVal = KMAPI.IKMTerminate();
      if (retVal != 0) {  // if fail
        Console.WriteLine("Error when terminating");
        return;   // fail
      }

    }
  }
}


错误信息

大多数KMAPI函数返回错误代码。 成功的KMAPI函数返回KM_ERR_OK。 错误发生在设备本身或KMAPI中。
当消息发生在KMAPI内时,返回错误代号,可以将其与KMAPI头文件(C / C ++)或全局文件(Visual Basic)中的错误列表进行比较。
此外,KMAPI还会为可以通过调用KMErrorGetMessage检索的错误分配一条消息。当错误发生在设备(驱动器或控制器)中时,KMAPI“解析”消息,并存储包括设备给出的错误消息和错误代号的相关信息。
可以通过类似于Win32 KMAPI GetLastError函数的KMErrorGetDeviceMessage检索最后一个错误信息。
最后,可以通过KMErrorGetOriginalDeviceMessage检索作为最初发送错误消息的文本的设备。

发送/检索文件

为了发送和检索文件,KMAPI分别为用户提供KMPutFileKMGetFile两种功能。
除了设备句柄参数之外,当调用这些函数之一时,必须发送文件的路径和实际命令。

高级异步消息处理

一些控制器和驱动器生成与特定命令无关的消息,称为异步消息
异步消息的一些示例是超速警告,限制开关闭合和来自softMC任务的运行时错误消息。
收到这些消息后,KMAPI将它们转换成Windows消息(事件),程序员可以请求将其发送到其应用程序。 默认情况下,KMAPI在模态对话框(通过Win32 MessageBox功能)中显示每个异步消息。
如果程序员想要不同地处理这些消息,则KMAsyncSetHandlerKMAPI功能允许将窗口注册为这些消息的目的地。
可以使用KMAsyncGetHandler保存以前的错误处理程序,程序员可以根据需要恢复它。 像所有Windows消息一样,WM_KM_ASYNC有两个参数:wParam和lParam。
lParam包含缓冲区的句柄,其中包含KMAPI接收到的异步消息。
程序员可以使用KMAsyncGetMessage从KMAPI获取异步消息。

异步消息和Microsoft Visual Basic

Visual Basic不提供直接处理Windows消息的简单机制。
为了克服这个限制,提供了一种称为KMAPI的自定义控件,其具有表示异步消息的接收的事件。
窗体上放置该控制自动初始化KMAPI(KMInitialize)并注册为异步消息的形式。

库参考

KMAPI初始化

KMInitialize

准备使用KMAPI:

C/C++ KMErrorCode KMInitialize ( void )
Visual Basic KMInitialize() As Long
C# System.Int32 IKMInitialize ()



返回值 如果成功初始化KMAPI,KM_ERR_OK。如果不能创建异步消息窗口,KM_ERR_WINDOWS_API。
如果应用程序上下文未成功创建, KM_ERR_MEM_LOCK。
备注 在调用任何其他KMAPI函数之前,必须调用KMInitialize函数。
参见 KMTerminate
例子 参考 Example #1

KMTerminate

为执行正确的应用程序终止清理所需的KMAPI。


C/C++ KMErrorCode KMTerminate ( void )
Visual Basic KMTerminate() As Long
C# IKMInitialize ()



返回值 如果KMAPI成功终止,KM_ERR_OK。如果应用程序上下文不能被锁定或删除,KM_ERR_MEM_LOCK。
如果在应用程序上下文注册表中找不到应用程序上下文,KM_ERR_NO_CONTEXT。
备注 必须在使用KMAPI终止的应用程序前调用KMTerminate函数。
参见 KMInitialize
例子 参考 Example #1

设备管理

KMCreateController

C/C++ KMDevice KMCreateController (KMProductType controllerProduct, LPSTR controllerName, KMCommType controllerCommType, short controllerID, LPSTR commOptions)
Visual Basic KMCreateController (ByVal controllerProduct As Integer, ByVal controllerName$, ByVal controllerCommType As Integer, ByVal controllerID As Integer, ByVal commOptions$) As Long
C# System.IntPtr IKMCreateController (System.UInt32 controllerProduct, System.String controllerName, System.UInt32 controllerCommType, System.UInt32 controllerID, System.String commOptions)



controllerProduct 要创建的控制器的类型
controllerName 控制器的字符串名称
controllerCommType API如何与控制器通信(请参阅通讯类型说明)
controllerID 控制器的地址
commOptions 控制器使用的COM端口
返回值 指向控制器的句柄。
备注 controllerProduct查看产品类型说明.
controllerName是为了方便用户而提供的,KMAPI不在内部使用
controllerCommType查看通信类型说明.
commOptions是一个通用参数,用于许多用途。 目前,只有类型COMM_TCPIP的连接使用此参数。 如果选择了COMM_TCPIP,则commOptions参数允许用户通过IP地址,控制器名称,序列号或DIP开关设置指定要连接的控制器。
commOptions的格式如下:
  • “IP: xx.xx.xx.xx”其中xx.xx.xx.xx是IP地址(例如“192.10.34.6”)。
  • “NM: yyyyy”其中yyyyy是由用户分配给控制器的Sys.Name属性的名称
  • “SN:XXXXX-XXX”,其中XXXXX-XXX是出厂时分配给控制器的序列号,并打印在型号标签上。
  • “DP:XXXXXX”,其中XXXXXX是支持的控制器上DIP开关设置的值(十进制)。 这对用户配置地址很有用。

当通过串口连接时,IP地址总是被指定为91.0.0.2。
如果不使用commOptions,则该参数应为空字符串(“”)。
KMCreateController代表用户分配内存。 必须通过KMDestroyDevice在程序终止前释放该内存。
controllerDevice的范围为1 - 9

参见 ____
例子 参考 Example #1

设备访问

KMExecuteCmd

向设备发送命令。


C/C++ KMErrorCode KMExecuteCmd (KMDevice device, LPSTR cmdStr)
Visual Basic KMExecuteCmd (ByVal device As Long, ByVal cmdStr$) As Long
C# System.Int32 IKMExecuteCmd (System.IntPtr device , System.String cmdStr)



device 处理正在发送命令的设备
cndStr 发送命令。
Return Value 如果命令成功发送,KM_ERR_OK
Remarks 将未修改命令cmdStr发送到由设备指定的设备。 应该与没有响应的命令一起使用。
See Also KMExecuteCmdResponse
Example See Example #1

KMExecuteCmdResponse

向设备发送命令并等待响应。

C/C++ KMErrorCode KMExecuteCmdResponse (KMDevice device, LPSTR cmdStr, LPSTR outStr, DWORD outSize)
Visual Basic KMExecuteCmdResponse (ByVal device As Long, ByVal cmdStr$, ByVal outStr$, ByVal outSize As Long) As Long
C# System.Int32 IKMExecuteCmdResponse (System.IntPtr device , System.String cmdStr , char[] outStr , System.UInt32 outSize)



device 处理正在发送命令的设备。
cndStr 发送的命令
outStr 缓冲区的响应。
outSize outStr缓冲区的大小。
Return Value 如果命令成功发送,KM_ERR_OK
Remarks 将未修改的命令cmdStr发送到 设备 指定的设备。 应该与具有响应的命令一起使用。
See Also KMExecuteCmd
Example See Example #1

Example #1

该程序显示如何初始化KMAPI,在每个设备上创建设备和访问变量。

C/C++ example 1.1.png
Visual Basic example 1.2.png

KMGetFile

从设备检索文件。


C/C++ KMErrorCode KMGetFile (KMDevice device, LPSTR cmdStr, LPSTR filename)
Visual Basic KMGetFile (ByVal device As Long, ByVal cmdStr$, ByVal filename$) As Long
C# System.Int32 IKMGetFile (System.IntPtr device, System.String cmdStr, System.String fileName)



device 处理正在检索的设备文件。
cmdStr 用于使设备发送文件的命令字符串。
filename 将文件保存到主机上的文件名。
Return Value 如果命令成功发送,KM_ERR_OK
Remarks cmdStr和filename参数中的文件名(如果有的话)不一定要匹配。
See Also KMPutFile
Example See Example #2

KMPutFile

将文件发送到设备。


C/C++ KMErrorCode KMPutFile (KMDevice device, LPSTR cmdStr, LPSTR filename)
Visual Basic KMPutFile(ByVal device As Long, ByVal cmdStr$, ByVal filename$) As Long
C# System.Int32 IKMPutFile (System.IntPtr device, System.String cmdStr, System.String fileName)



device 处理正在发送文件的设备
cmdStr 用于使设备检索文件的命令字符串
filename 主机上文件的文件名
Return Value 如果命令成功发送,KM_ERR_OK
Remarks cmdStr和filename参数中的文件名(如果有的话)不一定要匹配。
See Also KMGetFile
Example 参考 Example #2

Example #2

该程序显示如何通过KMAPI向/从设备(控制器或放大器)发送和检索文件。

C/C++ example 2.1.png
Visual Basic example 2.2.png

错误处理

错误处理是创建健壮性应用程序的最重要元素之一。
KMAPI提供全面的错误处理,允许程序员正确处理所有情况。使用KMAPI错误可能会发生在以下阶段:

  • KMAPI内部
    • 主机耗尽内存
  • API和设备之间的通信
    • 设备无法及时响应
  • 设备内部
    • 由于限位开关打开,不能进行移动

KMAPI中的每个功能都返回一个结果,几乎总是类型为KMErrorCode(Visual Basic用户的Long)。 每个错误类型都在错误代码中很好地识别。
以下是处理从KMAPI返回的错误的正确方法的示例:

C/C++ error 1.1.png
Visual Basic error 1.2.png

错误代码

以下是KMAPI可以返回的错误代码的列表。


Error code Description Value (hex)
KM_ERR_OK H00000000
KM_ERR_BAD HFFFFFFFF
KM_ERR_BAD_CRC H00800001
KM_ERR_TIME_OUT H00800002
KM_ERR_BAD_DEVICE_ID H00800003
KM_ERR_DEVICE_NOT_INSTALLED H00800004
KM_ERR_LOCK_FAILED H00800005
KM_ERR_NO_DATA H00800006
KM_ERR_ASYNC H00800008
KM_ERR_FAIL_SEND_MSG H00800009
KM_ERR_FAIL_ACK_NAK H0080000A
KM_ERR_FAIL_ACCEPT H0080000B
KM_ERR_FAIL_PROTOCOL H0080000C
KM_ERR_FAIL_RESPOND H0080000D
KM_ERR_UNKNOWN_MESSAGE H0080000E
KM_ERR_MEM_LOCK H0080000F
KM_ERR_FAIL_CREATE_FILE H00800010
KM_ERR_FAIL_OPEN_FILE H00800011
KM_ERR_FAIL_FIND_FILE H00800012
KM_ERR_UNEXPECTED_EOF H00800013
KM_ERR_NO_CONTEXT H00800014
KM_ERR_MEM_ALLOC H00800015
KM_ERR_INVALID_PRODUCT H00800016
KM_ERR_SYSTEM H00800017
KM_ERR_FAIL_FIND_NAME H00800018
KM_ERR_INVALID_TYPE H00800019
KM_ERR_NO_DEFAULT H0080001A
KM_ERR_NO_MAX_MIN H0080001B
KM_ERR_BAD_IRQ_NUMBER H0080001C
KM_ERR_INVALID_FORMAT H0080001D
KM_ERR_NOT_IMPLEMENTED H0080001E
KM_ERR_FAIL_FIND_MSG H0080001F
KM_ERR_NOT_DEVICE H00800020
KM_ERR_FAILED_OPEN_DEVICE H00800021
KM_ERR_INVALID_VALUE H00800022
KM_ERR_INVALID_DEVICE_CONTEXT H00800023
KM_ERR_GROUP_MAX_EXCEEDED H00800024
KM_ERR_AXIS_ALREADY_IN_GROUP H00800025
KM_ERR_FAIL_FIND_DEVICE H00800026
KM_ERR_FAIL_WRITE_FILE H00800027
KM_ERR_FAIL_READ_FILE H00800028
KM_ERR_CONFIG_ALREADY_EXISTS H00800029
KM_ERR_DEVICE_ALREADY_EXISTS H0080002A
KM_ERR_FAIL_PRODUCT_DLL H0080002B
KM_ERR_WINDOWS_API H0080002C
KM_ERR_VARIABLE_NOT_FOUND H0080002D
KM_ERR_SERIAL_FRAMING H0080002E
KM_ERR_FAILED_CLOSE_DEVICE H0080002F
KM_ERR_FAIL_PRODUCT_DLL H00800030
KM_ERR_SYNTAX_ERROR H00800100
KM_ERR_SSMC_ERROR H00030027
KM_ERR_SERVOSTAR_ERROR H00010027
KM_ERR_SSMC_DRIVER_ERROR H00040027

KMErrorGetMessage

获取与错误代码相关的错误消息的文本。


C/C++ KMErrorCode KMErrorGetMessage (KMErrorCode errCode, LPSTR buf, short bufLen)
Visual Basic KMErrorGetMessage (ByVal errCode As Long, ByVal buf$, ByVal bufLen As Integer) As Long
C# System.Int32 IKMErrorGetMessage (System.Int32 errCode, char[] buf, System.UInt32 bufLen)



errCode 获取消息的错误代码
buf 用于存储错误消息的字符数组缓冲区
bufLen buf数组长度
Return Value 如果命令成功发送,KM_ERR_OK
Remarks 返回的文本是依赖于设备的,不会以任何方式进行转换。
如果返回多行文本,将用换行符('\ n')分隔行。
See Also KMErrorGetDeviceMessage, KMErrorGetOriginalDeviceMessage
Example 参考 Example #3

KMErrorGetDeviceMessage

获取最后设备错误的原始文本和错误编号。

C/C++ KMErrorCode KMErrorGetDeviceMessage (KMErrorCodePtr pErrCode, LPSTR buf, short bufLen)
Visual Basic KMErrorGetDeviceMessage (pErrCode As Long, ByVal buf$, ByVal bufLen As Integer) As Long
C# System.Int32 IKMErrorGetDeviceMessage (System.IntPtr pErrCode, char[] buf, System.UInt32 bufLen)



pErrCode 指向变量存储最后一个错误编号的指针
buf 字符阵列缓冲区用于设备错误消息
bufLen buf数组长度
Return Value 如果命令成功发送,KM_ERR_OK
Remarks 最后的设备错误存储在每个应用程序上下文的基础上。
返回的文本是依赖于设备的,不会以任何方式进行转换。
如果返回多行文本,将用换行符('\ n')分隔行。
See Also KMErrorGetMessage, KMErrorGetOriginalDeviceMessage
Example See Example #3

KMErrorGetOriginalDeviceMessage

从缓冲区检索整个原始错误消息。


C/C++ KMErrorCode KMErrorGetOriginalDeviceMessage (LPSTR buf, short bufLen)
Visual Basic KMErrorGetOriginalDeviceMessage (ByVal buf$, ByVal bufLen As Integer) As Long
C# System.Int32 IKMErrorGetOriginalDeviceMessage (char[] buf, System.UInt32 bufLen)



buf 用于存储错误消息的字符数组缓冲区
bufLen buf数组长度
Return Value 如果命令成功发送,KM_ERR_OK
Remarks 返回的文本是生成并存储在缓冲区中的原始错误消息
See Also KMErrorGetMessage, KMErrorGetDeviceMessage
Example 参考 Example #3

Example #3

此程序显示如何获取与任何错误编号或发生的最后一个错误相关联的文本说明。

C/C++ example 3.1.png
Visual Basic example 3.2.png