2015-07-19 26 views
0

我使用USB到TTL串行電纜連接我的終極GPS突破到我的樹莓派。使用C代碼,我可以輕鬆連接並從GPS讀取NMEA句子。但是,當我編寫配置命令(如PMTK220)來設置更新速率時,它們將被忽略。我應該得到一個PMTK_ACK報告成功或失敗,但它不是即將到來的。當我使用終端窗口時也會出現這個問題。即。我運行:從樹莓派的adafruit終極GPS配置不起作用

while (true) do cat -A /dev/ttyUSB0 ; done 

一個終端,並得到$GPGGA$GPGSA$GPRMC等信息流。在我運行的另一個終端中:

echo "$PMTK220,200*2C\r\n" > /dev/ttyUSB0 

NMEA消息繼續,但沒有返回PMTK001。有任何想法嗎?

回答

0

確定,這裏的一些演示發送配置消息,卻得到了確認代碼(由GPSD源啓發):

#define _BSD_SOURCE 
#include <stdio.h>  
#include <stdarg.h> 
#include <sys/ioctl.h> 
#include <termios.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <assert.h> 
#include <semaphore.h> 
#include <string.h> 

#ifndef CRTSCTS 
# ifdef CNEW_RTSCTS 
# define CRTSCTS CNEW_RTSCTS 
# else 
# define CRTSCTS 0 
# endif /* CNEW_RTSCTS */ 
#endif /* !CRTSCTS */ 

int main (int argc, char **argv) { 
    int const baudr = B9600, mode = O_RDWR | O_NONBLOCK | O_NOCTTY; 
    int const fd = open("/dev/ttyUSB0", mode); 
    assert (fd != -1); 
    ioctl(fd, (unsigned long)TIOCEXCL); 
    struct termios ttyOld, ttyNew; 
    assert(tcgetattr(fd, &ttyOld) != -1); 
    ttyNew = ttyOld; 
    ttyNew.c_cflag &= ~(CSIZE | PARENB | PARODD | CRTSCTS | CSTOPB); 
    ttyNew.c_cflag |= CREAD | CLOCAL | CS8; 
    ttyNew.c_iflag = ttyNew.c_oflag = 0; 
    ttyNew.c_lflag = ICANON; 
    cfsetispeed(&ttyNew, baudr); 
    cfsetospeed(&ttyNew, baudr); 
    assert(tcsetattr(fd, TCSANOW, &ttyNew) != -1); 
    tcflush(fd, TCIOFLUSH); 
    usleep(200000); 
    tcflush(fd, TCIOFLUSH); 
    int const oldfl = fcntl(fd, F_GETFL); 
    assert (oldfl != -1); 
    fcntl(fd, F_SETFL, oldfl & ~O_NONBLOCK); 
    tcdrain(fd); 
    printf("port opened baudr=%d mode=%d i=%d o=%d c=%d l=%d fl=%d\n", baudr, mode, ttyNew.c_iflag, ttyNew.c_oflag, ttyNew.c_cflag, ttyNew.c_lflag, oldfl); 
    unsigned char buf[2048]; 
    int count = 0; 
    while(++count < 20) { 
     if (count == 4) { 
      char const *const cmd = "$PMTK220,200*2C\r\n"; 
      int const n = strlen(cmd); 
      assert(write(fd, cmd, n) == n); 
      tcdrain(fd); 
      printf("wrote command %d: %s\n", n, cmd); 
     } 
     int const n = read(fd, buf, sizeof(buf));  
     buf[(n >= sizeof(buf)) ? (sizeof(buf) - 1) : n] = 0; 
     printf(buf); 
    } 
    tcsetattr(fd, TCSANOW, &ttyOld); 
    close(fd); 
} 

帶結果:

[email protected] ~/c $ ./test 
port opened baudr=13 mode=2306 i=0 o=0 c=3261 l=2 fl=2050 
$GPGGA,175748.089,,,,,0,00,,,M,,M,,*71 
$GPGSA,A,1,,,,,,,,,,,,,,,*1E 
$GPRMC,175748.089,V,,,,,0.00,0.00,260715,,,N*43 
wrote command 17: $PMTK220,200*2C 

$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32 
$PMTK001,220,2*31 
$GPGGA,175749.089,,,,,0,00,,,M,,M,,*70 
$GPGSA,A,1,,,,,,,,,,,,,,,*1E 
$GPGSV,1,1,01,11,,,31*7A 
$GPRMC,175749.089,V,,,,,0.00,0.00,260715,,,N*42 
$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32 
$GPGGA,175750.089,,,,,0,00,,,M,,M,,*78 
$GPGSA,A,1,,,,,,,,,,,,,,,*1E 
$GPRMC,175750.089,V,,,,,0.00,0.00,260715,,,N*4A 
$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32 
$GPGGA,175751.089,,,,,0,00,,,M,,M,,*79 
$GPGSA,A,1,,,,,,,,,,,,,,,*1E 
$GPRMC,175751.089,V,,,,,0.00,0.00,260715,,,N*4B 
$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32 
$GPGGA,175752.089,,,,,0,00,,,M,,M,,*7A 
[email protected] ~/c $