套接字的介绍:http://zh.wikipedia.org/wiki/Berkeley%E5%A5%97%E6%8E%A5%E5%AD%97
主要分为客户端和服务器。客户端一般是需要主动去链接,需要配置服务器的IP和端口。服务器是被动响应,需要打开相应的端口。端口一般不推荐使用系统端口和常用软件使用端口。
可以使用socket模拟器来练习编程。可以使用SocketTool,可以到百度网盘下载。http://pan.baidu.com/share/link?shareid=430046&uk=2080830206.免安装,直接可以使用。一般推荐使用Tcp(暂不推荐使用UDP)。
分别创建客户端和服务器。下面是一个利用127.0.0.1建立内环的一个例子。服务器建立以后需要进行监听(listen)
机械手臂控制器配置:
控制面板(Control panel)->输入输出(I/O)->套接字(Socket)有服务器(Servers)和客户端(Clients)
进行服务器和客户端配置。
其相对应的配置文件在/usr/configs/sio.cfx
内容为:
<?xml version="1.0" encoding="utf-8" ?> <sio> <socketClt> <String name="name" value="Test" /> <String name="desc" value="" /> <Inet name="serverIp" value="127.0.0.1" /> <Uint name="portNumber" value="1000" /> <Int name="endOfString" value="13" /> <Float name="timeOut" value="5" /> <Bool name="nagle" value="true" /> </socketClt> <socketSrv> <String name="name" value="fromRobot" /> <String name="desc" value="" /> <Uint name="portNumber" value="1001" /> <Int name="endOfString" value="13" /> <Float name="timeOut" value="5" /> <Bool name="nagle" value="true" /> </socketSrv> </sio>
在Val3 studio里按下F1,可以得到Val3的说明手册。里面有对Sio类型的详细解释。
3.7. sio type 3.7.1. Definition The sio type is used to link a VAL 3 variable to a serial port or an Ethernet socket connection. A sio input-output is characterized by: • Parameters specific to the type of communication, defined in the system • An end of string character, to allow the use of the string type • A communication timeout delay The serial system inputs-outputs are active at all times. The Ethernet socket connections are opened at the time of the initial reading or writing access by a VAL 3 program. The Ethernet socket connections are closed automatically when the VAL 3 application is closed. All instructions using a sio type variable not linked to an input/output declared in the system generate a runtime error. The default initialization value of sio type variables is an undefined link. The link of a sio variable can be initialized from another sio variable, from the robot MCP, or using VAL 3 Studio in Stäubli Robotics Suite. 3.7.2. Operators When the communication time out delay is reached on reading or writing the serial input/output, a runtime error is generated. string <sio siOutput> = <string sText> Writes successively on siOutput the sText characters Unicode UTF8 codes, followed by the end of string character, and returns sText. num <sio siOutput> = <num nData item> Writes on siOutput the closest integer to nData item, modulo 256, and returns the value actually sent. num <num nData> = <sio siInput> Reads a byte on siInput and assigns nData with the byte value. string <string sText> = <sio siInput> Reads on siInput a string of Unicode UFT8 characters and affects sText with this string. The characters that are not supported by the string type are ignored. The string is completed when the end of string character is read, or when sText reaches the maximum size of a string (128 bytes). The end of string character is not copied into sText. To avoid confusions between = and == operators, the = operator is not allowed within VAL 3 expressions used as instruction parameter. nLen=len(sString=siInput) must be replaced with sString=siInput; nLen=len(sString). 3.7.3. Instructions void sioLink(sio& siVariable, sio siSource) Function The instructions links siVariable to the serial system input/output to which siSource is linked. See also void dioLink(dio& diVariable, dio diSource) void aioLink(aio& aiVariable, aio aiSource) num clearBuffer(sio siVariable) Function This instruction clears the siVariable reading buffer and returns the number of characters deleted For an Ethernet socket connection, clearBuffer deactivates (closes) the socket. clearBuffer returns -1 if the socket has already been deactivated. A runtime error is generated if siVariable is not connected to a system serial link or Ethernet socket. num sioGet(sio siInput, num& nData[]) Function This instruction reads a single character or an array of characters from siInput and returns the number of characters read. The reading sequence stops when the nData array is full or when the input reading buffer is empty. For an Ethernet socket connection, sioGet tries first to open a connection if there is no open connection. When the timeout for input communication has been reached, sioGet returns -1. If the connection is open, but there is no data in the input reader buffer, sioGet waits until data is received or until the end of the timeout period has been reached. A runtime error is generated if siInput is not linked to a system serial port or Ethernet socket, or if nData is not a VAL 3 variable. num sioSet(sio siOutput, num& nData[]) Function This instruction writes a character or an array of characters to siOutput and returns the number of characters written. Numerical values are converted before transmission into integers between 0 and 255, taking the nearest integer modulo 256. For an Ethernet socket connection, sioSet tries first to open a connection if there is no open connection. When the end of the output communication waiting time has been reached, sioSet returns -1. The number of characters written can be less than the size of nData if a communication error is detected. A runtime error is generated if siOutput is not linked to a system serial port or Ethernet socket. num sioCtrl(sio siChannel, string nParameter, *value) Function This instruction modifies a communication parameter of the specified serial input/output siChannel. For serial lines, some parameters or parameter values may not be supported by the hardware: refer to the controller's manual. The instruction returns: 0 The parameter is successfully modified -1 The parameter is not defined -2 The parameter value has not the expected type -3 The parameter value is not supported -4 The serial channel is not ready to apply the parameter change (stop it first) -5 The parameter is not defined for this type of channel The supported parameters are given by the table below: Parameter name Parameter type Description "port" num (For TCP client or server) TCP port "target" string (For TCP client) IP address of the TCP server to reach, such as "192.168.0.254" "clients" num (For TCP server) Maximum number of simultaneous clients on the server "endOfString" num (For serial line, TCP client and server) ASCII code for the end of string character to be used with sio '=' operators (in range [0, 255]) "timeout" num (For serial line, TCP client and server) Maximum response time for the communication channel. 0 means no time out. "baudRate" num (For serial line) Communication speed "parity" string (For serial line) Parity control: "none", "even" or "odd" "bits" num (For serial line) Number of bits per byte (5, 6, 7 or 8) "stopBits" num (For serial line) Number of stop bits per byte (1 or 2) "mode" string (For serial line) Communication mode: "rs232" or "rs422" "flowControl" string (For serial line) Flow control: "none" or "hardware" "nagle" bool (For TCP client or server) enable (default) or disable the nagle optimization. Disabling nagle optimization improves response time but increases network load. Example This program configures the main parameters of a serial line. sioCtrl(siPortSerial1, "baudRate", 115200)sioCtrl(siPortSerial1, "bits", 8)sioCtrl(siPortSerial1, "parity", "none")sioCtrl(siPortSerial1, "stopBits", 1)sioCtrl(siPortSerial1, "timeout", 0)sioCtrl(siPortSerial1, "endOfString", 13)
常见问题分析与总结:
1.服务器与客户端打开顺序无关
2.CS8C服务器不能获知是否与模拟器客户端连接,客户端也不知
3.利用clearBuff来激活CS8C的服务器
4.运行CS8C的应用程序,客户端会主动激活。sent或者receive都是主动连接。
5.关闭CS8C的应用程序,服务器与客户端会关闭
6.有时客户端连接不到CS8C的服务器的问题分析。
程序模拟: