0


SocketTool、串口调试助手、MQTT中间件基础

一、SocketTool

1、TCP 通信测试:

1)创建 TCP Server

2)创建 TCP Client

  1. 连接 Socket

4)数据收发

在TCP Server发送数据12345

在 TCP Client 端的 Socket 即可收到数据12345

  1. UDP 通信测试:

1)分别创建 UDP Server 和 UDP Client

2)先由 UDP Client 发送数据

UDP Servers 收到数据才能看到对方端口

在 UDP Server 收到过 UDP Client 的数据后,其对方 IP 地址和 UDP 端口均可确定 下来,然后 UDP Server 也可以向 UDP Client 发送数据了

二、串口通信

先创建两个虚拟串口,这里用到了Configure Virtual Serial Port Driver

然后打开串口调试工具,调整串口设置后打开串口COM2

接着在代码里开启另一个串口CMO1

  1. import com.fazecast.jSerialComm.SerialPort;
  2. import java.util.Scanner;
  3. public class SerialCommunicationExample {
  4. public static void main(String[] args) {
  5. // 尝试打开 COM1 端口,你可以根据需要修改这个值
  6. SerialPort serialPort = SerialPort.getCommPort("COM1");
  7. if (serialPort.openPort()) {
  8. try {
  9. // 设置串口参数,这些值应与你的设备匹配
  10. serialPort.setComPortParameters(9600, 8, 1, SerialPort.NO_PARITY);
  11. serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 2000, 0);
  12. // 获取用户输入的消息
  13. Scanner scanner = new Scanner(System.in);
  14. System.out.print("Enter message to send: ");
  15. String messageToSend = scanner.nextLine();
  16. // 发送消息
  17. serialPort.writeBytes(messageToSend.getBytes(), messageToSend.length());
  18. // 等待接收到回复(注意:这里可能需要更复杂的逻辑来处理接收数据)
  19. byte[] buffer = new byte[1024];
  20. int numRead;
  21. StringBuilder receivedMessage = new StringBuilder();
  22. while ((numRead = serialPort.readBytes(buffer, buffer.length)) > 0) {
  23. receivedMessage.append(new String(buffer, 0, numRead));
  24. }
  25. System.out.println("Received message: " + receivedMessage);
  26. } catch (Exception ex) {
  27. System.out.println("Error: " + ex.getMessage());
  28. } finally {
  29. // 关闭串口
  30. if (serialPort.isOpen()) {
  31. serialPort.closePort();
  32. }
  33. }
  34. } else {
  35. System.out.println("Error: Could not open the serial port.");
  36. }
  37. }
  38. }

在串口工具COM2发送数据,COM1能收到,COM1发送的在工具里也能接收到

三、MQTT中间件

先启动mqtt服务

然后订阅和推送

  1. import org.eclipse.paho.client.mqttv3.*;
  2. import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
  3. public class SubscribeSample {
  4. public static void main(String[] args) {
  5. String broker = "tcp://localhost:1883";
  6. String topic = "mqtt/test";
  7. String username = "emqx";
  8. String password = "public";
  9. String clientid = "subscribe_client";
  10. int qos = 0;
  11. try {
  12. MqttClient client = new MqttClient(broker, clientid, new MemoryPersistence());
  13. // 连接参数
  14. MqttConnectOptions options = new MqttConnectOptions();
  15. // options.setUserName(username);
  16. // options.setPassword(password.toCharArray());
  17. options.setConnectionTimeout(60);
  18. options.setKeepAliveInterval(60);
  19. // 设置回调
  20. client.setCallback(new MqttCallback() {
  21. public void connectionLost(Throwable cause) {
  22. System.out.println("connectionLost: " + cause.getMessage());
  23. }
  24. public void messageArrived(String topic, MqttMessage message) {
  25. System.out.println("topic: " + topic);
  26. System.out.println("Qos: " + message.getQos());
  27. System.out.println("message content: " + new String(message.getPayload()));
  28. }
  29. public void deliveryComplete(IMqttDeliveryToken token) {
  30. System.out.println("deliveryComplete---------" + token.isComplete());
  31. }
  32. });
  33. client.connect(options);
  34. client.subscribe(topic, qos);
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  1. import org.eclipse.paho.client.mqttv3.MqttClient;
  2. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  3. import org.eclipse.paho.client.mqttv3.MqttException;
  4. import org.eclipse.paho.client.mqttv3.MqttMessage;
  5. import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
  6. public class PublishSample {
  7. public static void main(String[] args) {
  8. String broker = "tcp://localhost:1883";
  9. String topic = "mqtt/test";
  10. String username = "emqx";
  11. String password = "public";
  12. String clientid = "publish_client";
  13. String content = "Hello MQTT";
  14. int qos = 0;
  15. try {
  16. MqttClient client = new MqttClient(broker, clientid, new MemoryPersistence());
  17. // 连接参数
  18. MqttConnectOptions options = new MqttConnectOptions();
  19. // 设置用户名和密码
  20. // options.setUserName(username);
  21. // options.setPassword(password.toCharArray());
  22. options.setConnectionTimeout(60);
  23. options.setKeepAliveInterval(60);
  24. // 连接
  25. client.connect(options);
  26. // 创建消息并设置 QoS
  27. MqttMessage message = new MqttMessage(content.getBytes());
  28. message.setQos(qos);
  29. // 发布消息
  30. client.publish(topic, message);
  31. System.out.println("Message published");
  32. System.out.println("topic: " + topic);
  33. System.out.println("message content: " + content);
  34. // 关闭连接
  35. client.disconnect();
  36. // 关闭客户端
  37. client.close();
  38. } catch (MqttException e) {
  39. throw new RuntimeException(e);
  40. }
  41. }
  42. }

标签: 中间件 网络

本文转载自: https://blog.csdn.net/weixin_62030612/article/details/139529281
版权归原作者 Jq_xie 所有, 如有侵权,请联系我们删除。

“SocketTool、串口调试助手、MQTT中间件基础”的评论:

还没有评论