2016-06-10 2 views
0

Arduino Uno에서 RaspberryPi 3B로 sendo 데이터를 보내려고합니다. 라즈베리가 요청하면 Arduino에서 14 int (최대 값 : 6000)를 보내야합니다. 이 14 개의 숫자 각각은 ADC (SPI 통신)에서 나왔습니다.Arduino와 Raspberry 사이의 직렬 통신에서 잘못된 데이터 전송

ARDUINO 측

#include "SPI.h" 
byte incomingByte = 0; // for incoming serial data 

const int N_SENSORI=14; 
const int DATAOUT = 11; 
const int DATAIN = 12; 
const int SPICLOCK = 13; 
const int SLAVESELECT = 10; 

//===============SPI COMMUNICATION================ 
short write_read_spi16(short what) { 
    digitalWrite(SS, LOW); 
    short res = SPI.transfer16(what); 
    digitalWrite(SS, HIGH); 
    return res; 
} 

//===============CONVERT INT IN BYTE AND SEND IT================ 
void longInt2Byte(int x){ 
    unsigned char buf[sizeof(int)]; 
    memcpy(buf,&x,sizeof(int)); 
Serial.write(buf,sizeof(buf)); 

} 
//======================================= 

void setup() { 

    Serial.begin(115200);  
    SPI.begin(); 
    pinMode(DATAOUT, OUTPUT); 
    pinMode(DATAIN, INPUT); 
    pinMode(SPICLOCK,OUTPUT); 
    pinMode(SLAVESELECT,OUTPUT); 
} 
void loop() { 


if (Serial.available() > 0) {   
    incomingByte= Serial.read(); 

    if (incomingByte=='E') { 

    write_read_spi16(0b1000001101110000); //THIS IS FOR THE ADC COMMUNICATION 

    for (int i = 1; i<N_SENSORI+1; i++) { //DONE 14 TIMES. 
     short s = write_read_spi16(0b1000001101110000 | (i<<10)); 
     int Data = s&0xFFF; 
     longInt2Byte(Data); 
     }  
     } 
}} 

// C++ SIDE

void stimulationController::SetupDario(){ 

    cout<<"Dentro al setupDario"<<endl<<flush; 
    buffer=new char [1000]; 
    const char* _portNameSensorsDario="/dev/ttyACM1"; 
    //SERIAL PORT FOR HAND SENSORS 
    struct termios options; 
    SerialHandleSensors=open(_portNameSensorsDario, O_RDWR | O_NOCTTY | O_NDELAY); //SerialHandleSensors=open(_portNameSensors, O_RDWR | O_NOCTTY | O_NDELAY); non blocking 
    if (SerialHandleSensors == -1) 
    { 
     cout<<endl<<"......ERROR: Unable to open: "<<_portNameSensorsDario<<endl; 
     return; 
    } 
    else 
    { 
     fcntl(SerialHandleSensors, F_SETFL,0); 
     cout<<"......OPENED PORT: Succesfully opened: "<<_portNameSensorsDario<<endl; 
    } 

    //GET THE OPTIONS, MODIFY AND SET 
    tcgetattr(SerialHandleSensors,&options); 
    cfsetispeed(&options,B115200); //BAUD RATE IN 
    cfsetospeed(&options,B115200); //BAUD RATE OUT 
    // options.c_lflag |= (ICANON | ECHO | ECHOE); 
    options.c_cflag |= (CLOCAL | CREAD); 
    options.c_cflag &= ~PARENB; 
    options.c_cflag |= CSTOPB; 
    options.c_cflag &= ~CSIZE; 
    options.c_cflag |= CS8; 
    tcsetattr(SerialHandleSensors,TCSANOW,&options); 

    usleep(3000000); 
cout<<"Fine Setup"<<endl<<flush; 
} 



int stimulationController::Dario() 
{ 

    { 
    cout<<" NUOVA FUNZIONE"<<endl; 


unsigned char bytes[4]; 
    bytes[0]=0x45; // 'E' 

    int tempWrite=write(SerialHandleSensors,bytes,1); 
    if(tempWrite==-1) 
    { 
     cout<<"......ERROR: PROBLEM WRITING TO ROBOTIC HAND"<<endl; 
     failure=true; 
     return 0; 

    } 

    int value=0; 


    int tempRead=read(SerialHandleSensors,buffer,28); 
    if(tempRead==-1) 
    { 
     cout<<"......ERROR: PROBLEM READING FROM ROBOTIC HAND"<<endl; 
     failure=true; 
     return 0; 
    } 


int j=0; 
for(int i=0; i<28; i=i+2){ 
    value= buffer[i] | ((int)buffer[i+1]<<8); 
    //ensorDataFoot.push_back(value); //Aggiunge un elemento 
    j=j+1; 
     cout<<"Dato "<<j <<"vale: "<<value<<endl<<flush; 
    } 

    return value; 

} 
} 

문제는 그 또는 딸기 쪽이 올바른 값을 출력 얼마 일부 시간 (시간의 대부분 사실이다) 그렇지 않습니다. 데이터가 반복되거나 (예 : 2500 insted 2000).

나는 문제를 파악할 수 없다. 데이터 전송 요청과 읽기 사이에 타이밍 문제가있을 수 있습니까? 그렇다면 그것을 제거 할 수있는 방법이 있습니까?

나는 모든 28 바이트를 읽을되어 있는지 확인하기 위해 할 - 동안 사이클을 추가 한

int TotByte=28; 
int ByteRead=0; 
int TempRead=0; 

do{ TempRead= read(SerialHandleSensors, buffer, (TotByte-ReadByte)); 
ReadByte= ReadByte+TempRead; 
cout<<"ReadByte is: "<<ReadByte<<endl<<flush; 
}while(ByteRead<TotByte); 

출력 :

ReadByte is: 5 
ReadByte is: 15 
ReadByte is: 25 

후 아무것도하지 않고 그런 식으로 유지

+0

커뮤니티 위키 답변을 편집 해 주셔서 감사합니다. 직접 게시하고 싶다면 (자유롭게 편집하고 잠재적 평판 포인트를 얻을 수 있도록) 다른 사본을 게시하고 알려 주시면 현재의 것을 삭제할 수 있습니다. – halfer

답변

0

(영업 대신 배치 답) :

나는 그것을 알아 냈다. 문제는 직렬 포트의 설정이었습니다. 나중에 참조 할 수 있도록 다음은 작동중인 버전입니다.

bufferTemp=new char [1000]; 
    bufferDEF = new char[1000]; 
    const char* _portNameSensorsBT="/dev/rfcomm0"; 

    struct termios options; 
    SerialHandleSensorsFoot=open(_portNameSensorsBT, O_RDWR); //SerialHandleSensors=open(_portNameSensors, O_RDWR | O_NOCTTY | O_NDELAY); non blocking 
    if (SerialHandleSensorsFoot == -1) 
    { 
     cout<<endl<<"......ERROR: Unable to open: "<<_portNameSensorsBT<<endl; 
     return 0; 
    } 
    else 
    { 
     fcntl(SerialHandleSensorsFoot, F_SETFL,0); 
     cout<<"......OPENED PORT: Succesfully opened: "<<_portNameSensorsBT<<endl; 
    } 

    //GET THE OPTIONS, MODIFY AND SET 
    tcgetattr(SerialHandleSensorsFoot,&options); 
    cfsetispeed(&options,B115200); //BAUD RATE IN 
    cfsetospeed(&options,B115200); //BAUD RATE OUT 
    // options.c_lflag |= (ICANON | ECHO | ECHOE); 

    options.c_iflag = IGNBRK | IGNPAR; 
    options.c_oflag = 0; 
    options.c_lflag = 0; 

    options.c_cflag |= (CLOCAL | CREAD); 
    options.c_cflag &= ~PARENB; 
    options.c_cflag |= CSTOPB; 
    options.c_cflag &= ~CSIZE; 
    options.c_cflag |= CS8; 
    tcsetattr(SerialHandleSensorsFoot,TCSAFLUSH,&options); 

    usleep(5000000); 

이러한 설정을 사용하면 속도 문제가있을 수 있지만 블루투스와 통신 할 수 있습니다.