Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
GnssServer
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
zhushanglei
GnssServer
Commits
30167451
Commit
30167451
authored
Mar 31, 2022
by
zhushanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
128f3a97
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
15 deletions
+12
-15
TcpServerHandler.java
src/main/java/server/TcpServerHandler.java
+12
-15
No files found.
src/main/java/server/TcpServerHandler.java
View file @
30167451
...
...
@@ -25,20 +25,16 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> {
System
.
out
.
println
(
"**接收到的基站数据<<start:"
+
msg
.
toString
()
+
">>end"
);
String
writeData
=
msg
.
toString
();
//要发送的字符串
byte
[]
bytes
=
writeData
.
getBytes
();
//将字符串转换为字节数组
writeComm
(
serialPort
,
bytes
);
Thread
.
sleep
(
20
);
//休眠0.02秒,等待下位机传送数据到串口。如果不休眠,直接再次使用port.bytesAvailable()函数会因为下位机还没有返回数据而返回-1,并跳出循环导致数据没读完。休眠时间可以自行调试,时间越长,单次读取到的数据越多。
int
flag
=
writeComm
(
serialPort
,
bytes
);
if
(-
2
==
flag
){
openComm
();
}
Thread
.
sleep
(
5000
);
//休眠0.02秒,等待下位机传送数据到串口。如果不休眠,直接再次使用port.bytesAvailable()函数会因为下位机还没有返回数据而返回-1,并跳出循环导致数据没读完。休眠时间可以自行调试,时间越长,单次读取到的数据越多。
String
result
=
readComm
(
serialPort
);
if
(
null
==
result
){
openComm
();
}
System
.
out
.
println
(
"**COM口读出数据<<start:"
+
result
+
">>end"
);
// if (msg.equals("01")){
// System.out.println("receive command :" + msg);
// ctx.channel().writeAndFlush("command 01 executed!\n");
// }else if(msg.equals("02")){
// System.out.println("receive command :" + msg);
// ctx.channel().writeAndFlush("command 02 executed!\n");
// }else {
// System.out.println("unknown command:" + msg);
// ctx.channel().writeAndFlush("unknown command!\n");
// }
}
private
static
SerialPort
serialPort
;
...
...
@@ -66,10 +62,10 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> {
return
serialPort
;
}
private
void
writeComm
(
SerialPort
serialPort
,
byte
[]
data
)
throws
InterruptedException
{
private
int
writeComm
(
SerialPort
serialPort
,
byte
[]
data
)
throws
InterruptedException
{
if
(
serialPort
==
null
||
!
serialPort
.
isOpen
()){
System
.
out
.
println
(
"COM口未打开"
);
return
;
return
-
2
;
}
int
result
=
serialPort
.
writeBytes
(
data
,
data
.
length
);
//将字节数组全部写入串口
if
(
result
!=
-
1
){
...
...
@@ -77,6 +73,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> {
}
else
{
System
.
out
.
println
(
"COM口写数据失败"
);
}
return
result
;
}
private
String
readComm
(
SerialPort
serialPort
)
{
...
...
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