简体   繁体   English

Java TCP套接字-向控制器发送命令

[英]Java TCP Socket - Send command to controller

As you understand from the title i have a controller to which i have connect succesfully over a tcp socket. 从标题可以理解,我有一个控制器 ,通过tcp套接字已成功连接到该控制器 From the manual of this controller i read that i have to send a command like that: 从该控制器的手册中 ,我读到我必须发送如下命令:
"A5 A5 A5 A5 A5 A5 A5 A5 01 00 00 80 00 00 00 00 00 00 FE 02 05 00 A2 00 01 00 00 68 F8 5A" “ A5 A5 A5 A5 A5 A5 A5 A5 A5 01 00 00 80 00 00 00 00 00 00 FE 02 05 00 A2 00 01 00 00 68 F8 5A”

Now i'm tottaly confused about that command. 现在,我对该命令一无所知。
How can i send this command through the socket? 如何通过套接字发送此命令?

* The manual mentions only rs232 and gprs communication the one i have communicates through wifi so i opened a tcp socket * 手册仅提及rs232和gprs通信,我通过wifi进行通信,因此我打开了一个TCP套接字

A very basic approach would be this: 一个非常基本的方法是这样的:

int targetPort = 5005;
String targetHost = "targetHost";
byte[] command = new byte[] { (byte) 0xa5, (byte) 0xa5, ... };
try (Socket socket = new Socket(targetHost, targetPort);
        OutputStream rawOutputStream = socket.getOutputStream()) {
    rawOutputStream.write(command);
    rawOutputStream.flush();
}

This merely opens up a socket to your controller, sends the command and that's it. 这只是为您的控制器打开一个套接字,发送命令,仅此而已。 For productive use, you probably would want to keep the socket around, read the answer, maybe wrap the socket's output stream with a BufferedOutputStream etc. 为了进行生产性使用,您可能希望保留套接字,阅读答案,也许用BufferedOutputStream包装套接字的输出流等。

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

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