Serial Communication issue between arduino and PC

Hi,

I am trying to send data to PC from arduino. I am using TTL to RS232 converter and then RS232 to USB converter .

I am able to send data only around 100 times then i have to reset arduino to start resending data.

I have tried with different baud rate but no help.

Please help.

I am trying to send data to PC from arduino.

Which Arduino?

I am using TTL to RS232 converter and then RS232 to USB converter

Why aren't you using the regular USB cable?

I am able to send data only around 100 times then i have to reset arduino to start resending data.

I'm guessing that there is something wrong with your code.

Hi Paul,

Thanks for reply. I am using Arduino Uno.

What do you mean by regular usb. I have read data transmitted by Arduino in scada software.

In scada i have written vbscript to read usb port.

I am able to see data transmitted by arduino on serial monitor but somehow it is not coming on usb port continuously means it works for few time then it stops.

What do you mean by regular usb.

The one that you use to upload sketches to the Arduino.

I am able to see data transmitted by arduino on serial monitor but somehow it is not coming on usb port continuously means it works for few time then it stops.

I still suspect that there is something wrong with your code.

scada software was not able to read regular usb port.

How to check if Serial.print is printing data on serial monitor but still arduino is not sending data ?

Krunalsidd:
How to check if Serial.print is printing data on serial monitor but still arduino is not sending data ?

I do no understand. Are you saying that it works with serial monitor but not with scada?

No ...

I am able to send data from arduino via serial to USB converter to scada and receive back feedback also but problem is it is not working continuously .

I am able to send data and receive feedback some 50-100 time and then it stop and after that i have to reset the system or power off and it will work again for 50-100 time but i want it to work continuously 24x7.

Krunalsidd:
but problem is it is not working continuously .

The real problem is that you have not yet posted the program that is causing the problem - in spite of several hints.

...R

I have attached my code.

One more point when i am not receiving data at output of converter at that time also i am able to see arduino transmitted data (checked on serial monitor)

sketch_oct29b.ino (4.36 KB)

Krunalsidd:
I am able to send data only around 100 times then i have to reset arduino to start resending data.

Now that I can see your program my first suspect is your use of the String class.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Separately, it looks like your code for receiving data is similar to the code in Serial Input Basics. If that is where it came from it would have made your program much easier to figure out if you had left the code for reading data in functions rather than copying it into loop() where it just gets in the way of seeing what you are trying to do.

...R

I have one question

My understanding is correct that if my Serial.print's output is visible on serial monitor that means arduino has transmitted data.

If above my understanding is correct then there is some issue in converter because after sometime when communication stops still at that time i am able to see data on serial monitor but scada is not receiving it.

Krunalsidd:
My understanding is correct that if my Serial.print's output is visible on serial monitor that means arduino has transmitted data.

Yes, if it is continuing to output the correct data. But it is still not wise to use the String class :slight_smile:

If above my understanding is correct then there is some issue in converter because after sometime when communication stops still at that time i am able to see data on serial monitor but scada is not receiving it.

I don't have your converter and I don't think you have posted a link to its datasheet or a diagram showing how everything is connected.

It could also be a problem in the software that is receiving the data from the converter - in fact that seems more likely.

...R

This is the serial to rs232 converter i am using

this is rs232 to usb converter

https://www.elfa.se/en/usb-to-serial-rs232-converter-aten-uc232a/p/12519009

with above combination i am getting around 100 output

This is TTL usb converter i tried

With above converter i am getting less then 10 output

Krunalsidd:
With above converter i am getting less then 10 output

There is something very strange going on.

I regularly use a USB-TTL converter (not the exact same one as your link) and have NEVER had a problem with it ceasing to function after X iterations.

You need to write a pair of short test programs - one for the PC and one for the Arduino - as an aid to figuring this out. This Python - Arduino demo may help get you started.

...R

This line is diasbled; but it contains a bug if enabled

//sprintf(char1,"%02X",msg[index]);

char1 is only two bytes in size so there is no space for a terminating nul character.

Below has a similar problem

      if (index < 16) // Make sure there is room
      {
        msg[index] = incomingByte; // Add char to array
        //sprintf(char1,"%02X",msg[index]);
        strmsg += msg[index] ;
        index++;
        msg[index] = '\0'; // Add NULL to end
      }

msg is only 4 bytes in size; you however happily allow to store 16 bytes in there.

So you have a good chance of writing outside the boundaries of your arrays and when hat happens your code usually crashes.

But I can able to see arduino is transmitting data . I am able to see serial.pint output on serial monitor only somehow that ttl output is not getting converted in usb after few iterations.

If i reset my arduino then again it is working for few iterations.

My received data is of fix size and with modbus it is working fine

One more point this issue occurs when there is two way communication happening between my PC (scada) and arduino .

When there is single way communication means only arduino sending data to my PC . It works perfectly fine.

Is it something specific setting for two way communication ?

Fix the bugs that I pointed out. The behaviour that you observe is typical for memory issues; the bugs that I pointed out result in memory issues.

Not behind my computer at the moment but from memory those bugs are on the receiving code, so yes, if your scada does not send data, you will not have the problem.

I will increase size of msg to 16 from 4 but i have one doubt

If serial.print shows values on serial monitor does that not guarantee that arduino is transmitting data ?

because when my scada is not able to receive data at that time also i am able to see values on serial monitor

Still it looks like issue on arduino or memory corruption ?

Krunalsidd:
Still it looks like issue on arduino or memory corruption ?

You need to write some test programs like I suggested in Reply #13.

...R