-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
write: /sys/class/gpio/export: device or resource busy #30
Comments
Can you try with |
I can reproduce that with this code and before finish, I do Ctrl + c. When I run again:
|
somehow restarting fixed my problem. I was also mucking about in raspi-config too, so not sure if anything there helped. I think if I exit without closing down gpio it can leave stuff around that is hard to clean up. I can catch interrupts and execute a close before exiting, but I am not sure it is a good idea for anything outside of main to do this. |
Two options:
You have to handle the signal in a goroutine and then switch on it in a loop, making the program exit cleanly and run it's defer's.
|
I ran into this same issue on a Raspberry Pi 2 Model B. Following @akofoed's instructions, I was able to get the program to run multiple times in a row without any more errors: package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/kidoman/embd"
_ "github.com/kidoman/embd/host/rpi" // This loads the RPi driver
"golang.org/x/net/context"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
sigch := make(chan os.Signal, 2)
signal.Notify(sigch, os.Interrupt, syscall.SIGTERM)
go handleSignals(sigch, ctx, cancel)
log.Println("hello world!")
embd.InitGPIO()
defer embd.CloseGPIO()
pin, err := embd.NewDigitalPin("GPIO_5")
if err != nil {
log.Fatal("opening pin:", err)
}
defer resetPin(pin)
if err = pin.SetDirection(embd.Out); err != nil {
log.Fatal("setting pin direction:", err)
}
nextValHigh := true
for {
select {
case <-ctx.Done():
return
case <-time.After(500 * time.Millisecond):
if nextValHigh {
pin.Write(embd.High)
} else {
pin.Write(embd.Low)
}
nextValHigh = !nextValHigh
}
}
}
func handleSignals(sigch <-chan os.Signal, ctx context.Context, cancel context.CancelFunc) {
select {
case <-ctx.Done():
case sig := <-sigch:
switch sig {
case os.Interrupt:
log.Println("SIGINT")
case syscall.SIGTERM:
log.Println("SIGTERM")
}
cancel()
}
}
func resetPin(pin embd.DigitalPin) {
if err := pin.SetDirection(embd.In); err != nil {
log.Fatal("resetting pin:", err)
}
pin.Close()
} What do you think about a PR to add this cleanup to the Raspberry Pi examples (including the readme)? That, or this should be handled automatically by the cleanup/ |
I am trying to get simple gpio working on my rpi model B.
From the
gopath/src/github.com/kidoman/embd/samples
directory I rungo build gpio.go
andsudo ./gpio
.I get quick panic with
panic: write: /sys/class/gpio/export: device or resource busy
Running latest raspbian image. I have built go from source on the device.
The text was updated successfully, but these errors were encountered: