Hardware Serial sending data using USART.

Post here first, or if you can't find a relevant section!
Post Reply
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

Hardware Serial sending data using USART.

Post by mustaq_ahm »

I am attempting to to send a message or from one STM32F103C8T6 board to another STM32F103C8T6 board. I have chosen PA10 and PA9 as my Rx and Tx for both the boards. And I have also assigned driver enable pin for both the pins (For Master PB15, which is always HIGH and for Slave PC14, which is always LOW). I am using HardwareSerial to read and write my data.

But I could able to send the data exactly to the slave from master. The ouput is printing just "??????". I dont know exactly where I am doing mistake. I am also attaching the screenshot and the codes that I am using to test the hardware serial.

My main task is to read and write the sensor values from the both the boards and and send them acorrding to algorithm given to the boards.


Hoping for responses from you.

Here below I am posting the code for my Master:

Code: Select all

#define enable PB15

HardwareSerial myserial(PA10, PA9);

char firstname[7] = "Mustaq";


void setup() 
{
  pinMode(enable, OUTPUT);
  digitalWrite(enable, HIGH);
  Serial.begin(115200);
  myserial.begin(19200);
}

void loop() 
{
  myserial.write(firstname);
  Serial.println(firstname);
  delay(1000);
}
Here below I am posting the code for my Slave:

Code: Select all

#define enable PC14

HardwareSerial myserial(PA10, PA9);

char myname[7];
byte nextchar = 0;
byte currentchar = 0;

void setup() 
{
  pinMode(enable, OUTPUT);
  digitalWrite(enable, LOW);
  Serial.begin(115200); 
  myserial.begin(19200);
}

void loop() 
{
  Serial.println("before while");
  while(myserial.available());
  {
    Serial.println("inside while");
    currentchar = myserial.read();
    myname[nextchar] = currentchar;
    nextchar++;
    Serial.println("one loop of while is done");
  }
  Serial.println(myname);
  delay(1000);
}
I am programming in stm32f103c8t6 in Arduino IDE with the current STM32 core files for STM32 based boards (official STM core, HAL based)
https://github.com/stm32duino/Arduino_Core_STM32. I am working on Windows 10. My Arduino IDE version 1.8.5.
Attachments
The file has been compressed.
The file has been compressed.
Webp.net-compress-image (1).jpg (89.1 KiB) Viewed 2164 times
by mustaq_ahm » Tue Sep 14, 2021 3:00 pm
Thank you for your time and replies. It was my simple mistake in code. Yes you correct nextchar++ is just index for myname.

In both master and slave. I have given different baud rates. That was causing the problem I think.

Also in Slave code. I removed semicoln in while statement. :D

Now everything works fine.
Go to full post
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Hardware Serial sending data using USART.

Post by fpiSTM »

nextchar++; OK but it is never reset to 0. ;)
Moreover you suppose the while(myserial.available()); will only read the "firstname" but nothing ensure that only one is received... mainly because you made several print.
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

Re: Hardware Serial sending data using USART.

Post by mustaq_ahm »

I added several print in my code because, so that I can debug in my own code in serail monitor.

Yes, you are right but myserial.available() will check for number bytes to recive when it is zero it will exit the while loop right?

I have also used nextchar++ so that it will store byte by byte to receiving end (myname) from the sending end(firstname).

Could you please elaborate where I am wrong?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Hardware Serial sending data using USART.

Post by fpiSTM »

nextchar is used as index of myname array.
So as you never reset it to 0 you go out of the array after 6 while loop...
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

Re: Hardware Serial sending data using USART.

Post by mustaq_ahm »

Thank you for your time and replies. It was my simple mistake in code. Yes you correct nextchar++ is just index for myname.

In both master and slave. I have given different baud rates. That was causing the problem I think.

Also in Slave code. I removed semicoln in while statement. :D

Now everything works fine.
Post Reply

Return to “General discussion”