1

I have trouble replacing device in/output commands like:

echo 100 > /dev/rtmotor_raw_l0   # output 100hz frequency
cat /dev/rtswitch0 # read switch state

output problem(python)

I tried replacing that command with python.

file = open('/dev/rtmotor_raw_l0','w')
file.write('100\n')            # I want output in this timing
file.close()                   # output reflected after closing file

The problem is that the output appears after closing the file. Does this mean I have to open and close this device each time I want to change its value? Also, changing 'w' to 'a' did not work.

input problem(python)

Almost the same problem happens in input observation.

file = open('/dev/rtswitch','r')
file.read()  # works
file.read()  # after first read it does't work anymore
file.close() # need to reopen the file to get newer value

I could only read 1 input in each opening files.

So, currently I have to reopen the devices each time I want to write/read new values. Are there any way to avoid this problem?

Thank you.

1
  • 1
    After the first read, the file pointer is at the end of the file; assuming '/dev/rtswitch is seekable, you need to rewind the file pointer to the beginning of the file before you can read the contents again: put file.seek(0) between the two reads. Commented Feb 1, 2019 at 14:53

1 Answer 1

1

Try call file.flush() after write() call. It will flush buffer to file and you can read data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.