Use Second Serial Port of my Arduino Mega

Hello everyone hope my english is enough,

i have a Arduino Mega 2560.
I use Visual Studio 2019.
i use a Serial-to-Usb Cable (Lindy 42686) to communicate with my Arduino.
Tx2 Arduino Pin 18 ->Rx Cable Pin 2
Rx 2 Arduino PIN 17 -> Tx Cable Pin 3,
Arduino Ground (Power) -> Ground Cable Pin 5

I connected a ultrasonic HC-SR04 to my Arduino and send the distance via Serial port 2.

I have connection with Serialport from Usb on COM3 and connection with Serial-to-Usb Cable on COM4.

My problem now is that i only receive data like this sdaten = "nUVg?g?g\0nUVg?ge\f\0nUVg?gec\0nUV"

I also cant connect to the arduino with COM4. I get a timeout or someting.

Im completely arduino beginner so please be patient.

Thanks for help

Serial ports 1, 2, 3 are just used via the pins on the board. You need to post your code, describe what you really want to do, and post a diagramme.

And this

i use a Serial-to-Usb Cable (Lindy 42686) to communicate with my Arduino.
Tx2 Arduino Pin 18 ->Rx Cable Pin 2
Rx 2 Arduino PIN 17 -> Tx Cable Pin 3,

makes no sense whatsoever. For starters, pin 18 is Serial1, but why don't you use a standard USB cable like everybody else does? Much the same can be said for Visual Studio. There may well be "experts" around who are disdainful of anything else, but they don't include you, and mere mortals get by with the standard IDE to programme a Mega just fine. Check the "getting started". The Arduino IDE is free.

Hi, thanks for your reply.

It was a mistake. I mean Pin 16 Tx2 and Pin 17 Rx2.

I build a Gui with C# Code. I want to use the Serialport 2 to communicate with the Gui while using Serialport from Usb to communicate with my Visual Studio Debugger.

In my code i use one instance of Serialport -> Serial to communicate with Vs Debugger and one instance -> Serial2 to communicate with the Gui.

In the setup routine i use the begin function with the value 9600 for both.
Serial.begin(9600);
Serial2.begin(9600);

In my loop routine i wait for function available() -> Some data for me.
void loop()
{
if (Serial2.available())
{
switch(Serial2.read())
{
case 'A':
digitalWrite(22, HIGH);
break;
case 'a':
digitalWrite(22, LOW);
break;
default:
break;
}
Serial.println(Serial2.read());
}

I did a little test. I sended data from my Gui(COM4) to my Arduino(COM3 VS Debugger).

I sended numbers(from COM4) to Arduino(COM3) and with Serial.println(Serial2.read()) i can see the data on COM3 (Debugger). What i get are numbers which dont make sense.

It is difficult for me to explain the problem and i hope you understand me.

If i communicate with COM3 from Gui to the arduino everyting works!

Thanks

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data.

Use the 2nd example unchanged other than to have it read from whichever Serial port you have your USB-TTL cable connected to.

...R

Hello thank you for your reply,

i think you example dont solve my problem. I have no problem with receive the data. If i use the COM3(Usb Serialport) everyting works fine. I can read/write what ever i want and it works but with COM4 something weird happens. Maybe I misunderstood your answer.

If i send 1 from Gui via COM4 to arduino i receive -1. If i send 10 i get 6... im completly lost with this serial communication...

Look at the file

serial2.txt (813 Bytes)

razer1337:
but with COM4 something weird happens.

That's a Windows problem. I know nothing about Windows.

...R

I have really no clue what happen to this serial adapter. I ordered an other one... :confused:

razer1337:
If i use the COM3(Usb Serialport) everyting works fine. I can read/write what ever i want and it works but with COM4 something weird happens.

No surprise there. It appears that you have successfully installed Arduino on COM3, so I would not expect anything useful on COM4. Please read the stickies at the head of this forum on how to post code using tags </>

Hey, thanks for your reply,

how is it possible to use the other Serialport? It almost souns like it isnt possible at all... Is it possible that i didnt understand the basic of this kind of communication?

razer1337:
how is it possible to use the other Serialport? It almost souns like it isnt possible at all... Is it possible that i didnt understand the basic of this kind of communication?

Do you mean the other serial port on the Mega or do you mean COM4 on your PC?

There should be no problem using the extra serial ports on the Mega and if you have a good USB-TTL cable I would expect it to work with any of the PC's USB ports. However it seems to be a bit of a lottery as to which COMx gets assigned to the USB port.
And maybe you need to install a driver for the USB-TTL cable.

I have never had a problem using my USB-TTL cables on this Linux laptop.

Have you connected GND as well as Tx and Rx on the TTL end of the USB-TTL cable?

...R

razer1337:
Is it possible that i didnt understand the basic of this kind of communication?

Very likely. You appear to have two separate problem, both of which are probably spurious.

First, the COM ports imply the serial ports on Windows. When you plug Arduino into PC - using a standard USB cable, Windows senses it and assigns a COM port, in your case COM3. It is smart enough to assign the same port every time you plug it in. You probably could force Windows to assign it to another port. I have no idea of how you would do that and, much more to the point, no idea of why you would want to. Perhaps you might explain that, but don't waste too much time on it.

The standard USB from PC cable that you plug into Arduino is always on Serial0, called Serial, at the Mega end. No exceptions. This is shared with pins 0,1.

razer1337:
how is it possible to use the other Serialport? It almost souns like it isnt possible at all...

Second, Mega's other serial ports, Serial1,2,3 on pins 14>19 are not used for PC but instead for various peripheral devices - display, Bluetooth, USB host, etc.. This is not only very possible, but also a lot simpler than you think it is. Pins 0,1 may also be used for peripheral devices if you need to, and pins 14>19 need not be reserved for serial devices.

It could be that what you really want to do is beyond Mega's ability, but your post is so incoherent that that is just a guess, and I suspect it is rather more likely that you are simply on the wrong tram, and it will come out OK in the end. One thing you might do though, is refrain from buying any more of those cables until you know what you are doing..

I would start by using software that is basically known to work; that will exclude problems in your C# code. For the debugging on COM3, use serial monitor; for communication on COM4, use a terminal program; I use RealTerm, but there are others.

Run the below program on the Arduino

void setup()
{
  Serial.begin(57600);
  Serial2.begin(57600);
}

void loop()
{
  if(Serial.available() > 0)
  {
    Serial2.write(Serial.read());
  }

  if(Serial2.available() > 0)
  {
    Serial.write(Serial2.read());
  }
}

Note that I use 57600 as the baudrate as that is the default for RealTerm.

Start both serial monitor and the other terminal program. Whatever you type in serial monitor should show in the other terminal program and vice versa.

I've just tested it with the above program and it works. Once you have that working, you can work on your C# code.

I also cant connect to the arduino with COM4. I get a timeout or someting.

Be accurate, "or something" is not accurate. And this statement does not say what you did; if you tried to upload code via COM4, that will never work (you can only upload via the USB port on the board).

Thanks guys for your help. I ordered:

Now it works as it should. (Debug and use my app at the same time). Now i use a 32 Bytestream R/W on my Arduino and C# App. But one (problem) left. I can only receive 32 Byte at ones on my C# App. If i send for example 64 Byte i will get two DataReceived Event(remember raw data!) with each 32 Byte. It doesnt matter how big my Buffersize(>32Byte) is on both size.

Maybe someone know this problem and give me some advices.

Thanks

There is no guarantee that data comes in in one batch in your C# application. So you need to collect all data (e.g. couint till 64 for your example) before processing it.