Hello! I am trying to receive a command (in the form of a string) from a serial port from my laptop to a STNUCLEO-F767ZI development board, store that data, then use it to control the frequency of a blinking LED and the time it stays on. The first half of my code works; it can receive and store a string of numbers as integers. However, Mbed OS crashes whenever it gets past that. Using Arduino IDE’s serial monitor, this is what is printed when I send over the string ‘1005000’:
I got '1005000
’
Servo velocity: ‘100’
Servo time: ‘5000’
++ MbedOS Error Info ++
Error Status: 0x80FF0144 Code: 324 Module: 255
Error Message: Assertion failed: !(flags & osFlagsError)
Location: 0x800A403
File: .\mbed-os\rtos\source\Thread.cpp+176
Error Value: 0x0
Current Thread: main Id: 0x20005E0C Entry: 0x800B159 StackSize: 0x1000 StackMem: 0x20004868 SP: 0x200057C8
For more info, visit: mbedos-error
– MbedOS Error Info –
= System will be rebooted due to a fatal error =
= Reboot count(=5) reached maximum, system will halt after rebooting =⸮
This is my code; I used the empty Mbed 6 program template, and the only file I edited is main.cpp. I am using the latest version of Mbed Studio, and all of my tools are up to date.
/*
- Copyright (c) 2020 Arm Limited and affiliates.
- SPDX-License-Identifier: Apache-2.0
*/
#include “mbed.h”
// Create a serial object
using ThisThread::sleep_for;
static BufferedSerial pc(USBTX, USBRX);
//variables start
int onTime = 20;
int servoVelocity = 0;
int servoTime = 0;
int timeLeft = 0;
//variables end
//functions start
int arrayConcat (char array, int
arrayStart
, int dataLength) {
int dummy = 0;
int dest = 0;
for (int i = 0; i < dataLength; i++) {
dummy = (array[i + arrayStart] - 48) * pow(10, dataLength - i - 1);
dest = dest + dummy;
}
return dest;
}
//functions end
int main(void)
{
//setup
pc.set_baud(9600);
char buffer[10] = {};
PwmOut servoXPin(LED1);
//setup end
while (1) {
if (pc.readable()) {
sleep_for(100ms);
pc.read(buffer, 10); //get data from pc and store it in buffer
printf("I got '%s'\n\n", buffer);
servoVelocity = arrayConcat(buffer, 0, 3);
servoTime = arrayConcat(buffer, 3, 4);
printf("Servo velocity: '%d'\n", servoVelocity);
printf("Servo time: '%d'\n", servoTime);
}
while (timeLeft) {
servoXPin.period_ms(servoVelocity);
servoXPin.pulsewidth(.5f);
timeLeft -= servoVelocity;
}
for (int i = 0; i <= 10; i++) {
buffer[i] = {}; //clear buffer
}
}
}