문제

I'm thinking of using a coin acceptor with my raspberry pi. As I understood the coin acceptor sends a byte with a specified value for each coin. My question now: How can I read that byte which comes in over GPIO?

Thanks for help!

도움이 되었습니까?

해결책

I found this may be helpful sites.google.com/site/semilleroadt/raspberry-pi-tutorials/gpio

Pretty much this line

cat /sys/class/gpio/gpio11/value 

should read your coin value I think.

Assuming you use a Linux OS.

This is how you write the value 1 to GPIO

echo 1 > /sys/class/gpio/gpio11/value

The link also provides better C code so you don't have to use shell commands.

The gpio11 in that path is based on the file access created by

echo 11 > /sys/class/gpio/export

Also you need to set out direction
Configure the Pin Direction (In/Out):

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

In the tutorial 11 was used but your number may be different I still don't understand what a GPIO is but I believe is where you stick in your wires on the chip lol.

Here is a image how the board GPIO's look like (for future references)

image

C code below

Manage GPIO from application

All these same operations can be made using a software application. Follow short lines of C code showing how the reproduce the same steps as above (remember to change XX with the GPIO number you want to use).

Reserve (export) the GPIO:

int fd;
char buf[MAX_BUF]; 
int gpio = XX;

fd = open("/sys/class/gpio/export", O_WRONLY);

sprintf(buf, "%d", gpio); 

write(fd, buf, strlen(buf));

close(fd);

Set the direction in the GPIO folder just created:

sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio);

fd = open(buf, O_WRONLY);

// Set out direction
write(fd, "out", 3); 
// Set in direction
write(fd, "in", 2); 

close(fd);

In case of out direction set the value of GPIO:

sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);

fd = open(buf, O_WRONLY);

// Set GPIO high status
write(fd, "1", 1); 
// Set GPIO low status 
write(fd, "0", 1); 

close(fd);

In case of in direction get the current value of GPIO:

char value;

sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);

fd = open(buf, O_RDONLY);

read(fd, &value, 1);

if(value == '0')
{ 
     // Current GPIO status low
}
else
{
     // Current GPIO status high
}

close(fd);

Once finished free (unexport) the GPIO:

fd = open("/sys/class/gpio/unexport", O_WRONLY);

sprintf(buf, "%d", gpio);

write(fd, buf, strlen(buf));

close(fd);

An important note you have to keep in mind if you plan to set or, more important, get the value of a GPIO through this way in continous mode. If you open the "value" file for get the current GPIO status (1 or 0) remember that, after the fist read operation, the file pointer will move to the next position in the file. Since this interface was made to be read from cat command the returned string will be terminated by the new line character (\n). This mean after the first "valid" read all the next read operation will return always the last character in the file, in this case only the new line '\n'. For obtain a correct status value for each read operation you simply have to set the file pointer at the beginning of the file before read by using the command below:

lseek(fp, 0, SEEK_SET);

You will not have this problem if you open and close GPIO value file every time you need to read but, as you can know, for continuous read introduce a short delay. Since these short lines of codes are only an example if you want to use them in your code remember add the control for error in open GPIO file.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top