2015-03-08 140 views
1

我想通過使用運行在Raspberry Pi上的Apache服務器,通過GPIO串行端口將數據從Raspberry Pi發送到Arduino Uno。我將RPI的TX引腳連接到一個3.3   V-to-5   V電平移位器,並將其輸出連接到RX Arduino PIN。如何通過Apache網絡服務器控制Raspberry Pi的串行GPIO引腳

從Raspberry Pi發送數據到Arduino我使用了下面的C程序,我重新編譯爲Raspberry Pi,它工作正常。我重命名了可執行代碼SendUART

#include <stdio.h> 
#include <unistd.h> //Used for UART 
#include <fcntl.h> //Used for UART 
#include <termios.h> //Used for UART 
#include <string.h> 

main(int argc,char **argv) 
{ 
    //----- TX BYTES ----- 
    unsigned char tx_buffer[20]; 
    unsigned char *p_tx_buffer; 
    int lx; 

    //------------------------- 
    //----- SETUP USART 0 ----- 
    //------------------------- 
    //At bootup, pins 8 and 10 are already set to UART0_TXD, UART0_RXD (ie the alt0 function) respectively 
    int uart0_filestream = -1; 

    //OPEN THE UART 
    //The flags (defined in fcntl.h): 
    // Access modes (use 1 of these): 
    //  O_RDONLY - Open for reading only. 
    //  O_RDWR - Open for reading and writing. 
    //  O_WRONLY - Open for writing only. 
    // 
    // O_NDELAY/O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status 
    //           if there is no input immediately available (instead of blocking). Likewise, write requests can also return 
    //           immediately with a failure status if the output can't be written immediately. 
    // 
    // O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process. 
    uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);  //Open in non blocking read/write mode 
    if (uart0_filestream == -1) 
    { 
     //ERROR - CAN'T OPEN SERIAL PORT 
     printf("Error - Unable to open UART. Ensure it is not in use by another application\n"); 
    } 

    //CONFIGURE THE UART 
    //The flags (defined in /usr/include/termios.h - see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html): 
    // Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000 
    // CSIZE:- CS5, CS6, CS7, CS8 
    // CLOCAL - Ignore modem status lines 
    // CREAD - Enable receiver 
    // IGNPAR = Ignore characters with parity errors 
    // ICRNL - Map CR to NL on input (Use for ASCII comms where you want to auto correct end of line characters - don't use for bianry comms!) 
    // PARENB - Parity enable 
    // PARODD - Odd parity (else even) 
    struct termios options; 
    tcgetattr(uart0_filestream, &options); 
    options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; //<Set baud rate 
    options.c_iflag = IGNPAR; 
    options.c_oflag = 0; 
    options.c_lflag = 0; 
    tcflush(uart0_filestream, TCIFLUSH); 
    tcsetattr(uart0_filestream, TCSANOW, &options); 
    p_tx_buffer = &tx_buffer[0]; 
    strcpy(tx_buffer, argv[1]); 
    lx=strlen(tx_buffer); 


    if (uart0_filestream != -1) 
    { 
     int count = write(uart0_filestream, &tx_buffer[0], 10); //Filestream, bytes to write, number of bytes to write 
     if (count < 0) 
     { 
      printf("UART TX error\n"); 
     } 
    } 
    //----- CLOSE THE UART ----- 
    close(uart0_filestream); 
} 

然後我用一個PHP腳本(test.php的)通過 「系統」 命令來處理軟件:

<?php 
    $mes = ""; 
    $retval = 0; 
    $last_line = 0; 
    echo($mes); 
    $SndMsg = "/var/www/SendUART " . $mes; 
    $last_line = system($SndMsg, $retval); 
    echo $last_line 
?> 

而且我通過命令行來執行它:

php -f test.php 

該字符串被Arduino正確接收(我爲Arduino開發了一個簡單的sketch,如果從Rx引腳接收到所有字符,它將打開板載LED。

然後我通過Apache Web服務器稱爲PHP腳本,寫在地址欄中輸入: http://192.168.1.103/test.php

其中192.168.1.103是樹莓派的IP地址和test.php是PHP腳本。當然,無論test.php和SenUART程序都存儲在同一文件夾中的/ var/www /,腳本,但在瀏覽器中顯示以下錯誤:

Error - 無法打開UART。確保它不被其他應用程序使用

如何解決該問題?

+0

UART是否可以作爲串口控制檯使用?你使用raspbian嗎? http://www.raspberry-projects.com/pi/pi-operating-systems/raspbian/io-pins-raspbian/uart-pins – chugadie 2015-03-08 17:47:55

+0

UART已啓用。我正在使用raspbian。 – Ferrari 2015-03-08 22:06:36

+0

您必須禁用UART作爲串行控制檯使用。看到以前發佈的鏈接。 – chugadie 2015-03-09 12:46:58

回答

1

運行您的網絡服務器的用戶可能無法訪問UART。您可以通過配置Apache以通過設置在httpd.conf下面以root身份運行快速測試:

User root 

因爲它不是運行Web服務器作爲根是個好主意,你要找出什麼用戶的Apache通常像系統一樣運行(可能是www),併爲該用戶提供使用串行端口的權限。這樣的事情可能工作:

chown :www /dev/ttyAMA0 
chmod g+rw /dev/ttyAMA0 

或者,您可能只需要用戶www添加到組像callout

useradd -G callout www 

調整爲特定的系統。

+0

親愛的菲利普,太棒了!有用!!非常感謝你! – Ferrari 2015-03-10 08:53:36