hallo,
ich komme zur Zeit nicht weiter: meine UART Kommunikation zwischen Raspi und Arduino hängt sich ständig auf.
Ich vermute den Fehler eher Raspi-seitig, weil der Arduino-Code in ähnlicher Form bereits mit Borland C++ Builder auf dem PC funktioniert hat (wenn sich nicht ein c+p Fehler eingeschlichen hat, den ich allerdings noch nicht gefunden habe).
Der Arduino (momentan ein Mega2560) ist per USB mit einem Raspi-USB-Port verbunden ("/dev/ttyACM0") .
Hat jemand einen Tipp, das zu beheben?
Raspi code:
Code:/* UART communication send/receive string of tokens Raspberry Pi master ver 0101 */ #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[256] = "/dev/ttyACM0"; int Serial; const uint32_t UARTclock = 115200; int i0; #define TOKLEN 30 #define MSGLEN 1024 #define iINVALID -29999 std::string inputString=""; char cval[TOKLEN]; // number as cstring char mbuf[MSGLEN]; // cstring msg buffer //================================================================== // serial TCP //================================================================== void loop() { while(1) { static bool stringComplete = false; //------------------------------------------------------------- // send char formatstr[MSGLEN]; // debug, cut-down: strcpy(formatstr, "§"); strcat(formatstr, "message from Raspi: %d;\n"); sprintf(mbuf, formatstr, i0); for(uint8_t i=0; i<strlen(mbuf); i++) { // serialPutchar( Serial, mbuf[i]); // Send values to the slave } // strcpy(mbuf, ""); // <~~~~~~~~~~~~~~~~~~~~~~~~editiert! //delay? delay(10); // <~~~~~~~~~~~~~~~~~~~~~~~~editiert! //------------------------------------------------------------- // receive inputString=""; char cstr[TOKLEN]; int n=0; char inChar; while (serialDataAvail( Serial ) && n<MSGLEN-1) { if(n==MSGLEN-2) inChar='\n'; // emergency brake else inChar = serialGetchar( Serial ); inputString += inChar; if (inChar == '\n') { stringComplete = true; } n++; } if (stringComplete) { strcpy (mbuf, inputString.c_str() ); // strcat(mbuf, "\0"); // <~~~~~~~~~~~~~~~~~~~~~~~~editiert! fprintf(stderr,mbuf); fprintf(stderr,"\n"); // process mbuf ! 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); } // // eof
Arduino code:
Code:// Arduino COM to Raspi // (C) 2018 by dsyleixa // ver 0101 // I2C #include <Wire.h> int i0 = 0; #define TOKLEN 30 #define MSGLEN 1024 #define iINVALID -29999 String inputString=""; char cval[TOKLEN]; // number as cstring char mbuf[MSGLEN]; // cstring msg buffer //================================================================== // setup //================================================================== void setup() { Serial.begin(115200); pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW); } //================================================================== // loop //================================================================== void loop() { static bool stringComplete = false; //------------------------------------------------------------- // receive int n=0; char inChar; while (Serial.available() && n<MSGLEN-1) { if(n==MSGLEN-2) inChar='\n'; else inChar = (char)Serial.read(); inputString += inChar; if (inChar == '\n') { stringComplete = true; } n++; } if (stringComplete) { inputString.toCharArray(mbuf, MSGLEN-1); // process mbuf ! // inputString=""; stringComplete = false; //delay? delay(1); } //------------------------------------------------------------- // send //---------------------- char formatstr[MSGLEN]; strcpy(formatstr, "§"); strcat(formatstr, "message from Arduino: %d;\n"); sprintf(mbuf, formatstr, i0); for (int i=0; i<strlen(mbuf); i++ ) Serial.print(mbuf[i]); // strcpy(mbuf, ""); // <~~~~~~~~~~~~~~~~~~~~~~~~editiert! i0++; //delay? delay(10); // <~~~~~~~~~~~~~~~~~~~~~~~~editiert! } // end of file
console output:
dann bleibt es stehen...Code:initializing... starting UART loop... §message from Arduino: 0; §message fr211; §message from Arduino: 2212; §message from Arduino: 2213; §message from Arduino: 221�no: 2210; §message from Arduino: 2211; §message from Arduino: 2212; §message from Arduino: 2213; §message from Arduino: 221�
Restart, neuer output:
dann bleibt es wieder stehen...Code:initializing... starting UART loop... §message from Arduino: 0gno: 618; §message fr��message from Arduino: 615; §message from Arduino: 616; §message from Arduino: 617; §message from Arduino: 618; §message fr







Zitieren

Lesezeichen