Opentcs 夜光开发手册(六)

夜光序言:

 

嗯唔:命 是用来保护人的 而不是用来伤害人的~~

 

正文:

 

opentcs,最为关键之处就是构建客户端,涉及PLC通讯协议

package china.opentcs.test;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**

 *
 *
 * author:  Genius Team
 *  
 */
public class CapitalizeServer {

    /**

     *
     *
     * author: Genius  Team
     *
     *
     */
    public static void main(String[] args) throws Exception {
        System.out.println("The capitalization server is running.");
        int clientNumber = 0;
        ServerSocket listener = new ServerSocket(1314);
        try {
            while (true) {
                new Capitalizer(listener.accept(), clientNumber++).start();
            }
        } finally {
            listener.close();
        }
    }

    /**
     * A private thread to handle capitalization requests on a particular
     * socket.  The client terminates the dialogue by sending a single line
     * containing only a period.
     */
    private static class Capitalizer extends Thread {
        private Socket socket;
        private int clientNumber;
        private static boolean commandExcecuted;

        public Capitalizer(Socket socket, int clientNumber) {
            this.socket = socket;
            this.clientNumber = clientNumber;
            log("New connection with client# " + clientNumber + " at " + socket);
        }

     
        public void run() {
            try {

                // Decorate the streams so we can send characters
                // and not just bytes.  Ensure output is flushed
                // after every newline.
                BufferedReader into = new BufferedReader(
                        new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                Scanner sc = new Scanner(System.in);
                out.print("Point-0005<EOF>");
                while (true) {
                    String input = into.readLine();

                    if (input.equals("<EOF>")) {
                        System.out.println("Please Provide The next Position?");
                        String point = sc.next();
                        out.print(point+"<EOF>");
                        out.flush();
                    }

                    System.out.println(input);

                }


            } catch (IOException e) {
                log("Error handling client# " + clientNumber + ": " + e);
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    log("Couldn't close a socket, what's going on?");
                }
                log("Connection with client# " + clientNumber + " closed");
            }
        }

        /**
       
         */
        private void log(String message) {
            System.out.println(message);
        }
    }
}

 

package china.opentcs.test;



import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

/**

 *
 * author:
 */
public class CapitalizeClient
        extends Thread {

    private boolean ct = false;

    private String ipaddress;

    private Socket sock;

    // reading from keyboard (keyRead object)
    private BufferedReader keyRead;
    // sending to client (pwrite object)
    private OutputStream ostream;

    private PrintWriter pwrite;

    // receiving from server ( receiveRead  object)
    private InputStream istream;
    protected DataInputStream dataInputStream;  // InputStream to receive data
    private BufferedReader receiveRead;
    private String currenposition;

    public CapitalizeClient(String ip)
            throws IOException {

        this.ipaddress = ip;
        this.sock = new Socket(ipaddress, 11000);
        this.keyRead = new BufferedReader(new InputStreamReader(System.in));
        this.ostream = this.sock.getOutputStream();
        this.pwrite = new PrintWriter(ostream, true);
        this.istream = this.sock.getInputStream();
        this.dataInputStream = new DataInputStream(istream);
        this.receiveRead = new BufferedReader(new InputStreamReader(istream));

    }

    @Override
    public void run() {

        while (!ct) {

        }

    }

    public void disconnect()
            throws IOException {

        System.out.println("org.opentcs.virtualvehicle.CapitalizeClient.disconnect()");
        try {
            this.ct = true;

        }
        finally {
            this.istream.close();
            this.ostream.close();
            this.pwrite.close();
            this.sock.close();
        }

    }

    public void sendMessage(String message) {

        System.out.println("org.opentcs.virtualvehicle.CapitalizeClient.send");
        this.pwrite.println(message);

    }

    public String getPosition()
            throws IOException {

        this.pwrite.println("<EOF>");

        byte[] bytedata = new byte[1024];
        int character;
        StringBuilder data = new StringBuilder();

        while ((character = istream.read()) != -1) {

            if ((char) character == '<') {

                if ((char) istream.read() == 'E') {

                    if ((char) istream.read() == 'O') {

                        if ((char) istream.read() == 'F') {

                            if ((char) istream.read() == '>') {

                                break;

                            }

                        }

                    }

                }

            }
            data.append((char) character);

        }

        System.out.println("This is received : " + ":::::::::" + data.toString());

        String position = data.toString();
        this.pwrite.flush();

        return position;

    }

    public Boolean readCommand()
            throws IOException {

        BufferedReader reader = this.receiveRead;

        if (reader.readLine() != null) {

            System.out.println(reader.readLine().toLowerCase());
            if (reader.readLine().toLowerCase() == "yes") {

                return true;

            }
        }

        return false;

    }

    public static void main(String[] args)
            throws Exception {
        System.out.println("The capitalization Client  is running.");

        CapitalizeClient c = new CapitalizeClient("192.168.1.40");

        c.start();
        c.getPosition();

    }

}

 

 

 

 

 

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
opentcs通信涉及使用TCP/IP协议进行通信。具体实现流程如下: 1. 主机(host)建立一个新的TCP/IP连接,呼叫TCS。 2. 主机发送一个简单的报文(XML telegram),描述需要被创建的订单。 3. 主机关闭自己的TCP/IP连接的output stream流,让TCS的kernel知道数据已经发送完毕。 4. TCS解析报文(来自主机的),创建指定格式的订单并激活订单。 5. TCS发送一个XML telegram,确认来自主机的电报XML被处理了。 6. TCS关闭TCP/IP连接。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【opentcs】模拟真实车辆和opentcs通信的简单ui界面,opentcs与实物车辆对接步骤一](https://blog.csdn.net/qq_20826539/article/details/131933460)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [夜光精讲 Opentcs 通信系统](https://blog.csdn.net/weixin_41987706/article/details/90374918)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值