RTDE.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package at.acdp.urweb.web;
  2. import java.io.*;
  3. import java.net.Socket;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. public class RTDE implements Runnable {
  8. private final String ip;
  9. private final int port;
  10. private volatile boolean _running=true;
  11. private Socket rt;
  12. private OutputStream os;
  13. private DataInputStream in;
  14. public RTDE(String ip, int port) {
  15. this.ip=ip;
  16. this.port=port;
  17. }
  18. private void connect() throws IOException {
  19. this.rt = new Socket(ip, port);
  20. this.os=rt.getOutputStream();
  21. this.in=new DataInputStream(rt.getInputStream());
  22. readVersionMessage();
  23. }
  24. private void readVersionMessage() throws IOException {
  25. int msgSize=in.readInt();
  26. int msgType=in.readByte()&0xff;
  27. long tstamp=in.readLong();
  28. byte source=in.readByte();
  29. byte robotMsgType=in.readByte();
  30. byte projectNameSize=in.readByte();
  31. byte[] name=new byte[projectNameSize];
  32. in.read(name);
  33. String nameS=new String(name);
  34. int majorVersion=in.readByte() & 0xff;
  35. int minorVersion=in.readByte() & 0xff;
  36. int bugFixVersion=in.readInt();
  37. int buildNumber=in.readInt();
  38. byte[] buildDateB=new byte[msgSize-(16+projectNameSize+10)];
  39. in.read(buildDateB);
  40. String buildDate=new String(buildDateB);
  41. }
  42. private void writeCmd(String cmd) {
  43. try {
  44. System.out.println("send cmd:"+cmd);
  45. os.write(cmd.getBytes(StandardCharsets.UTF_8));
  46. int length=in.readInt();
  47. System.out.println("len: "+length);
  48. byte[] buf=new byte[length];
  49. //in.read(buf);
  50. int mType = in.readByte() & 0xff;
  51. System.out.println("mtype: "+mType);
  52. while(true) {
  53. int sublength=in.readInt();
  54. System.out.println("sublength: " + sublength);
  55. int subType = in.readByte();
  56. System.out.println("subtype: "+subType);
  57. byte[] res=new byte[sublength-5];
  58. in.read(res);
  59. }
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. @Override
  65. public void run() {
  66. try {
  67. connect();
  68. writeCmd("set_digital_out(1,True)\n");
  69. writeCmd("set_digital_out(2,True)\n");
  70. writeCmd("movej([-1.95,-1.58,-1.16,-1.15,-1.55,1.18], a=1.0, v=0.1)\n");
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. }