I'm using the library SerialTransfer to send an integer between two arduino unos. The problem is that the receiver is receiveng the data, but it's displaying 0 everytime..
Here below there is my code:
TX
#include "SerialTransfer.h"
#include <SoftwareSerial.h>
SoftwareSerial s(3,2); //Rx, Tx
SerialTransfer myTransfer;
struct STRUCT {
int num;
//int negnum;
} testStruct;
int randnum;
void setup() {
Serial.begin(9600);
s.begin(9600);
myTransfer.begin(s);
}
void loop() {
randnum = 10 + random(1,15);
testStruct.num = randnum;
myTransfer.txObj(testStruct, sizeof(testStruct));
myTransfer.sendData(sizeof(testStruct));
Serial.print(testStruct.num); //Serial.println(testStruct.negnum);
delay(500);
}
RX
#include "SerialTransfer.h"
#include <SoftwareSerial.h>
SerialTransfer myTransfer;
struct STRUCT {
int num;
//int negnum;
} testStruct;
SoftwareSerial s(3,2); //Rx, Tx
void setup() {
Serial.begin(9600);
s.begin(9600);
myTransfer.begin(s);
}
void loop() {
if(myTransfer.available()) {
myTransfer.rxObj(testStruct, sizeof(testStruct));
Serial.print("number positive: "); Serial.print(testStruct.num); //Serial.print("number negative: "); Serial.println(testStruct.negnum);
}
else if(myTransfer.status <0) {
Serial.print("ERROR");
Serial.println(myTransfer.status);
}
}
Why is this happening? How can I fix this?
Have you connected
Arduino1 Arduino2
RX ------------> TX
TX ------------> RX
and both GND together ?
RV mineirin
I tried the code with 2 Unos and get the same results. I do not know how to fix the code. I use code based on the serial input basics tutorial . The methods are not hidden in a library in that tutorial code. I believe that the tutorial teaches the basics of serial comms better, but that's me.
Hi, try this sketchs.
Here it worked correctly;
RV mineirin
// Transmiter
#include "SerialTransfer.h"
#include <SoftwareSerial.h>
SoftwareSerial s(3, 2); //Rx, Tx
SerialTransfer myTransfer;
struct STRUCT {
int num;
} testStruct;
int randnum;
void setup() {
Serial.begin(9600);
s.begin(9600);
myTransfer.begin(s);
}
void loop() {
randnum = 10 + random(1, 15);
testStruct.num = randnum;
uint16_t sendSize = 0;
sendSize = myTransfer.txObj(testStruct, sendSize);
myTransfer.sendData(sendSize);
delay(500);
}
// Receiver
#include "SerialTransfer.h"
#include <SoftwareSerial.h>
SerialTransfer myTransfer;
struct STRUCT {
int num;
} testStruct;
SoftwareSerial s(3,2); //Rx, Tx
void setup() {
Serial.begin(9600);
s.begin(9600);
myTransfer.begin(s);
}
void loop() {
if(myTransfer.available()) {
uint16_t recSize = 0;
recSize = myTransfer.rxObj(testStruct, recSize);
Serial.println(testStruct.num);
}
else if(myTransfer.status <0) {
Serial.print("ERROR");
Serial.println(myTransfer.status);
}
}
1 Like
system
Closed
September 17, 2021, 5:14pm
5
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.