update,
soweit ich es beurteilen kann ist der Arduino code jetzt fail-safe, weil Serial.readStringUntil() intern alle Puffer und Timeouts selber verwaltet.
(Arduino = Mega2560)
Der Code wurde inzwischen geupdated zum Debuggen:
i0 wird jetzt incrementiert in jeder Arduino loop und dann zurückgesendet,
und i2 wird jetzt incrementiert in jeder Raspi loop und dann zurückgesendet.
Es geht einige Sekunden oder gar Minuten gut, dann hängt sich das System plötzlich aber wieder auf:
Arduino code:
Code:
// Arduino Serial to Raspi USB
// history:
// 0705 debug value (i2) from Raspi
// 0704 Serial.readStringUntil(), debug value (i0) from Arduino
// 0703 simple msg str
// 0702 fixes
// 0701 Serial.read delimiters
// 0700
// 0101 ported for RPi USB
//
// 0009
// 0008 analog aX=analogRead(AX) (X=0...11), syntax "&AX=%ld",aX
// 0007 pin-loop, 6x out-pins OUTn(I/O/pwm), &_ALLPINS_=0 when BCB-close
// 0006 output pins DPINn
// 0003 send/receive strings
// 0002 receiving strings, pattern: &varname1=value1;
// 0001 receiving simple Serial char
// ver 0705
const uint32_t UARTclock = 115200;
int32_t i0=0,i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0,i9=0; // int (general)
String inputString="";
#define TOKLEN 30
#define MSGLEN 1024
char mbuf[MSGLEN]; // cstring msg buffer
char cval[TOKLEN]; // number as cstring
//==================================================================
// string tools
//==================================================================
int16_t strstrpos(const char * haystack, const char * needle) // find 1st occurance of substr in str
{
const char *p = strstr(haystack, needle);
if (p) return static_cast<int16_t>(p-haystack);
return -1; // Not found = -1.
}
char * cstringarg( const char* haystack, const char* vname, char* sarg ) {
int i=0, pos=-1;
unsigned char ch=0xff;
const char* kini = "&"; // start of varname: '&'
const char* kin2 = "?"; // start of varname: '?'
const char* kequ = "="; // end of varname, start of argument: '='
const int NLEN=30;
char needle[NLEN] = ""; // complete pattern: &varname=abc1234
strcpy(sarg,"");
strcpy(needle, kini);
strcat(needle, vname);
strcat(needle, kequ);
pos = strstrpos(haystack, needle);
if(pos==-1) {
needle[0]=kin2[0];
pos = strstrpos(haystack, needle);
if(pos==-1) return sarg;
}
pos=pos+strlen(vname)+2; // start of value = kini+vname+kequ
while( (ch!='&')&&(ch!='\0') ) {
ch=haystack[pos+i];
if( (ch=='&')||(ch==';')||(ch=='\0') ||(ch=='\n')
||(i+pos>=(int)strlen(haystack))||(i>NLEN-2) ) {
sarg[i]='\0';
return sarg;
}
if( (ch!='&') ) {
sarg[i]=ch;
i++;
}
}
return sarg;
}
//==================================================================
// setup
//==================================================================
void setup() {
Serial.begin(UARTclock);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
i0=0;
i1=1;
i2=-22;
i3=33;
i4=-444;
i5=5555;
i6= (uint16_t)B10101010*256;
i6+=(uint16_t)B10101010;
}
//==================================================================
// loop
//==================================================================
void loop() {
//-------------------------------------------------------------
// receive
int n=0;
char inChar;
if(Serial.available()) {
inputString=Serial.readStringUntil('\n');
inputString.toCharArray(mbuf, min( (int)inputString.length(), MSGLEN-1) );
}
//----------------------
// process mbuf!
// debug: check for changed i2 by Raspi
cstringarg(mbuf, "i2", cval); //
if(strlen(cval)>0) {
i2=(int32_t)atol(cval);
}
//----------------------
inputString="";
//delay
delay(1);
//-------------------------------------------------------------
// send
//Serial.flush(); // debug: not needed
char formatstr[MSGLEN];
strcpy(formatstr, "§");
strcat(formatstr, "&i0=%ld;&i1=%ld;&i2=%ld;&i3=%ld;&i4=%ld;&i5=%ld;&i6=%ld;\n");
sprintf(mbuf, formatstr, i0,i1,i2,i3,i4,i5,i6 );
//for (int i=0; i<strlen(mbuf); i++ ) Serial.print(mbuf[i]);
Serial.print(mbuf);
//delay?
delay(1);
i0++;
}
Raspi code:
Code:
/*
UART communication
send/receive string of tokens
*
Raspberry Pi master
ver 0702
*/
/*
* change log
* 0702: debug value (i2) from Raspi
* 0701: Serial Read delimiter, debug value (i0) from Arduino
* 0700: first adjustments
* 0669: UART 115200 baud *
* 0667: Arduino via USB = ACM0
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
#include <math.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <pthread.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#define byte uint8_t
char uart[128] = "/dev/ttyACM0";
int Serial;
const uint32_t UARTclock = 115200;
int32_t i0=0,i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0,i9=0; // int (general)
//int32_t i0,i1,i2,i3,i4,i5,i6,i7,i8,i9;
#define TOKLEN 30
#define MSGLEN 1024
#define iINVALID -29999
std::string inputString="";
char cval[TOKLEN]; // number as cstring
char mbuf[MSGLEN]; // cstring msg buffer
//==================================================================
// string tools
//==================================================================
int16_t strstrpos(const char * haystack, const char * needle) // find 1st occurance of substr in str
{
const char *p = strstr(haystack, needle);
if (p) return static_cast<int16_t>(p-haystack);
return -1; // Not found = -1.
}
char * cstringarg( const char* haystack, const char* vname, char* sarg ) {
int i=0, pos=-1;
unsigned char ch=0xff;
const char* kini = "&"; // start of varname: '&'
const char* kin2 = "?"; // start of varname: '?'
const char* kequ = "="; // end of varname, start of argument: '='
const int NLEN=30;
char needle[NLEN] = ""; // complete pattern: &varname=abc1234
strcpy(sarg,"");
strcpy(needle, kini);
strcat(needle, vname);
strcat(needle, kequ);
pos = strstrpos(haystack, needle);
if(pos==-1) {
needle[0]=kin2[0];
pos = strstrpos(haystack, needle);
if(pos==-1) return sarg;
}
pos=pos+strlen(vname)+2; // start of value = kini+vname+kequ
while( (ch!='&')&&(ch!='\0') ) {
ch=haystack[pos+i];
if( (ch=='&')||(ch==';')||(ch=='\0') ||(ch=='\n')
||(i+pos>=(int)strlen(haystack))||(i>NLEN-2) ) {
sarg[i]='\0';
return sarg;
}
if( (ch!='&') ) {
sarg[i]=ch;
i++;
}
}
return sarg;
}
//==================================================================
// serial TCP
//==================================================================
void loop() {
while(1) {
static bool stringComplete = false;
//-------------------------------------------------------------
// send
// debug
i2++; // change value to send back to Arduino
char formatstr[MSGLEN];
// debug, cut-down:
strcpy(formatstr, "§");
strcat(formatstr, "&i0=%d;&i1=%d;&i2=%d;&i3=%d;\n");
sprintf(mbuf, formatstr, i0,i1,i2,i3);
for(uint8_t i=0; i<strlen(mbuf); i++) { //
serialPutchar( Serial, mbuf[i]); // Send values to the slave
}
//delay?
delay(1);
//-------------------------------------------------------------
// receive
int n=0;
char inChar;
char cstr[TOKLEN];
inputString="";
stringComplete = false;
if (serialDataAvail(Serial)) {
while(!stringComplete && n<MSGLEN-1) {
if(n==MSGLEN-2) inChar='\n'; // emergency brake
else
inChar = serialGetchar(Serial);
if(inChar=='\n' || inChar>=' ') inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
n++;
}
}
if (stringComplete) {
//inputString.to_str(mbuf, len-1);
strcpy (mbuf, inputString.c_str() );
fprintf(stderr,"\n"); fprintf(stderr,mbuf); //fprintf(stderr,"\n");
// cstringarg( char* haystack, char* vname, char* carg )
// haystack pattern: &varname=1234abc; delimiters &, \n, \0, EOF
cstringarg(mbuf, "i0", cval); //
if(strlen(cval)>0) {
sprintf (cstr, "i0=%d \n", (int32_t)atol(cval));
fprintf(stderr, cstr);
}
cstringarg(mbuf, "i1", cval); //
if(strlen(cval)>0) {
sprintf (cstr, "i1=%d \n", (int32_t)atol(cval));
fprintf(stderr, cstr);
}
cstringarg(mbuf, "i2", cval); //
if(strlen(cval)>0) {
sprintf (cstr, "i2=%d \n", (int32_t)atol(cval));
fprintf(stderr, cstr);
}
cstringarg(mbuf, "i3", cval); //
if(strlen(cval)>0) {
sprintf (cstr, "i3=%d \n", (int32_t)atol(cval));
fprintf(stderr, cstr);
}
cstringarg(mbuf, "i4", cval); //
if(strlen(cval)>0) {
sprintf (cstr, "i4=%d \n", (int32_t)atol(cval));
fprintf(stderr, cstr);
}
inputString="";
stringComplete = false;
//delay?
delay(1);
}
}
}
//==================================================================
int main() {
printf("initializing..."); printf("\n");
// UART Serial com port
Serial = serialOpen (uart, UARTclock);
printf("starting UART loop..."); printf("\n");
loop();
serialClose( Serial);
exit(0);
}
output e.g.
Code:
§&i0=17628;&i1=1;&i2=17434;&i3=33;&i4=-444;&i5=5555;&i6=43690;
i0=17628
i1=1
i2=17434
i3=33
i4=-444
§&i0=17629;&i1=1;&i2=17435;&i3=33;&i4=-444;&i5=5555;&i6=43690;
i0=17629
i1=1
i2=17435
i3=33
i4=-444
§&i0=17630;&i1=1;&i2=17436;&i3=33;&i4=-444;&i5=5555;&i6=43690;
i0=17630
i1=1
i2=17436
i3=33
i4=-444
§&i0=17631;&i1=1;&i2=17437;&i3=33;&i4=-444;&i5=5555;&i6=43690;
i0=17631
i1=1
i2=17437
i3=33
i4=-444
§&i0=17632;&i1=1;&i2=17438;&i3=33;&i4=-444;&i5=5555;&i6=43690;
i0=17632
i1=1
i2=17438
i3=33
i4=-444
§&i0=17633;&i1=1;&i2=17439;&i3=33;&i4=-444;&i5=5555;&i6=43690;
i0=17633
i1=1
i2=17439
i3=33
i4=-444
§&i0=17634;&i1=1;&i2=17440;&i3=33;&i4=-444;&i5=5555;&i6=43690;
i0=17634
i1=1
i2=17440
i3=33
i4=-444
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! hangs up !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
hat jemand vielleicht doch die rettende Idee?
Lesezeichen