从串口读取数据时如何打破 WHILE 循环?
我有一个 while 循环,它使用扫描仪从串行端口读取数据,并将其读取的所有数据添加到 List
中。 我的问题是我不知道收到数据后如何退出循环?
我在循环中收到的数据是
[***、报告、***、MIB 系列、货币、计数器、---------------------------- ----, 2022年3月6日,, 01:26, 工位:1, 存款, 编号:, 2, 混合, 模式, RSD, UV, MG, IR, ---------- ----------------------, 面额, 单位, 总计, D10, 0, D, 0, D20, 0, D, 0, D50, 0, D、0、D100、0、D、0、D200、0、D、0、D500、0、D、0、D1000、0、D、0、D2000、0、D、0、D5000、0、D、 0, --------------------------------, 总计, 0, D, 0, ------- --------------------------, m3]
我使用的代码是
public static List<String> readFromPort(SerialPort serialPort) {
Scanner sc = new Scanner(serialPort.getInputStream());
List<String> line = new ArrayList<>();
while (sc.hasNextLine()) {
line.add(sc.next());
}
sc.close();
return line;
}
我尝试添加这个条件到 WHILE 循环 && !line.contains("m3")
但由于某种原因,我必须在串行设备上按两次“发送”才能接收数据。如果我使用列表中的其他字符串,它可以正常工作(我仅按“发送”一次),但随后我没有收到完整的数据。
还有什么想法可以尝试吗?
注意:我正在使用 JSerialComm 库。
编辑:列表末尾的字符串如下面的屏幕截图所示,当我发布问题时,两个无法识别的字符会被自动删除。
I have a while loop that reads from a serial port using a scanner and adds all the data it reads to a List<String>
.
My problem is I don't know how to exit the loop when I receive the data?
The data I receive in the loop is
[***, REPORT, ***, MIB-Series, Currency, Counter, --------------------------------, Mar.6,2022,, 01:26, Station:1, Deposit, No.:, 2, Mixed, Mode, RSD, UV, MG, IR, --------------------------------, DENOM, UNIT, TOTAL, D10, 0, D, 0, D20, 0, D, 0, D50, 0, D, 0, D100, 0, D, 0, D200, 0, D, 0, D500, 0, D, 0, D1000, 0, D, 0, D2000, 0, D, 0, D5000, 0, D, 0, --------------------------------, TOTAL, 0, D, 0, --------------------------------, m3]
And the code that I use is
public static List<String> readFromPort(SerialPort serialPort) {
Scanner sc = new Scanner(serialPort.getInputStream());
List<String> line = new ArrayList<>();
while (sc.hasNextLine()) {
line.add(sc.next());
}
sc.close();
return line;
}
I tried adding this condition to WHILE loop && !line.contains("m3")
but for some reason, I then have to press "Send" on the serial device twice to receive data. If I use some other string from the list it works fine(I press "Send" only once), but then I don't receive the complete data.
Any ideas what else to try?
NOTE: I'm using JSerialComm library.
EDIT: The String at the end of List is like in the screenshot below, the two unrecognized characters are deleted automatically when I post the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您知道问题屏幕截图中显示为正方形的两个字符的 unicode 代码点,请使用 文字,例如:
在上面的行中,第一个文字是
\u03A9
,它位于小写字母 m 之前,第二个字面量是\uFFFF
位于小写字母 m 之后,数字 3 之前。If you know the unicode code-point for the two characters displayed as squares in the screen capture in your question, then use a literal, for example:
In the above line, the first literal is
\u03A9
which precedes the lowercase m and the second literal is\uFFFF
which follows the lowercase m and precedes the digit 3.java 扫描仪在读取新行或跳过输入时存在几个问题
扫描仪在使用next()或nextFoo()后跳过nextLine()?
如何使用 java.util.Scanner 正确读取 System.in 中的用户输入并对其进行操作?
扫描仪跳过输入,可能存在空格?
更好但速度较慢的解决方案涉及使用 < a href="https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/io/BufferedReader.html" rel="nofollow noreferrer">BufferedReader 用于从任何输入流读取任何输入,请参阅扫描仪与 BufferedReader
另请 参阅从 @Abra 解决方案中获取积分,您应该对不可打印字符使用 uni 代码点
下面的完整代码
java Scanner has several issues when reading new lines or skipping inputs
Scanner is skipping nextLine() after using next() or nextFoo()?
How to use java.util.Scanner to correctly read user input from System.in and act on it?
Scanner skipping input, possible white space?
An better but slower solution involves using BufferedReader for reading any input from any input stream see Scanner vs. BufferedReader
Also taking points from @Abra solution you should use uni code points for non printable characters
Full code below
我使用以下代码解决了这个问题:
我在 WHILE 循环内添加了一个 IF 语句,其中包含与我之前在 WHILE 循环中使用的条件相同的条件。
这解决了我的问题,现在我只需从我的设备上单击“发送”即可获得正确的数据。
感谢大家的帮助。
I solved this using this code:
I added an IF statement inside the WHILE loop which contained the same condition as I previously used in WHILE loop.
This solved my problem, and now I'm getting the correct data with only one click on "Send" from my device.
Thanks everyone for your help.