Y. Ri
Y. Ri

Reputation: 53

How can I replace "cat" and "echo" read/write command with python/c program?

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.

Upvotes: 0

Views: 361

Answers (1)

Matej
Matej

Reputation: 820

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

Upvotes: 1

Related Questions