Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
metage
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
guoyu
metage
Commits
f2ec23a6
Commit
f2ec23a6
authored
Apr 13, 2023
by
qizhiqiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rs-232发送消息接口
parent
e882aea1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
1 deletion
+29
-1
RxtxUtils.java
src/main/java/com/mushiny/metage/util/RxtxUtils.java
+23
-1
WebSocket.java
src/main/java/com/mushiny/metage/webSocket/WebSocket.java
+6
-0
No files found.
src/main/java/com/mushiny/metage/util/RxtxUtils.java
View file @
f2ec23a6
...
...
@@ -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
();
...
...
src/main/java/com/mushiny/metage/webSocket/WebSocket.java
View file @
f2ec23a6
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"
));
}
//给所有当前连接的用户发送消息
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment