so I have a big problem with getting this sorted out.
I am running an UNO connected to the PC via USB and it communicates with a train simulator, simply makes a LED go on when someting happens in the game.
There is no reason why ArduinoA should not pass data to ArduinoB via a Serial link and then have ArduinoB pass it on to the PC along with data directly from ArduinoB
How do you normally send the data to the PC from the Mega ? That will not change
To get the data from the Uno to the Mega you "print" it to a serial port (see below) and on the Mega you read it on a Serial port when it becomes available
You cannot sensibly use the normal Serial port on the Uno because it is used by the Serial monitor and to upload to the PC so will need to use SoftwareSerial on any pins except 0 and 1. There are examples in the IDE
To receive the data on the Mega you should use one of its serial ports other than Serial, ie Serial1, Serial2 or Serial3 and connect the data leads from the Uno to the associated pins on the Mega
The baud rates on the Uno and Mega must match and there must be a common GND connection between them
Basically you print data on the Uno, read it on the Mega and once read print it to the PC
#include "SoftwareSerial.h"
SoftwareSerial toMega(10, 11); //Rx and Tx pins of your choice
void setup()
{
toMega.begin(9600); //start the interface to the Mega
}
void loop()
{
toMega.println("Hello Mega"); //send data to the Mega
delay(1000);
}
You can send any data type that you like to the Mega by "printing" it but parsing it on the Mega can be "interesting"
Could you help me to convert this code to work with the uint8_t from the PC?
What I want to do in the code is to read the data bytes from the PC via the ARDUINO MEGA and make the connected ARDUINO UNO react to this with digitalWrite(13, HIGH).
In the same way that the Uno can send data to the Mega and the Mega can pass it on to the PC the Mega can receive data from the PC and pass it on to the Uno
Don't try to write this all in one go. Start with the code in reply #8 and get the Uno passing data to the Mega. When it is received on the Mega just print it. When you have got that working have the Mega pass it on to the PC and print it in the Serial monitor
What is this train simulator? Is there a specification of the communication protocol for it? Does it always send 52 bytes? Does it always expect 20 bytes as a reply?
How many modules will be connected to the Mega (think future)? You only have three additional serial ports available on the Mega.
This reads: wait till one or more bytes are available and regardless wether 1, 2, ... or 52 bytes are available read 52 bytes. So what happens if there are less than 52 bytes available?
Why do you cast to a char pointer? doPC is a byte array, you should be able to send that directly without a cast.
there is no specification of the communication protocol for it. A few guys asked the developers to make the possibility to connect the sim to external devices and then made the code for Arduino. I don't know why is it like this, but it works.
I am planning max 2 external UART modules.
I am really confused because I do understand the code for communicating with the PC but I don't know how to get 2 Arduinos work together in order to do it.
Serial communication between 2 Arduinos is the same as communication between an Arduino and the PC
In the latter case the Arduino transmits some data via Serial.print() or Serial.write(), it is received by the PC and used, maybe just written to the Serial monitor. Data in the other direction is sent from the PC, received by the Arduino then used to do something
Arduino to Arduino Serial communication is exactly the same except that you substitute a second Arduino for the PC