1

ArduinoとRaspberryを通信させようとしています。sysfs ライブラリと C-Arduino プログラムを使用する Raspberry の C プログラムがあります。

私がすること: Arduino は、私がラズベリーでプログラムをコンパイルして起動するよりも、既にコンパイル済みの (同じ Raspberry 上で) 独自のプログラムを搭載しています。

問題: 以下のコードからわかるように、1 つの Raspberry 入力の遅延で Raspberry のデータを取得します。

Type: a
OFFXX
Type: s
ONFXX
Type: a
OFFXX
Type: s
ONFXX
Type: s
OFFXX
Type: a
OFFXX

初めて必ずOFFXXになる

アルドゥイーノのコード:

int led = 13;
void setup() {                
  pinMode(led, OUTPUT);
  Serial.begin(57600);  
}
void loop() {
   if (Serial.available() > 0) {
      char comando = toLowerCase(Serial.read());
        if (comando == 'a') { 
           digitalWrite(led, HIGH);
           Serial.print("ON");        
        }
        else {
           digitalWrite(led, LOW);
           Serial.print("OFF");
        }        
   } 
} 

そしてラズベリーコード:

#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(){

int fd = open("/dev/ttyS0", O_RDWR);
if (fd == -1) {
  perror("/dev/ttyS0");
  return 1;
}

char msg[] = "a";
char rx[] = "XXXXX";

struct termios tios;
tcgetattr(fd, &tios);
// disable flow control and all that, and ignore break and parity errors
tios.c_iflag = IGNBRK | IGNPAR;
tios.c_oflag = 0;
tios.c_lflag = 0;
cfsetspeed(&tios, B57600);
tcsetattr(fd, TCSAFLUSH, &tios);

// the serial port has a brief glitch once we turn it on which generates a
// start bit; sleep for 1ms to let it settle
usleep(1000);

// output to serial port
while(1){
    printf("Type: ");
    scanf("%s", msg);
    write(fd, msg, strlen(msg));

    read(fd, rx, strlen(rx));
    printf("%s\n", rx);
}
}

USBケーブルとGPIOの両方にこの問題があります

編集:他の問題: OUTPUT が以前に初期化された最後の文字を覚えているのはなぜですか?

4

1 に答える 1

0

RAW モードで Raspberry 側のシリアル ポートを開きます。次のコードを使用して、RAW モードでポートを開きます。

int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

if(fd == -1)
{
        return -1;
}
else
{

        struct termios new_termios;
        struct termios orig_termios;

        tcgetattr(fd, &orig_termios);
        memcpy(&new_termios, &orig_termios, sizeof(new_termios));

        cfmakeraw(&new_termios);

        cfsetispeed(&new_termios, B57600);
        cfsetospeed(&new_termios, B57600);

        tcsetattr(fd, TCSANOW, &new_termios);

}

うまくいけば、それはあなたの場合に完璧に機能します。

于 2015-02-17T11:27:45.613 に答える