Good Morning, I'm new to Arduino and I'm attempting to communicate with RS232, RS485 and Canbus although I'm struggling with selecting the best hardware for the job.
I've seen the UNO R4 has onboard canbus but it needs an external receiver, plus extra adaptors for rs485 and rs232.
The MKR unit has shields for both canbus and RS485 so I'll just need an adapter for TLL to RS232.
I'm wondering if any boards have all 3 communications types built in? or if there's a simpler/neater way of doing this without lots of different adapters.
I'm assuming If I used multiple adapters, I would need multiple communication pinouts/channels for each communication type. I wouldn't be able to use one TTL pinout for all 3 adapters etc... If that makes sense.
For example the GIGA R1 WIFI has 4 UART ports and 1 Canbus port which requires an external tranceiver. I could put RS232 and RS485 on 2 of the UART ports with adapters and my canbus on the canbus ports with a external transceiver. (I couldn't put both rs232 and rs485 on the same UART port even if I was to communication on the port individually).
Hardware wise, seems like it will be six of one half dozen of the other, chose whatever you like best. However you should also consider what libraries are available to do what you want. Many of the tried and true libraries for RS485, CAN and Serial were developed for the AVR processors and you will find many examples/tutorials for them.
It has three serial ports but I'm guessing I'll need an adapter for both RS232 and RS485 as they run on different + inverted voltages, unlike TTL which utilizes 0-5v high and low signal.
Also, what is the exact idea of libraries? I just wrote a quick serial communication program with 0 prior experience of programming which wasn't too hard. So I'm not so sure what the libraries are actually used for.
the MCP2515 board in the photo has a canbus transciver on the PCB - it is being driven by the MCP_CAN_lib library
the use of the ESP32 two-wire interface eliminates the MCP2515 and all that is required is a canbus transciver
you may need to explicitly include a library such as for the two-wire, WiFi, BLE, DAC, etc
however many support libraries are included automatically, e.g. to drive the GPIO, Serial IO, etc
apart from custom PCBs never seen a board with all builtin - maybe worth having a look at LILYGO boards
make sure you test each module in a separate program before attempting to implement the complete project
Right makes sense, I just need a board with at least 2 UART ports and 1 can-bus port. Again.. assuming most boards don't have an in-built transceiver so I'll need a external transceiver for the can-bus? and obviously the RS communication converters too?
You can use software serial on digital pins, but it has many limitations for max baudrate, serial parameters and rx/tx simultaneously. Ideally you reserve one UART for uploading code and serial monitor.
Esp32 would be better choice.
Serial.print() prints to the IDE Serial monitor via the USB port
RS485.print() is printing to a hardware or software serial port defined as RS485
e.g. in the following Serial1 is a ESP32 hardware serial port
// ESP32 Serial1 test - for loopback test connect pins RXD1 and TXD1
#define RXD1 16 // can map Serial1 and Serial2 to many ESP32 GPIO pins
#define TXD1 17 // check pin usage https://randomnerdtutorials.com/esp32-pinout-reference-gpios/
// for RS232 shield connect
// ESP32 RXD1 to TTL/RS232 Rx
// ESP32 TXD1 to TTL/RS232 Tx
// connect GND pins together and VCC to 3.3V on ESP32 5V on UNO ect
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)
void setup() {
// initialize both serial ports:
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
Serial.printf("\n\nESP32 serial1 test RXD1 pin %d TXD1 pin %d\n", RXD1, TXD1);
Serial.printf(" loopback test connect pin %d to pin %d\n", RXD1, TXD1);
Serial.printf("RS232: ESP32 pin %d RXD1 to TTL/RS232 Rx and pin %d TXD1 to TTL/RS232 Tx\n", RXD1, TXD1);
Serial.printf("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}
void loop() {
// read from Serial1, send to Serial
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
// read from Serial, send to Serial1
if (Serial.available()) {
int inByte = Serial.read();
//Serial.write(inByte); // local echo if required
Serial1.write(inByte);
}
}
couple of test runs serial monitor output
ESP32 connected to a Mega
SP32 serial1 test Rx pin 16 Tx pin 17
for loopback test connect pin 16 to pin 17
loopback test 1
loopback test 2 abcdefghijk 000000000000000
hello from mega
test 2 from mega
test 3 from mega 1234567890
------------------------------------------------------------
Arduino Mega Serial1 test - for loopback test connect pin 18 to pin 19
RS232: Mega pin 18 TXD to TTL/RS232 Tx and pin 19 RXD to TTL/RS232 Rx
RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx
loopback test1
loopback test 2 1234567890 llllllllllll
hello from ESP32
test 2 from esp32
test 3 from ESP32 12345678900987654321
you connect the external RS232 or RS485 modules to specified pins - say RS232 to GPIO16/GPIO17 and RS485 to GPIO18/GPIO19
you program opens two hardware serial ports Serial1 and Serial2 vso
#define RXD1 16 // can map Serial1 and Serial2 to many ESP32 GPIO pins
#define TXD1 17
#define RXD2 18
#define TXD2 19
void setup() {
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
bytes read/written from/to Serial1 communicates with the RS232 module
bytes read/written from/to Serial2 communicates with the RS485 module
depends on the LCD and the library supporting it
what LCD are you using?
avoid using screen images if possible - they are difficult to read and waste space
copy the TEXT and post using code tags </>
some graphics libraries support println() statements
for example, running the graphicstest example from the Adafruit_ILI9341 library
using code such as
Where did you find this code, and what processor is it for? I ask, because there are numerous deficiencies/redundancies.
Did this come from ChatGPT, or some other AI, perchance? If so, please always identify when using these sources for code, as it will generally require more effort to explain to you what's wrong with it.