用R读取和转换二进制原始数据(reading and converting binary raw data with R)

编程入门 行业动态 更新时间:2024-10-21 19:41:57
用R读取和转换二进制原始数据(reading and converting binary raw data with R)

我有一个包含原始/二进制数据和ascii的文件。 它包含一个时间戳和一个表示速度的无符号16位值。 我想用'R'绘制该速度,因此读取该文件。

该文件的定义是:

自午夜以来的毫秒数 - 在ascii中 +':$' + BufferStr +#13#10

然后,BufferString包含我想要提取的以下信息:

字节3:序列字(无符号) 字节4和5:速度(无符号) ...

我想生成一个包含2列时间和速度的表

我的approch是将文件作为csv读取一次以获取时间戳列。

library(bit) dataPath="rawdata.txt" #to create time col read file as csv: rwch=read.csv(dataPath, sep=":")

然后再次以二进制文件的形式读取文件。 我将文件分成27个字节,这是一行的长度。

#read binary to.read = file(path, "rb") rw=readBin(to.read, what="raw", n = 100*27, endian = "big") rws=split(rw, ceiling(seq_along(rw)/27))

但后来我卡住了。 生成的向量不包含原始数据,我无法再次拆分它以获得速度。 我已经尝试了几个函数,如hexView,readRaw,但我无法生成我的列表。

我很高兴有任何暗示

I have a file which contains raw/binary data and ascii. It contains a time stamp and a unsigned16 bit value which represents a Speed. I would like to plot that speed with 'R' and therefore read that file.

The definition of the file is:

number of milliseconds since midnight-In ascii + ':$' + BufferStr + #13#10

The BufferString then contains the following information I would like to extract:

Byte 3: Sequence word (unsigned) Byte 4 and 5: Speed (unsigned) ...

I would like to generate a table with 2 columns time and Speed

My approch was to read the file once as a csv to get the timestamp column.

library(bit) dataPath="rawdata.txt" #to create time col read file as csv: rwch=read.csv(dataPath, sep=":")

And then read the file again as binary. I split the file into 27 Byte pieces which is the length of a line.

#read binary to.read = file(path, "rb") rw=readBin(to.read, what="raw", n = 100*27, endian = "big") rws=split(rw, ceiling(seq_along(rw)/27))

But then I stuck. The resulting vector does not contain raw data and I’m not able to split it again in order to get the Speed out of it. I’ve tried several functions such as hexView ,readRaw but I’m not able to generate my list.

I'm glad for any hint

最满意答案

最好在正在处理的连接上使用readBin()而不是尝试读取整个文件(除非文件中只有一个data.type,但这里你有混合类型)。 这似乎适用于您的示例文件。

N<-28 RC<-27 secs<-numeric(N) speeds<-numeric(N) con<-file("/rawdata.txt", "rb") for(i in seq.int(N)) { print(i) secs[i] <- as.numeric(readChar(con,8)) stopifnot(readChar(con,2)==":$") #check readBin(con,"raw",3) #skip 3 bytes speeds[i] <- readBin(con, "int",1,2, signed=F) readBin(con,"raw",10) #skip 10 bytes stopifnot(readBin(con,"raw",2)==c(13,10)) #check } data.frame(secs,speeds) close(con)

It's really best to use readBin() on a connection that you are processing rather than trying to read a whole file (unless there is only one data.type in a file, but here you have mixed types here). This appears to work on your sample file.

N<-28 RC<-27 secs<-numeric(N) speeds<-numeric(N) con<-file("/rawdata.txt", "rb") for(i in seq.int(N)) { print(i) secs[i] <- as.numeric(readChar(con,8)) stopifnot(readChar(con,2)==":$") #check readBin(con,"raw",3) #skip 3 bytes speeds[i] <- readBin(con, "int",1,2, signed=F) readBin(con,"raw",10) #skip 10 bytes stopifnot(readBin(con,"raw",2)==c(13,10)) #check } data.frame(secs,speeds) close(con)

更多推荐

本文发布于:2023-07-07 21:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068518.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:原始数据   reading   converting   data   raw

发布评论

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

>www.elefans.com

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