Java从连续USB流中读取显示损坏(Java reading from continuous USB stream showing corruption)

编程入门 行业动态 更新时间:2024-10-27 09:48:42
Java从连续USB流中读取显示损坏(Java reading from continuous USB stream showing corruption)

我有一个应用程序,它将USB设备中的数据记录到本地文件中,但是接收到的数据已损坏。 数据被编码为ASCII并具有由空格分隔的4列信息。 设备上连接了2个传感器,第一列表示传感器。 数据以60Hz的频率发送,两个传感器数据同时传输。 理想的输出应该是形式

01 value1 value2 value3 02 value1 value2 value3 01 value1 value2 value3

等等...

但是前两行是正确的,但后续的备用行有很多空格(在Windows中,在Ubuntu中的gEdit中显示为\ 00s,Vim显示为^ @的序列)。 这是它在gEdit中的外观(注意在所有\ 00之后都有传感器值)

01 0.109 5.933 1.803 02 5.550 6.027 1.714 \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00 \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0001 0.109 5.933 1.803 02 5.553 6.027 1.713 \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00 \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0001 0.108 5.933 1.803

我正在测试录制5秒钟,到最后写入的行数据更少,\ 00s更多。

数据以ASCII流的形式发送,带有回车符,然后是换行符,用于分隔传感器值。 读取是在线程类的run()方法中完成的。 构造函数将PrintWriter实例化为目标输出文件,保存为变量writer 。 run方法如下所示。 我想问题在于处理消息的结束但是无法使其正常工作。

public void run() { long startTime = System.currentTimeMillis(); error = true; have1 = false; int data; try { // Send output commands to device out = new FileOutputStream(portName); out.write(0x0d); out.close(); // Open stream to read from device FileReader fr = new FileReader(portName); BufferedReader br = new BufferedReader(fr); while(System.currentTimeMillis() < startTime+ltime) { data = br.read(); if (data == -1) break; else if (data == 0x0d) { if (br.read() == 0x0a) { //System.out.println(line); writer.println(line+" "+System.currentTimeMillis()); line = ""; } } else { // Have data line += (char) data; } } br.close(); // Command to stop recording out = new FileOutputStream(portName); out.write('x'); out.close(); writer.flush(); writer.close(); } catch (IOException e) { // generated by the device e.printStackTrace(); readError = true; System.out.println("RECORDING ERROR"); } }

I've got an application which records data from a USB device to a local file, however the data being received is corrupted. The data is encoded as ASCII and has 4 columns of information separated by spaces. There are 2 sensors attached to the device, with the first column indicating the sensor. The data is sent at a frequency of 60Hz, with both sensor data being transmitted at the same time. The ideal output should be in the form

01 value1 value2 value3 02 value1 value2 value3 01 value1 value2 value3

etc...

However the first 2 lines are correct, but subsequent alternate lines have a lot of blank spaces (in Windows, in gEdit in Ubuntu it's shown as \00s and Vim shows it as sequences of ^@). Here's how it looks in gEdit (note there are sensor values after all the \00s)

01 0.109 5.933 1.803 02 5.550 6.027 1.714 \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00 \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0001 0.109 5.933 1.803 02 5.553 6.027 1.713 \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00 \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0001 0.108 5.933 1.803

I'm testing recording for 5 seconds and by the end the written lines have less data and more \00s.

The data is sent as ASCII streams with carriage return followed by line feed separating sensor values. The reading is is done in the run() method of a thread class. The constructor instantiates a PrintWriter to the destination output file, saved as variable writer. The run method is shown below. I imagine the problem is in the handling of the end of messages but can't get it working.

public void run() { long startTime = System.currentTimeMillis(); error = true; have1 = false; int data; try { // Send output commands to device out = new FileOutputStream(portName); out.write(0x0d); out.close(); // Open stream to read from device FileReader fr = new FileReader(portName); BufferedReader br = new BufferedReader(fr); while(System.currentTimeMillis() < startTime+ltime) { data = br.read(); if (data == -1) break; else if (data == 0x0d) { if (br.read() == 0x0a) { //System.out.println(line); writer.println(line+" "+System.currentTimeMillis()); line = ""; } } else { // Have data line += (char) data; } } br.close(); // Command to stop recording out = new FileOutputStream(portName); out.write('x'); out.close(); writer.flush(); writer.close(); } catch (IOException e) { // generated by the device e.printStackTrace(); readError = true; System.out.println("RECORDING ERROR"); } }

最满意答案

我想可能是你的USB设备和程序没有同步,我想到了这个原因:

没有初始同步,你假设你是同步的。 I / O操作需要很长时间(相对于毫秒)。

解决方案:

在你结束这个过程之前不要写任何文件,这会节省你的时间,而且正如我所见,时间是一个问题。 解析数据以完全删除所有损坏的数据,或者假设它是最后一个和下一个有效数据之间的算术平均值(所有这些应该在数据收集之后运行)。

我不知道你如何同步设备,试着让它等到收到一些数据。

I think it may be that your USB device and your program are not sync, and I think of this causes:

No initial sync, you're assuming you are in sync. I/O operations take much time (relative to miliseconds).

Solutions:

Do not write any file until you've ended the process, this will save you time, and as I see, time is an issue. Parse the data to completely delete all corrupted data, or assume it is the arithmetic mean between the last and next valid data (all of this should run after the data gathering).

I don't know how you could sync the device, try making it wait until some data is recived.

更多推荐

本文发布于:2023-08-04 15:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:reading   USB   Java   continuous   corruption

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!