PROBLEMS WITH USB KEEP DISCONNECTING.

Hi Pardon me if I am asking a very simple question or if my question had already been asked. I had tried to look everywhere but couldn't find a solution to my problem.

I am using Ubuntu System connected to Arduino mega 2650 which connects to a 16 channel relay module.
And I am controlling the arduino with Apache/Php

My arduino is connected to a 12v 40amp power supply and the usb is connected to my ubuntu PC.
I have 3 arduino mega which connects to my ubuntu pc and each board with corresponding relay set and mosfet.

My channel relay module is also connected to my arduino by the corresponding pin from 2 to 17, pin 20 to A15. It is also connected to the power supply in terms powering some locking device.
To prevent auto reset, I had connected a 10uf capacitor on my Reset to GND header.(I tried the resistor doesn't work)

I would send data thru Serial /dev/ttyACM* to the corresponding port.

The problem is when I try to turn on and off the relay, after a couple of times. Sometime maybe 5-10 times and sometime barely 2-3 times, the arduino would disconnect.

I had used php_serial_class or just the usual

exec("/bin/stty -F /dev/ttyACM$p 9600 cs8 hupcl -clocal -crtscts");
$fp = fopen("/dev/ttyACM".$p,"r+");
stream_set_timeout($fp, 30);
stream_set_blocking($fp, false);
fwrite($fp, $data);
$reply = fgets($fp);
fclose($fp);

But problem is persistant

As I check I realised the following

  1. After disconnect, the serial port would change port. Example it was ACM0, it would jump to ACM3 or AMC2 Randomly.
  2. When I try dmesg | grep tty It display alot of ports just added. I wanted to kill the serial port by finding the process running. But all processes are the usual and nothing special.
  3. Everything works perfectly on the arduino if the relay is not connected or turned on. The arduino will just keep running flawlessly, no disconnect etc. But when connected to relay and turned on, it will get disconnected after a while.
  4. I tried using minicom to send data to my arduino. It works but the same issue arises it will get disconnected after a while. But at least minicom doesn't have problem 1 and 2.

Any help would be grateful.

serial.png

Hi Sorry my problem is solved. I realised the problem is witht he sleep(1);

my old code
exec("/bin/stty -F /dev/ttyACM$p 9600 cs8 hupcl -clocal -crtscts");
$fp = fopen("/dev/ttyACM".$p,"r+");
stream_set_timeout($fp, 30);
stream_set_blocking($fp, false);
sleep(1);
fwrite($fp, $data);
sleep(1);
$reply = fgets($fp);
fclose($fp);

after I removed the delay of 1 second the problem is solved.

I guess arduino close and change port if there is a delay in closing the port.

exec("/bin/stty -F /dev/ttyACM$p 9600 cs8 hupcl -clocal -crtscts");
$fp = fopen("/dev/ttyACM".$p,"r+");
stream_set_timeout($fp, 30);
stream_set_blocking($fp, true);
fwrite($fp, $data);
$reply = fgets($fp);
fclose($fp);