Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading and converting binary raw data with R

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

like image 260
Sami Avatar asked Oct 21 '22 09:10

Sami


1 Answers

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)
like image 95
MrFlick Avatar answered Nov 03 '22 07:11

MrFlick