Hi.
I have tried a lot of different approaches, to try to solve this.
The project is partly working. The main task is to get a lot of Int's (analog values from joystick,etc) from one arduino to another via serial.
I have also found a sample code that works, with just forwarding data from one serial port to another. (but this is just for debugging, not my main goal)
When i change that code, so i can save the variables to the plc before sending to other serial port it hangs.
This code works without hangs. But this is just forwarding.
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
Serial1.write(inByte);
}
}
I had different aproaches.
-
To line up all the int's in one long string and send it to the other, for sorting the long sting back into seperate int's.
(Have seen example code of this with comma separated values)
I have also used this method with success on an industrial plc. but there i had available function blocks to get the job done easily. -
To send each int in separate string with an starting address. (my current example)
This is now partly working. I think the problem occurs when i also send this data on the second serial line to my pc. because im not receiving any packages as long as the serial between the 2 arduinos are connected. but once i disconnect the TX on the arduino with 2 serials, it sends the buffered packages to the pc terminal. This happens everytime i connect/disconnect RX TX.
so it seem like the receiving serial are preventing the sending serial from working.
Here is the code for the sending arduino. (arduino uno in my case)
#include <SoftwareSerial.h>
String stringOne, stringTwo, stringThree, sendstring, endstring;
SoftwareSerial mySerial(0, 1); // RX, TX
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
// read the input on analog pin 0:
int sensorValue1 = analogRead(A0);
// read the input on analog pin 1:
int sensorValue2 = analogRead(A1);
// read the input on analog pin 2:
int sensorValue3 = analogRead(A2);
stringOne = sensorValue1;
stringTwo = sensorValue2;
stringThree = sensorValue3;
endstring = "x";
sendstring = "A";
sendstring = sendstring + stringOne + endstring;
Serial.println(sendstring); // prints A followed by sensorvalue
delay(1000);
sendstring = "B";
sendstring = sendstring + stringTwo + endstring;
Serial.println(sendstring); // prints B followed by sensorvalue
delay(1000);
sendstring = "C";
sendstring = sendstring + stringThree + endstring;
Serial.println(sendstring); // prints C followed by sensorvalue
delay(1000);
}
Here is the code for the receiving arduino (also the one with 2 serial ports in use, Arduino mega in my case)
The var incomingintA & incomingintB are the ones i will use internally in this plc.
String stringOne, stringTwo, sendstring, incomingString;
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
if (Serial1.available() > 0) {
incomingString = Serial1.readStringUntil("x");
delay (10);
}
if (incomingString.startsWith("A")) {
String incomingA = incomingString.substring(1);
Serial.println(incomingA.toInt());
int incomingintA = (incomingA.toInt());
}
if (incomingString.startsWith("B")) {
String incomingB = incomingString.substring(1);
Serial.println(incomingB.toInt());
int incomingintB = (incomingB.toInt());
incomingString = ("0");
}
}
Thanks in advance if anyone finds a solution for this