2014-01-14 45 views
0

所以我有在C此功能(下面的代码),从我的数据库检查数据。如果这个数据等于255,我想在我的一个GPIO引脚(12)上点亮树莓派上的LED。分段故障

我第一次尝试的代码而不亿立方米的功能,它的工作一切正常。现在我已经包含了bcm2835,并且改变了我的Makefile(编译工作正常),我得到了这个“Segmentation fault”错误。

我知道这意味着我的程序正在使用它不应该使用的内存,但我不知道在bcm行中是什么导致了这种情况。

下面是函数:

void check_pasid(char k[]){ 

MYSQL *conn; 
MYSQL_RES * result; 
MYSQL_ROW row; 



    char *server = "server"; 
    char *user = "myusername"; 
    char *password = "mypassword"; 
    char *database = "dbname"; 
    char query1[100]; 

    // Make the connection to the Mysql-database. 
    conn = mysql_init(NULL); 

    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { 
     fprintf(stderr, "%s\n", mysql_error(conn)); 
     exit(1); 

    } 

sprintf(query1,"SELECT COUNT(*) FROM passcan WHERE UserID = \"%s\";",k); 


int result1 = mysql_query(conn, query1); 
result = mysql_store_result(conn); 
row = mysql_fetch_row(result); 

int compare = 1; 
compare = strcmp(row[0], "1"); 

if(compare == 0){ 
    printf("Led is turning on"); 
    // Turn it on 
    bcm2835_gpio_write(PIN12, HIGH); 
    delay(5000); 
    // Turn it off 
    bcm2835_gpio_write(PIN12, LOW); 
} 
else{ 
    printf("Led is not turning on"); 
} 

} 

我希望有人给我一些信息。

+0

我希望在'k'字符串少于约50个字符,或者你将有一个缓冲区溢出。 –

+0

@JoachimPileborg它是 – moffelnijdam

回答

0

正确的使用BCM2835的
这里的problably可以更好地工作:

#include <bcm2835.h> 
// Led on RPi Plug P1 pin 11 (which is GPIO pin 17) 
#define PIN RPI_GPIO_P1_11 
uint8_t status = LOW; 
int SetLed() 
{ 
    // Inizialize the library 
    if (!bcm2835_init()) 
     return 1; 
    // Set the pin to be an output 
    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP); 
    // Set the Led: 
    bcm2835_gpio_write(PIN, status); 
    status=!status; 
    /* 
    I fetched a flip-flop variable, problably does not work without integer variable, so replace the up line with 
    if (status == HIGH) 
    status = LOW; 
    else 
    status = HIGH; 
    */ 
    //Clean-up and return success. 
    bcm2835_close(); 
    return 0; 
} 

我不是专家,代码可以是错误的,但部分重要的是功能bcm2835_init()bcm2835_gpio_fsel()和必须几乎被称为一次的bcm2835_close()