Im trying to port my old mbed code to mbed 6 and it completly broke my serial communication.
Im using buffered serial like so:
static BufferedSerial pc(P0_2, P0_3);
char pc_buf[3] = {};
pc.set_baud(9600);
bool debug_on = true;
void commandManager(char c[]){
switch(c[0]){
case 'w':
//Confirm command is recieved and is being executed for teleop purposes
pc.write(pc_buf, c[0]);
fwd();
break;
default:
break;
}
}
int main(){
while(1) {
char tempcommand = pc.read(pc_buf, sizeof(pc_buf));
if(debug_on == true){
printf("pc_buf result is: ");
pc.write(pc_buf, tempcommand);
}
commandManager(&tempcommand);
}
}
when I try to enter w, or any other commands I have in my command manager, the letter im typing is printed back to be with pc.write(I think) but none of the printf(s) are printed to the console,
UNLESS i spam type keys, then multiple printf(s) are printed back at once, but they don’t contain the tempcommand they are supposed to print with, making me think the tempcommand chars are not being read.
my setup worked pre-mbed 6 with mbed 4> code, but after I tried to port it to mbed 6, it now fails like above. what am I doing wrong?
Well for one thing, what you have as char tempcommand is actually getting set by pc.read() to the number of characters read. I think you want to be using pc_buf in most of the places you’re using tempcommand.
Thank you for the sample code, I took bits and peices of it and ended up with this which reads and writes like my pre mbed6 code!
switch(c[0]){
case 'w':
//Confirm command is recieved and is being executed for teleop purposes
pc.write(&c[0], sizeof(char));
fwd();
//command = '\0';
break;
default:
break;
}
int main(){
size_t totalSize = 0;
char buff[BUFFSIZE] = {};
char c;
buff[totalSize] = '\0';
while (1){
pc.read(&c, sizeof(char));
//im too lazy to clear buffer, so I do this instead.
pc.write(&buff[0], sizeof(char));
commandManager(&c);
}
}