Working simple digital pin example as shell script but C++ requiring open/close for every read

The lines below I put in a script and ran it from a command line with a wire run from pin 3 to pin 4 on J8 and it prints a 1 and then a 0. I disconnect my wire and I get 0 both times. So I am setting one pin high/low and detecting state on another. Then I made 3 scripts by taking pieces of these - one that left out the unexports at the end, one that just sets 38 high and one that sets it low. I put the detection of the input bit changing into a C++ program. In my C++ program, I opened /sys/class/gpio/gpio39/value, read it and then dropped into a loop where I would sleep for a second and read looking for a change. I closed the port outside the loop. I never saw a change. Then I moved the ope and close of the port inside the loop and it now works. Do you have to close and re-open the ports every time you want to read the current state? That seems really inefficient.

GPIO1_6 = (32 * 1) + 6 = 38 It’s J8 pin 3

GPIO1_7 = (32 * 1) + 7 = 39 It’s J8 pin 4

echo 38 > /sys/class/gpio/export
echo 39 > /sys/class/gpio/export

echo 0x0f > /sys/kernel/debug/omap_mux/gpmc_ad6
echo 0x27 > /sys/kernel/debug/omap_mux/gpmc_ad7

set gpio pin 38 to output

echo out > /sys/class/gpio/gpio38/direction

set gpio pin 39 to input

echo in > /sys/class/gpio/gpio39/direction

set the pin high

echo “high” > /sys/class/gpio/gpio38/direction
#read the input it is looped back to
cat /sys/class/gpio/gpio39/value

set the pin low

echo “low” > /sys/class/gpio/gpio38/direction

read the input it is looped back to

cat /sys/class/gpio/gpio39/value

echo 38 > /sys/class/gpio/unexport
echo 39 > /sys/class/gpio/unexport

Bump - can someone reply to say “yes, that’s what I am experiencing also” or “no, you must have something set up wrong” ???

Try seeking back to the start of the file and then performing the read, I don't know about C++ but in C (pseudo) you could do something like this:


while (1)




I don't know if seeking will work but you could give it a go.