繁体   English   中英

如何使用Java套接字将基于tcp / ip的消息转换为(copco)开放协议?

[英]How to convert message to (copco) open protocol based on tcp/ip with Java sockets?

我正在尝试开发扭矩软件。 我通过java套接字连接到设备,但是我不知道如何用扭矩建立会话。 建立沟通步骤:

我(客户):发送-MID 0001

扭矩(服务器):发送-MID 0002

为此,我必须将消息MID 0001转换为OPEN协议,根据文档,结果是:002000010000000000000当我发送此消息时,服务器不应答。
也许我的消息没有转换为开放协议好吗?

在此处输入图片说明 在此处输入图片说明

我正在尝试以字节或字符串形式发送消息。 有人知道如何使用Alpha开放协议和Java与Stanley扭矩建立连接吗?

我的客户代码:

     public class MyClientSocket {
        private static Socket socket;

        public static void main(String args[]) {


            try {
                String host = "192.168.1.15";
                int port = 4545;
                InetAddress address = InetAddress.getByName(host);
                socket = new Socket(address, port);

                //Send the message to the server
                DataOutputStream os = new DataOutputStream(socket.getOutputStream());

                String sendMessage = "002000010000000000000";

                byte[] bytes = sendMessage.getBytes(StandardCharsets.US_ASCII);


                os.write(bytes);
//              os.writeUTF(sendMessage);

                System.out.println("Message sent to the server : " + sendMessage);

                //Get the return message from the server
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String message = br.readLine();
                System.out.println("Message received from the server : " + message);


            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    }

这是输出:

    Message sent to the server : 002000010000000000000
Message received from the server : null

Process finished with exit code 0

请帮我。

我自己找到了解决方案。 消息中的最后一个零是结束消息零,我不得不这样写消息:

"00200001000000000000\0"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM