Serial Communication Issue with Arduino UNO R3

I am using Arduino UNO and Relay Kit to operate electrical appliance from my computer. I am using visual basic 6 for serial communication.

Everything works fine for sometime (I mean for 10-15 communications) but after that Arduino doesn't respond and my visual basic application hangs until i re-plug the USB cable of Arduino.

Here is my Arduino Program...

int led = 13;
int led1 = 12;
String incoming;

void setup()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
pinMode(led1, OUTPUT);
digitalWrite(led1, HIGH);
}
void loop()

{
  if (Serial.available() > 0)
  {

    incoming = Serial.readString();
    if (incoming == "352")
    {
      Serial.print("Arduino");
    }
    else if (incoming == "Led ON 1")
    {
      digitalWrite(led, HIGH);
    }
    else if (incoming == "Led OFF 1")
    {
      digitalWrite(led, LOW);
    }
    else if (incoming == "Led ON 2")
    {
      digitalWrite(led1, HIGH);
    }
    else if (incoming == "Led OFF 2")
    {
      digitalWrite(led1, LOW);
    }
  }
}

Please help me to solve this problem...

Thanks in Advance :slight_smile:

Your symptom is a common feature of the 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. Just use cstrings - char arrays terminated with 0.

And have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Thanks a lot Robin2 for replying... :slight_smile:

I modified my Arduino code but i get the same issue...

Please help me... :confused:

The modified version of code is...

int led = 13;
int led1 = 12;
char in;
void setup()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH);
  pinMode(led1, OUTPUT);
  digitalWrite(led1, HIGH);
}
void loop()

{
  if (Serial.available() > 0)
  {
    in = Serial.read();
    if (in == 'A')
    {
      Serial.print('C');
    }
    else if (in == 'F')
    {
      digitalWrite(led1, HIGH);
    }
    else if (in == 'N')
    {
      digitalWrite(led1, LOW);
    }
  }
}

Does your sketch work if you do not use the VB application and just use the serial monitor in the Arduino IDE?

post your VB code? is it a console or a Windows Form project?
I assume you are using the SerialPort component - which method are you using to read the serial data?
Take care if you are using a Form with a SerialPort event handler such as

   ' character received from serial port
    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        TextBox1.AppendText(SerialPort1.ReadExisting())
    End Sub

it is not thread safe in that the event handler is directly communicating with the TextBox GUI component
e.g. it crashes if you attempt to run with debug

Thanks everyone for replying to my post… :slight_smile:

I checked different combinations( I mean different Arduino boards and with different PC) and finally came to conclusion that everything is good from programming side.

I am close enough to solve this problem but just need your help…

I programmed everything in my laptop and when I check my arduino in my laptop, everything works good.

My Laptops Windows properties is below…

My Laptop Windows Properties

When I installed my program to my desktop I am getting that Issue (Arduino stops working after few communications)…

My Desktop Windows Properties is below…

My Desktop Windows Properties

I think this might be because of 32-bits and 64-bits

Someone please help me and guide me to solve this problem…

Thanks in Advance… :slight_smile:

did you use Visual Studio to rebuild your VB code on each of the PCs to suit the 32 or 64 bit environment?
the 32 bit code should run on 64bit windows OK - I tend to build 32bit code when porting around different windows systems
I doubt if 64 bit code would run on 32bit windows at all (never tried it though!)

does a terminal emulator such as teraterm pro run of the two systems display the same problems as VB?

Thanks Horace for replying... :slight_smile:

I recompiled VB6 code on both OS (As 64-bits compiled program was showing some runtime error on 32-bits)

I haven't tried any terminal emulator...

Would it be helpful if i reinstall 64-bits OS on my desktop? (As this is working good in my 64-bits OS in my Laptop)

I had missed that you are using VB6 - I would expect it to have problems on modern operating systems
You could instal 64 bit windows on the desktop if you have a spare licence
can you port the VB to Visual Studio 2017?
e.g. download Visual Express and try it

I don't have much knowledge about VB 2017.

I would prefer installing windows 64-bits.

Will It work then??

try installing 64bit windows if you have a spare licence or an upgrade
I would still recommend upgrading to an upto date version of VB - how complex is the code?
if you do a web search for convert vb6 to visual studio 2017 you will get plenty of links

Thanks Horace... :slight_smile:

I will try my best...

Thanks everyone for taking part in this discussion... :slight_smile:

I have uploaded a VS project containing a VB terminal emulator to Github
I may give you some idea of VB programming in Visual Studio 2017