Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 7 months ago. This question has been closed. Reason: Duplicate question
Issues with receiving serial data
I am transmitting 2-byte messages from the Max environment to a Nucleo F767zi via serial. The transmission works fine for individual messages, but when I send multiple messages that are "simultaneous", the board freezes and must be reset. I've tried both Serial and RawSerial and have compiled using online mbed and Platformio. I adapted the code and ran it on an Arduino Uno and it works fine. Help would be appreciated!
solenoid_board_serial
#include "mbed.h"
DigitalOut solenoid[] = {(LED1), (LED2), (LED3)};
RawSerial pc(USBTX, USBRX); // set tx, rx for serial object "pc"
volatile char c = 0; // Initialized to the NULL character
const char buffSize = 25;
volatile char inputBuffer[buffSize];
char bytesRcvd = 0;
const static char startMarker = 128;
const static char endMarker = 129;
bool readInProgress = false;
void noteOnOff(){
char pitch = inputBuffer[0] - 1;
char vel = inputBuffer[1];
if (vel > 0) {
solenoid[pitch] = 1;
}
if (vel == 0) {
solenoid[pitch] = 0;
}
}
void readSerial() {
while(pc.readable()){
c = pc.getc();
if(c == endMarker) {
noteOnOff();
readInProgress = false;
inputBuffer[bytesRcvd] = 0;
bytesRcvd = 0;
}
if(readInProgress) {
inputBuffer[bytesRcvd] = c;
bytesRcvd ++;
if (bytesRcvd == buffSize) {
bytesRcvd = buffSize - 1;
}
}
if(c == startMarker){
bytesRcvd = 0;
readInProgress = true;
}
}
}
int main() {
pc.attach(&readSerial);
while (1){}
}