0

I tried to read information from a "character device file". This file is used by a sensor which stores data with a high frequency. I looked at some solutions but they didn't work. Could someone briefly explain to me how to solve this and also which libraries I have to import.

import os
import numpy

data = numpyndarray(100)
#data = "xxxx"
dev = os.open("/dev/rtlightsensor0", os.0_RDWR)
os.write(dev,0,os.SEEK_SET)
print os.read(dev,16)

OSError: Invalid argument [Errno 22] Edit, i changed the data with a buffer, but still same problem.

ser co
  • 77
  • 1
  • 1
  • 6
  • Can you give some sample input , output and what you have tried so far? – nitin3685 Nov 20 '19 at 02:01
  • https://stackoverflow.com/questions/21424393/how-do-i-perform-low-level-i-o-on-a-linux-device-file-in-python. Does this link help? – nitin3685 Nov 20 '19 at 02:03
  • but which lib do i need for the write and read function? – ser co Nov 20 '19 at 02:04
  • Check the second answer in the link provided. They seem to be doing it with `import os`. I can't comment any further without more info as to what kind of file it is and what you are expecting. – nitin3685 Nov 20 '19 at 02:07
  • 1
    @nitin3685 i i edit it again, you mean input = file input? I only know there are 4 Numbers but dont have any other information. – ser co Nov 20 '19 at 02:08
  • @nitin3685 im not a expert but properties from file "File type: character device" i if i open it, its always shows me 4 Numbers like "34 12 4 22" these are sensor data and i need them for a nother Programm. But i dont know how to copy them. I tryd the second solution but got the error "OSError: [Errno 22] Invalid argument. This error describes a wrong path but mine should be right. – ser co Nov 20 '19 at 02:48
  • This is pretty unrelated but it finds for file size, try this out with your own file, [file size](https://stackoverflow.com/a/2774125/12128167). I still do not understand why you have to os.write before read. – Sin Han Jinn Nov 20 '19 at 03:33
  • i used write cause ppl told me its the way haha, but yes i dont understand why i should use write and not read. I tryd to use write and my own file size but it doesnt work with the same error – ser co Nov 20 '19 at 05:21

1 Answers1

0

You code now is just fine. It is os.O_RDWR not os.0_RDWR

import os

dev = os.open("/dev/rtlightsensor0", os.O_RDWR)
print os.read(dev,16)

Since i dont have access to your device i cant read your char device. But on my machine,the following works on a different char device.

import os

dev = os.open("/dev/urandom", os.O_RDWR)
print(os.read(dev, 16))

Output:

b'\xca\xbe\x90\xd6\x08\xdd\x918\xbc_\x85\\6J%e'
nitin3685
  • 825
  • 1
  • 9
  • 20