2017-04-16 13 views
0

나는 arduino에서 시리얼을 읽고 싶다.C 언어를 사용하여 리눅스에서 arduino 시리얼을 읽기

int x; 
void setup() { 
    Serial.begin(9600); 
} 
void loop() { 
    Serial.println("DO YOU HEAR ME ??"); 
    delay(1000); 
} 

이 combinatin의 출력 즉 :

#include <stdio.h> /* Standard input/output definitions */ 
#include <string.h> /* String function definitions */ 
#include <unistd.h> /* UNIX standard function definitions */ 
#include <fcntl.h> /* File control definitions */ 
#include <errno.h> /* Error number definitions */ 
#include <termios.h> /* POSIX terminal control definitions */ 
#include <sys/ioctl.h> 
int main(){ 
    char data[1024]; 
    char dev[] = "/dev/ttyACM1"; 
    int fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); 
    fcntl(fd, F_SETFL, FNDELAY); 
    struct termios options; 
    tcgetattr(fd, &options); 
    cfsetispeed(&options, B9600); 
    cfsetospeed(&options, B9600); 
    options.c_cflag |= CS8; 
    options.c_cflag |= CS8; 
    options.c_cflag &= ~CRTSCTS; 
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); 
    tcsetattr(fd, TCSANOW, &options); 
    ioctl(fd, TCFLSH, 2); 
    while(1){ 
     read(fd, data, sizeof(data)); 
     printf(data); 
    } 
    //write(fd, data, sizeof(data)); 
} 

내 아두 이노는 매우 간단한 스케치를 실행 :이 코드를 사용

??OU HEAR ME ?? 
DO YOU HEAR ME ?? 
DO YOU HEAR ME ?? 
A¹­þ 
??OU HEAR ME ?? 
DO YOU HEAR ME ?? 
DO YOU HEAR ME ?? 
A¹­þ 
??OU HEAR ME ?? 
DO YOU HEAR ME ?? 
DO YOU HEAR ME ?? 

내 질문의 질서를 만드는 방법입니다 혼돈. 이 문제는 버퍼가 끝나고 새 버퍼가 시작될 때 발생합니다 (더 큰 버퍼보다 ​​덜 정크 데이터). 그러나 무한 버퍼를 가질 수 없습니다. 또한 처음 읽을 때 많은 쓰레기가 있습니다 .. 동기화 할 방법이 있습니까?

은 (또한 내가 어떤 실수하지 네이티브 영어 죄송합니다.)

+0

이 read''의 반환 값을 확인하십시오 : http://tldp.org/HOWTO/Serial-Programming-HOWTO/가 (예제 코드를 형성 ..) 여기

그리고 새로운 가난한 아두 이노 코드입니다. –

+0

@DavidCullen 나는 cannonical 입력을 가능하게하고 이제는 라인의 시작 부분에서만 정크가 발생한다. 그래서 나는 그것이 입력에 약간의 비트가 있다고 가정하고있다. 그러나 어디에서 (3 자 , 3bytes). – Kozlowsqi

+0

한 쪽 메모는 전송 속도를 높이고 문제가 해결되는지 확인 하시겠습니까? – LethalProgrammer

답변

0

난 내 자신의 문제에 대한 해답을 발견했다.

#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <termios.h> 

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <strings.h> 

#include <iostream> 
//Here I define some vars 
#define BAUDRATE B9600 
#define MODEMDEVICE "/dev/ttyACM0" 
#define _POSIX_SOURCE 1 

class serial{ 
public: 
serial(){ 
    fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY); 
    if (fd <0) {perror(MODEMDEVICE); exit(-1); } 
    // Improvement No. 1 I save old setting and clean the new one 
    tcgetattr(fd,&oldtio); 
    bzero(&newtio, sizeof(newtio)); 

    // Here I set all the flags to vars at once 
    newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; 
    newtio.c_iflag = IGNPAR | ICRNL; 
    newtio.c_oflag = 0; 
    newtio.c_lflag = ICANON; 
    //here I set some new flags.. 
    newtio.c_cc[VINTR] = 0;  /* Ctrl-c */ 
    newtio.c_cc[VQUIT] = 0;  /* Ctrl-\ */ 
    newtio.c_cc[VERASE] = 0;  /* del */ 
    newtio.c_cc[VKILL] = 0;  /* @ */ 
    newtio.c_cc[VEOF]  = 4;  /* Ctrl-d */ 
    newtio.c_cc[VTIME] = 0;  /* inter-character timer unused */ 
    newtio.c_cc[VMIN]  = 1;  /* blocking read until 1 character arrives */ 
    newtio.c_cc[VSWTC] = 0;  /* '\0' */ 
    newtio.c_cc[VSTART] = 0;  /* Ctrl-q */ 
    newtio.c_cc[VSTOP] = 0;  /* Ctrl-s */ 
    newtio.c_cc[VSUSP] = 0;  /* Ctrl-z */ 
    newtio.c_cc[VEOL]  = 0;  /* '\0' */ 
    newtio.c_cc[VREPRINT] = 0;  /* Ctrl-r */ 
    newtio.c_cc[VDISCARD] = 0;  /* Ctrl-u */ 
    newtio.c_cc[VWERASE] = 0;  /* Ctrl-w */ 
    newtio.c_cc[VLNEXT] = 0;  /* Ctrl-v */ 
    newtio.c_cc[VEOL2] = 0;  /* '\0' */ 
    // and I finally save the settings 
    tcflush(fd, TCIFLUSH); 
    tcsetattr(fd,TCSANOW,&newtio); 
} 
~serial(){ 
    close(fd); 
} 

    std::string sread(){ 
     res = read(fd,buf,255); 
     buf[res]=0; 
     return buf; 
    } 
    void swrite(const char* input){ 
     write(fd,input,sizeof(input)); 
    } 
private: 
    int fd,c,res; 
    struct termios oldtio,newtio; 
    char buf[255]; 
}; 


int main(){ 
    serial s; 
    s.swrite("Light"); 
    std::string buf = s.sread(); 
    std::cout << buf; 
} 

이 더있다 : (이 오류를 처리하지 않고 대부분의 C++를 사용하지 않기 때문에 매우 가난한 사람은 ..) 나는 수업을 구성 할이 시간 C++를 사용 여기에 코드입니다 "HOW TO 시리얼 프로그래밍"에서 해적판 :

int x; 
String buff; 
int lpin = A0; 
int tpin = A1; 
int data; 
void setup() { 
    Serial.begin(9600); 
    pinMode(lpin,INPUT); 
    pinMode(tpin,INPUT); 
} 
void loop() { 
    if(Serial.available() == 1) 
    { 
    buff = Serial.readString(); 
    if(buff == "Light"){ 
     data = analogRead(lpin); 
     Serial.print(data); 
     Serial.print("\n"); 
    }else if(buff == "Temp"){ 
     data = analogRead(tpin); 
     Serial.print(data); 
     Serial.print("\n"); 
    }else{ 
     Serial.print("Something went wrong !"); 
     Serial.print("\n"); 
    } 
    } 
    delay(1); 
}