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.
8 years, 4 months ago.
how do i receive serial data from mbed nucleo F042K6?
I will be using beaglebone to send the data. I will be sending this data from the serial terminal and want to receive it on the mbed. But befor that, I want to do a simple test program 1st. I want to use this 4 bytes to simulate the receive, 0x01, 0x02, 0x03, 0x04. I dont think this is correct. It's from one tutorial i saw. but i didn't know how to change it. How do i do it ?
Serial pc(USBTX, USBRX); int main() { char a[4] = { 0x01, 0x02, 0x03, 0x04 }; pc.gets (a); pc.printf("got the byte", a); }
1 Answer
8 years, 4 months ago.
// include the mbed library #include "mbed.h" // declare the serial port. Serial pc(USBTX, USBRX); // our fake data const int fakeDataSize = 4; const char fakeData[4] = { 0x01, 0x02, 0x03, 0x04 }; // report the fake data one byte at a time. char getcFake() { // track which byte to send next (static = remembered between function calls) static int count = 0; // if at the end of the data go back to the start (just in case this is called more than 4 times) if (count == fakeDataSize) count = 0; // return the appropriate byte from fakeData and increase the counter. return fakeData[count++]; } int main() { // repeat 4 times for (int i = 0; i < 4 ; i++) { // read a byte. char datain = getcFake(); // replace with pc.getc(); for real data // %02X = output as 2 characters in hex, adding leading 0s if needed. \r\n = <CR><LF>, the standard new line sequence pc.printf("Got the byte 0x%02X\r\n", datain); } }
actually there is a relay. so the bytes for the relay is { 0xAB, 0x01, 0x99, 'STATE' } when the relay is off its STATE = 0x00, when on STATE = 0x01. . stm32 nucleo with my pir and ultrasonic (i call this node A). stm32 nucleo with the relay(i call this node B). when Node A sense no people in the room, it will send a command to BeagleBone. Then my BeagleBone will send 0xab, 0x01, 0x99, ‘STATE=0x00’ to Node B to ask Node B to turn off the relay. means like Node B receive from the beaglebone. i do this but it not work.how to do it correctly?
#include "mbed.h" Serial pc(SERIAL_TX, SERIAL_RX); Serial xbee1(PA_9, PA_10); DigitalOut Relay(PB_0); int main() { const char Relay[3] = { 0xAB, 0x01, 0x99 }; for (int i = 0; i<3; i++) xbee1.getc(Relay[i]); if (on) xbee1.getc(0x01); else xbee1.getc(0x00); while(1) { if (Relay){ pc.printf("on"); for (int i = 0; i<3; i++) xbee1.getc(Relay[i]); } else { for (int i = 0; i<3; i++) xbee1.getc(Relay[i]); pc.printf("off"); } } } }
You seem to have missed how getc() works. You don't pass it anything, it's receiving data not sending it. As shown in the code I posted previously, which you seem to have ignored, it returns the value received.
The rest of the structure of your program is fundamentally wrong, I'm not sure how you think it should work because I can't see any logic at all to the structure. You are making decisions based on undefined variables and the status of an output rather than the input. You are trying to read the input but bassing the read function a constant and never checking the result. You have variables with the same name as outputs.
You keep asking a question, waiting for an answer and then immediately asking another question about adding an extra feature. You keep making such fundamental mistakes in basic logic and structure that it's clear you've never taken the time to work through a programming tutorial or course and have at best skimmed through one.
Spend a couple of days and working through a tutorial making sure you understand the logic being used and why things are done a certain way rather than just where to put brackets and semicolons.
This board is for support, not writing your code for you or teaching the fundamentals of programming.
That said, here's your code. I'm not going to write any more for you.
#include "mbed.h" Serial pc(SERIAL_TX, SERIAL_RX); Serial xbee1(PA_9, PA_10); DigitalOut Relay(PB_0); const char RelayCommand[3] = { 0xAB, 0x01, 0x99 }; int main() { int byteCount = 0; // where we are in the header char byteIn; while (true) { byteIn = xbee1.getc(); // read from the radio if (byteCount < 3) { // still looking for the header if (byteIn == RelayCommand[byteCount]) // data in matches the next byte in the header byteCount++; // expect the next header byte else if (byteIn == RelayCommand[0]) // Got the first byte of a header when we didn't expect it byteCount = 1; // expect the second byte next else byteCount = 0; // No idea what's going on. Go back to the start } else { // got the full header. if (byteIn == 0x01) { Relay = 1; pc.printf("off"); } else if (byteIn == 0x00) { Relay = 0; pc.printf("off"); } byteCount = 0; } } }