5 years, 8 months ago.

Sending String via Serial

Hello,

I would like to send a string through my serial communication. However I can't find any documentation on "puts" function and I don't know if I'm using it right. I connected my Mbed to an Arduino and its seems like Arduino doesn't receive the string.

Here is my code. When I press 6 or 9 it should send the correct String.

Mbed String code

#include "mbed.h" 

Serial pc(USBTX, USBRX); // TX, RX
Serial TXRX(p13, p14); //TX, RX
char* s; 
 
int main(){
    pc.baud(9600);
    TXRX.baud(9600);
 
    while(1){
        if(pc.readable()){
            char x = pc.getc(); 
            wait_ms(100);
            
            if(x=='9'){pc.printf("%c\n",x);s="1,2,3";}
            if(x=='6'){pc.printf("%c\n",x);s="7,8,9";}
            pc.printf("%s\n",s); 
            TXRX.puts(s); //may be the problem
        }       
    }
}

When I send a single char via "putc", my Arduino receives it well. So I assume "puts" is the problem.

Mbed single char code

#include "mbed.h" 

Serial pc(USBTX, USBRX);    // tx, rx
Serial TXRX(p13, p14); //TX, RX
 
int main(){
    pc.baud(9600);
    TXRX.baud(9600);
 
    while(1){
        if(pc.readable()){
            char x=pc.getc();
            wait_ms(100);
            TXRX.putc(x);
        }       
    }
}

This one is working.

And here is my Arduino code.

Arduino code

#include <Wire.h>
#include <Zumo32U4.h>

String TrameValeurs;
  
void setup() {
  Serial.begin(9600);
}

void loop() { 
  TrameValeurs = Serial.readString();
  //Serial.read(&TrameValeurs, sizeof(TrameValeurs));

  Serial.print("String :\n");
  Serial.print(TrameValeurs);
  Serial.print("\n");
  
  int virg1, virg2, virg3;
  String inst1, inst2, inst3;
  
  virg1 = TrameValeurs.indexOf(','); 
  inst1 = TrameValeurs.substring(0, virg1);
  virg2 = TrameValeurs.indexOf(',', virg1 + 1);
  inst2 = TrameValeurs.substring(virg1 + 1, virg2);
  virg3 = TrameValeurs.indexOf(',', virg2 +1); 
  inst3 = TrameValeurs.substring(virg2 + 1, virg3);

  Serial.print("instructions : \n");
  Serial.print(inst1);
  Serial.print(",");
  Serial.print(inst2);
  Serial.print(",");
  Serial.print(inst3);
  Serial.print("\n");

  if((inst1=="7")&&(inst2=="8")&&(inst3=="9")){
    //Serial.write(10);
    ledRed(0);
    ledYellow(1);
    ledGreen(0);
  }
  if((inst1=="1")&&(inst2=="2")&&(inst3=="3")){
    //Serial.write(20);
    ledRed(1);
    ledYellow(0);
    ledGreen(1);
  }
  else{
    ledRed(0);
    ledYellow(0);
    ledGreen(0);
  } 
}

Thank you

So,

I tried all of your methods above but It still doesn't receive anything

/media/uploads/equalizer/serial_communication.png

From the Arduino, could "TrameValeurs = Serial.readString();" be the problem ?

posted by equalizer equalizer 01 Mar 2019

Ok guys I found my problem,

I am really dumb cause on my Arduino I was reading a string coming from my pc and not from my Mbed by using Serial instead of Serial1. Thank you all :)

Edit : String is correctly sent

posted by equalizer equalizer 01 Mar 2019

3 Answers

5 years, 8 months ago.

I think your problem is s="1,2,3", that line is setting a pointer to a constant value not setting a pointer to point at a constant string. At no point is any memory allocated to hold the text.

Try the following changes

//char *s;
char s[16];

//s="1,2,3";
sprintf(s,"1,2,3");

Accepted Answer
5 years, 8 months ago.

You could try:

snip

      char container[50]; //give 50 character size to be safe
            
      sprintf(container,"%s",s); 
      TXRX.puts(container); 


      // or something different like- 

    int len;
    len=strlen(s);
    while(len--)
        TRX.putc(*s++);


But I haven't tested this.

5 years, 8 months ago.

Hi, you can use variable of the string type.

you can try..

#include "mbed.h"
#include <string>
 
Serial pc(USBTX,USBRX); 
 
string testString;
 
int main() {
    
    testString = "Hello";
    
    while (1) {  
         pc.printf("printf: %s\n", testString.c_str());
         wait(1);
//or
         pc.puts(("putc:" + testString + "\n").c_str());
         wait(1);
    }
}