Commit f2ec23a6 authored by qizhiqiang's avatar qizhiqiang

rs-232发送消息接口

parent e882aea1
......@@ -5,6 +5,7 @@ import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
......@@ -12,6 +13,8 @@ import gnu.io.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import static com.mushiny.metage.util.HexadecimalUtil.hexStringToByteArray;
/**
* @version 1.0
* @author: Gy
......@@ -24,7 +27,7 @@ public class RxtxUtils extends Thread implements SerialPortEventListener {
static Enumeration<?> portList; // 有效连接上的端口的枚举
InputStream inputStream; // 从串口来的输入流
public static OutputStream outputStream;// 向串口输出的流
SerialPort serialPort; // 串口的引用
static SerialPort serialPort; // 串口的引用
// 堵塞队列用来存放读到的数据
private BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>();
//当前接收到的重量信息
......@@ -149,6 +152,25 @@ public class RxtxUtils extends Thread implements SerialPortEventListener {
}
}
public static void sendToPort(String data) {
OutputStream out = null;
try {
out = serialPort.getOutputStream();
out.write(data.getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void sendDate(Double weightings) throws IOException{
//客户端发送数据,首先用无参构造方法获得DatagramSocket对象。
DatagramSocket send = new DatagramSocket();
......
package com.mushiny.metage.webSocket;
import com.mushiny.metage.util.RxtxUtils;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.CopyOnWriteArraySet;
import static com.mushiny.metage.util.HexadecimalUtil.hexStringToByteArray;
@ServerEndpoint(value = "/websocket")
@Component
public class WebSocket {
......@@ -22,6 +25,9 @@ public class WebSocket {
//加入set中
webSocketSet.add(this);
System.out.println("Establishing a connection:"+webSocketSet.size());
//往串口发送数据
RxtxUtils.sendToPort(("T"+"\r\n"));
}
//给所有当前连接的用户发送消息
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment