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
ece83525
Commit
ece83525
authored
Apr 06, 2023
by
qizhiqiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
称重小程序开发
parent
fbedd15c
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
133 additions
and
8 deletions
+133
-8
pom.xml
pom.xml
+11
-0
RxtxUtils.java
src/main/java/com/mushiny/metage/util/RxtxUtils.java
+12
-7
ServerStart.java
src/main/java/com/mushiny/metage/util/ServerStart.java
+13
-1
WebSocket.java
src/main/java/com/mushiny/metage/webSocket/WebSocket.java
+61
-0
WebSocketConfig.java
...in/java/com/mushiny/metage/webSocket/WebSocketConfig.java
+16
-0
application-dev.properties
src/main/resources/application-dev.properties
+11
-0
application.properties
src/main/resources/application.properties
+9
-0
No files found.
pom.xml
View file @
ece83525
...
...
@@ -29,6 +29,17 @@
<artifactId>
spring-boot-starter-tomcat
</artifactId>
<scope>
provided
</scope>
</dependency>
<!-- websocket -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-websocket
</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-devtools
</artifactId>
</dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
...
...
src/main/java/com/mushiny/metage/util/RxtxUtils.java
View file @
ece83525
package
com
.
mushiny
.
metage
.
util
;
import
com.mushiny.metage.webSocket.WebSocket
;
import
gnu.io.SerialPortEvent
;
import
gnu.io.SerialPortEventListener
;
import
java.io.*
;
...
...
@@ -8,6 +9,8 @@ import java.util.*;
import
java.util.concurrent.BlockingQueue
;
import
java.util.concurrent.LinkedBlockingQueue
;
import
gnu.io.*
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
/**
* @version 1.0
...
...
@@ -69,7 +72,7 @@ public class RxtxUtils extends Thread implements SerialPortEventListener {
*
* @return 返回1 表示端口打开成功,返回 0表示端口打开失败
*/
public
int
startComPort
(
String
portName
)
{
public
int
startComPort
(
String
portName
,
int
bitRate
,
int
dataBits
,
int
stopBit
,
int
parityBit
)
{
// 通过串口通信管理类获得当前连接上的串口列表
portList
=
CommPortIdentifier
.
getPortIdentifiers
();
...
...
@@ -112,9 +115,9 @@ public class RxtxUtils extends Thread implements SerialPortEventListener {
// 设置串口的一些读写参数
try
{
// 比特率、数据位、停止位、奇偶校验位
serialPort
.
setSerialPortParams
(
9600
,
SerialPort
.
DATABITS_8
,
SerialPort
.
STOPBITS_1
,
SerialPort
.
PARITY_NONE
);
serialPort
.
setSerialPortParams
(
bitRate
,
dataBits
,
stopBit
,
parityBit
);
}
catch
(
UnsupportedCommOperationException
e
)
{
e
.
printStackTrace
();
return
0
;
...
...
@@ -136,10 +139,12 @@ public class RxtxUtils extends Thread implements SerialPortEventListener {
if
(
msgQueue
.
size
()
>
0
)
{
weightings
=
Double
.
parseDouble
(
msgQueue
.
take
());
System
.
out
.
println
(
weightings
);
sendDate
(
weightings
);
WebSocket
webSocket
=
new
WebSocket
();
webSocket
.
sendAllToUserMessage
(
String
.
valueOf
(
weightings
));
// sendDate(weightings);
}
}
}
catch
(
InterruptedException
|
IO
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
...
...
@@ -154,7 +159,7 @@ public class RxtxUtils extends Thread implements SerialPortEventListener {
//我们创建DatagramPacket打包对象对其进行打包
//四个参数分别为:数据,数据长度,主机地址,端口
DatagramPacket
dataPacket
=
new
DatagramPacket
(
sendData
,
sendData
.
length
,
InetAddress
.
getByName
(
"192.1
68.1.15
"
),
10086
);
sendData
,
sendData
.
length
,
InetAddress
.
getByName
(
"192.1
28.2.208
"
),
10086
);
//调用DatagramSocket对象的send()方法进行数据发送
send
.
send
(
dataPacket
);
...
...
src/main/java/com/mushiny/metage/util/ServerStart.java
View file @
ece83525
package
com
.
mushiny
.
metage
.
util
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.CommandLineRunner
;
import
org.springframework.stereotype.Component
;
...
...
@@ -11,10 +12,21 @@ import org.springframework.stereotype.Component;
@Component
public
class
ServerStart
implements
CommandLineRunner
{
@Value
(
"${int.server.pordName}"
)
private
String
portName
;
@Value
(
"${int.server.bitRate}"
)
private
int
bitRate
;
@Value
(
"${int.server.dataBits}"
)
private
int
dataBits
;
@Value
(
"${int.server.stopBit}"
)
private
int
stopBit
;
@Value
(
"${int.server.parityBit}"
)
private
int
parityBit
;
@Override
public
void
run
(
String
...
args
)
throws
Exception
{
RxtxUtils
cRead
=
new
RxtxUtils
();
int
i
=
cRead
.
startComPort
(
"COM2"
);
int
i
=
cRead
.
startComPort
(
portName
,
bitRate
,
dataBits
,
stopBit
,
parityBit
);
if
(
i
==
1
)
{
cRead
.
start
();
System
.
out
.
println
(
"打开端口"
);
...
...
src/main/java/com/mushiny/metage/webSocket/WebSocket.java
0 → 100644
View file @
ece83525
package
com
.
mushiny
.
metage
.
webSocket
;
import
org.springframework.stereotype.Component
;
import
javax.websocket.*
;
import
javax.websocket.server.ServerEndpoint
;
import
java.util.concurrent.CopyOnWriteArraySet
;
@ServerEndpoint
(
value
=
"/websocket"
)
@Component
public
class
WebSocket
{
private
static
CopyOnWriteArraySet
<
WebSocket
>
webSocketSet
=
new
CopyOnWriteArraySet
<
WebSocket
>();
//与某个客户端的连接回话,需要通过它来给客户端发送数据
private
Session
session
;
@OnOpen
public
void
onOpen
(
Session
session
){
this
.
session
=
session
;
//加入set中
webSocketSet
.
add
(
this
);
System
.
out
.
println
(
"建立连接:"
+
webSocketSet
.
size
());
}
//给所有当前连接的用户发送消息
public
void
sendAllToUserMessage
(
String
message
){
try
{
if
(
webSocketSet
.
size
()
!=
0
){
for
(
WebSocket
item:
webSocketSet
)
{
item
.
session
.
getBasicRemote
().
sendText
(
message
);
}
}
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
}
//当浏览器关闭触发该事件
@OnClose
public
void
onClose
(
Session
session
){
webSocketSet
.
remove
(
this
);
}
//接收前端发送的消息
@OnMessage
public
void
onMessage
(
String
params
,
Session
session
){
//params就是发送的消息根据自己的业务进行操作就可以了
System
.
out
.
println
(
params
);
}
//发送错误时调用
@OnError
public
void
onError
(
Session
session
,
Throwable
error
){
System
.
out
.
println
(
"webSocket发送错误!!!"
);
error
.
printStackTrace
();
}
}
src/main/java/com/mushiny/metage/webSocket/WebSocketConfig.java
0 → 100644
View file @
ece83525
package
com
.
mushiny
.
metage
.
webSocket
;
import
org.springframework.beans.factory.annotation.Configurable
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.web.socket.server.standard.ServerEndpointExporter
;
@Configurable
public
class
WebSocketConfig
{
//注入一个ServerEndpointExporter,该Bean会自动注册@ServerEndpoint注解申明的websocket endpoint
@Bean
public
ServerEndpointExporter
serverEndpointExporter
(){
return
new
ServerEndpointExporter
();
}
}
src/main/resources/application-dev.properties
0 → 100644
View file @
ece83525
# 串口
int.server.pordName
=
COM2
# 比特率
int.server.bitRate
=
9600
# 数据位
int.server.dataBits
=
8
# 停止位
int.server.stopBit
=
1
# 奇偶校验位
int.server.parityBit
=
0
\ No newline at end of file
src/main/resources/application.properties
View file @
ece83525
spring.profiles.active
=
dev
#关闭缓存,即时刷新
spring.thymeleaf.cache
=
true
#热部署生效
spring.devtools.restart.enabled
=
true
#设置重启的目录,添加那个目录的文件需要的restart
spring.devtools.restart.additional-paths
=
src/main/java
\ No newline at end of file
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