VARsocketdevsockte1;VARstringsendStr;VARstringreceiveStr;VARnumreceiveStrNum;VARstringstrPart;VARstringstrVar;VARstringData{20};VARboolOK;PERSnumputX;PERSnumputY;PERSnumputZ;接收指令
SocketReceivesockte1\str:=receiveStr\NoRecBytes:=receiveStrNum;解析程序
j:=1;strVar:="";FORiFROM1TOreceiveStrNumDOstrPart:=StrPart(receiveStr,i,1);IFstrPart";"THENstrVar:=strVar+strPart;strPart:="";ELSEIFstrPart=";"THENData{j}:=strVar;strVar:="";strPart:="";j:=j+1;ENDIFENDFOROK:=StrToVal(Data{1},pickX);OK:=StrToVal(Data{2},pickY);OK:=StrToVal(Data{3},pickZ);发送指令
sendStr:="1";SocketSendsockte1\str:=sendStr四、字节类型的发送接受,与字符串类似,只不过是建了一个byte类型的数组,用于存放数据。因最多发送接收为个byte,那就声明一个byte类型的长度的一维数组。声明变量
VARsocketdevsockte2;VARbytereceiveByte{};VARstringsendStr;VARnumreceiveByteNum;VARstringstrPart{};VARstringstrVar:="";VARstringData{};VARboolOK;接收指令
SocketReceivesockte2\Data:=receiveByte\NoRecBytes:=receiveByteNum;解析程序
j:=1;strVar:="";FORstrPFROM1TODOstrPart{strP}:="";ENDFORFORiFROM1TOreceiveByteNumDOstrPart{i}:=ByteToStr(receiveByte{i}\Char);IFstrPart{i}";"THENstrVar:=strVar+strPart{i};ELSEIFstrPart{i}=";"THENData{j}:=strVar;strVar:="";j:=j+1;ENDIFENDFOR发送指令
sendStr:="1";SocketSendsockte1\str:=sendStr五、二进制字节的发送接收,ABB提供了丰富的解析指令,UnpackRawBytes解二进制字节、CopyRawBytes复制二进制字节、PackRawBytes把变量转为二进制字节等等。但有个非常要注意点,ABB为了数据能可靠的得到接收内容,有一个“分包”发送机制:先发4个字节,再发剩余字节。此处在ABB机器人端表现为2个receive指令。我们在开发上位机程序时,肯定想方设法满足这个“分包”,那就掉到“坑”里了,其实在实际测试中发现,上位机1包发所有的也可以,只要有4个字节的包头就行。声明变量
VARsocketdevsockte3;VARrawbytesreceiveRawData;VARrawbytessendRawData;VARnumbytes_rcvd;VARnummsg_length;VARnumpickX;VARreturn_value接收指令
SocketReceivesockte3,\RawData:=receiveRawData,\ReadNoOfBytes:=4;UnpackRawBytesreceiveRawData,1,msg_length,\IntX:=UDINT;SocketReceivesockte3,\RawData:receiveRawData,\ReadNoOfBytes:=msg_length,\NoRecBytes:=bytes_rcvd;IF(bytes_rcvdmsg_length)THENErrWrite\W,"ROSSocketRecvFailed","Didnotreceiveexpected#ofbytes.",\RL2:="Expected:"+ValToStr(msg_length),\RL3:="Received:"+ValToStr(bytes_rcvd);RETURN;ENDIFUnpackRawBytesreceiveRawData,1,pickX,\IntX:=DINT;发送指令
PackRawBytesreturn_value,sendRawData,1,\IntX:=DINT;SocketSendsockte3,\RawData:=sendRawData;六、此文献给对ABBsocket有技术需求的朋友。预览时标签不可点收录于话题#个上一篇下一篇