早上好,每个人,
我的Semester项目的子任务要求我在Arduino和Raspberry Pi 3之间进行串行通信。我在编程方面不是很有经验,只有一个C ++学期。我找到了一个名为wiringSerial的好库,它预先设置了串行通信的所有首选项。现在问题 - 如果我手动写入串口(终端,使用echo a > /dev/ttyUSB0
),我得到了预期的正确响应。但是,如果我使用我用C ++编写的程序,我会得到完整的垃圾或者没有任何回复。如果你们其中一个人可以帮助我,我将非常感激。这是代码:
Arduino Side:
#include "Arduino.h"
int PIN_NUMBER;
int BAUD_RATE=9600;
int PROBLEM_LED=2;
int PWM_PINS[]={3, 5, 6, 9}; //x+: 3, x-: 5, y+: 6, y-: 9;
const int PIN_COUNT=4;
const int MAX_VOLT=5;
const char XPLUS_MASK = 0x62;
const char XMINUS_MASK = 0x63;
const char YPLUS_MASK = 0x64;
const char YMINUS_MASK = 0x65;
const char ACK_MASK = 0x60;
const char INIT_MASK = 0x61;
bool initialized = false;
const char MASK[]={XPLUS_MASK, XMINUS_MASK, YPLUS_MASK, YMINUS_MASK};
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(BAUD_RATE);
while(!Serial);
for(int i=0; i<4; i++){
pinMode(PWM_PINS[i], OUTPUT);
}
pinMode(PROBLEM_LED, OUTPUT);
}
void decode(char opCode, char value){
if((!initialized && ((opCode & INIT_MASK))!=INIT_MASK)){
Serial.write(0);
return;
}
else if(((opCode & INIT_MASK))==INIT_MASK){
Serial.write(ACK_MASK);
for(int i=0; i<4; i++){
analogWrite(PWM_PINS[i], 0);
}
Serial.flush();
initialized=true;
return;
}
for(int i=0; i<4; i++){
if((opCode & MASK[i])==MASK[i]){
analogWrite(PWM_PINS[i], value);
Serial.write(ACK_MASK);
return;
}
}
return;
}
void loop() {
char opCode=0;
char value=0;
if(Serial.available()==1){
opCode=Serial.read();
decode(opCode, 0);
}
else if(Serial.available()>=2){
opCode=Serial.read();
value=Serial.read();
Serial.read();
decode(opCode, value);
}
while(Serial.available()){
Serial.read();
}
delay(100);
}
Raspberry Side:
#include "ArdPiComm.h"
#include <iostream>
#include <wiringSerial.h>
#include <wiringPi.h>
#include <bitset>
#include <stdio.h>
#include <errno.h>
#include <string.h>
ArdPiComm::ArdPiComm(){
INIT_BYTE=0x61;
XPLUS_MASK=0x62;
XMINUS_MASK=0x63;
YPLUS_MASK=0x64;
YMINUS_MASK=0x65;
ACK_MASK=0x60;
BAUD_RATE=9600;
PORT="/dev/pts/7\0";
fd=0;
MASKS[0]=XPLUS_MASK;
MASKS[1]=XMINUS_MASK;
MASKS[2]=YPLUS_MASK;
MASKS[3]=YMINUS_MASK;
}
ArdPiComm::ArdPiComm(char* port, int baud_rate) {
INIT_BYTE=0x61;
fd=0;
XPLUS_MASK=0x62;
XMINUS_MASK=0x63;
YPLUS_MASK=0x64;
YMINUS_MASK=0x65;
ACK_MASK=0x60;
BAUD_RATE=baud_rate;
PORT=port;
}
ArdPiComm::~ArdPiComm() {
delete this;
}
void ArdPiComm::close(){
serialClose(fd);
return;
}
bool ArdPiComm::INIT(){
fd=serialOpen("/dev/ttyUSB0\0", 9600);
unsigned char rt=0;
serialPutchar(fd, 0x61);
serialFlush(fd);
while((serialDataAvail(fd))==0);
rt=serialGetchar(fd);
if((rt&ACK_MASK)==ACK_MASK){
std::cout << "Connection established. ";
return true;
}
std::cout << "Connection not Acknowledged. Please Retry.";
return false;
}
/*---------------------- steer function --------------------------
* PRE: Input is an Array of Dimensions 1x4, where A[0] X+, A[1] X-, A[2] Y+, A[3] Y-.
* The Values Stored in the Array are ranging from 0-255 describing the deviance from the 0
* point on corresponding axis
* POST: If write was sucessful, output is true, else it is false
---------------------------------------------------------------*/
bool ArdPiComm::send(int A[]){
unsigned char value=0;
unsigned int rt=0;
for(int k=0; k<4; k++){
if(A[k] > 255){
std::cout << "Value too large, please input value between 0 and 255\n";
return false;
}
value=(A[k] & 0xFF);
//write the Destination and then the value;
serialPutchar(fd, MASKS[k]);
serialPutchar(fd, value);
serialFlush(fd);
// getting ACK
while(serialDataAvail(fd)==0); //spinning
rt=serialGetchar(fd);
rt= rt & ACK_MASK;
if(rt!=ACK_MASK){
std::cout << "Failed to acknowledge. Please retry\n ";
return false;
}
if(rt==ACK_MASK){
std::cout << "Received loud and clear. Please Proceed.\n ";
return true;
}
else {
std::cout << "no idea what happend. Revise your code\n";
return false;
}
}
return true;
}
对不好的评论抱歉。这应该没有任何问题:P 非常感谢您的宝贵时间。
亲切的问候, 基利安
p.s。:这是图书馆源代码的链接:https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringSerial.c