For example, I have found that using a watch dog in the code does not work. Additionally, pressing the reset button on the Arduino when it fails also has no response.
Finally, using Serial.flush() after each transmission also has no effect.
Currently, when the Serial monitor stops working (the TX light on the Arduino switches off and the Serial Monitor no longer captures data), I have to unplug the USB, plug it back into a different USB port on the laptop, and then upload the code back to the Arduino. Is there any way to do this programmatically?
You want to write a program to physically unplug the Arduino?
Add code to the Arduino sketch to blink an LED, using the blink without delay technique. Does the Arduino stop working? Or, is it the PC that goes flaky?
jeremyg:
However, the laptop connected to the Arduino is, I think, experiencing power surges from other machines in the laboratory. I'm kind of stuck with a particular spot in the lab, so it's not quite as simple as moving to another power point.
The nice thing about laptops is that they have batteries and can work when disconnected from the mains power point.
Indeed, laptops have batteries. However, this is often collecting data for weeks at a time and measuring drift. So running it off the laptop battery isn't feasible in this instance.
I was able to replicate the issue this morning (finally!).
The laptop charger and a Nikon transformer are both plugged into the wall sockets, but at different power points (spaced about 2 m apart).When the Nikon transformer is switched off, the serial connection is disrupted. This drops the connection and the Arduino hangs. This does not disrupt the rest of the code from operating (blink without delay continues to operate), it just disrupts the serial connection (no further data is received).
Has anyone had any experience with this? Previously, Robin has said:
Robin2:
Then, if the application is important, run a clean mains power supply for the laptops to connect to.
...R
and my initial thought it to purchase an uninterrupted power supply. This is pretty expensive and outside my budget. Are there any other options for cleaning a mains power supply?
Alternatively, can this be completed with software? Currently, pressing the red reset button on the Arduino does not reinitialize the serial connection (blink without delay continues to operate, though!). Currently, I have to disconnect the Arduino USB cable, reconnect it to a different USB port, and re-upload the code to the Arduino.
I was thinking of unmounting/re-mounting the USB connection if no signal is received for, say, 30 seconds. However, I've never attempted this before. Can anyone offer some guidance or point to an example or tutorial?
I don't think you have said what sort of Arduino you are using.
And it is not clear from your description whether the Arduino resets? Maybe you could get it to flash an LED in an identifiable sequence in setup() so that you easily tell if it resets even when there is no communication with the PC.
Does the PC program that is communicating with the PC recognize that the communication has stopped? If so maybe it can restart it.
What operating system are you using on the PC? If you are using Windows maybe the COM port changes?
Would it help to power the PC and the transformer from the same (or adjacent) outlets?
Robin2:
And it is not clear from your description whether the Arduino resets? Maybe you could get it to flash an LED in an identifiable sequence in setup() so that you easily tell if it resets even when there is no communication with the PC.
I implemented a unique sequence of flashes with the LED in setup and can confirm that the watchdog timer does not get triggered. The code appears to be looping successfully, but the Arduino is not sending information via serial (the TX light stops flashing and turns off). The setup sequence is only triggered when I manually press the red reset button on the Arduino, however, this does not restore serial communication. When I attempt to disconnect and reconnect the serial monitor, I get an error message: "Error while setting serial port parameters: 9,600 N 8 1".
Robin2:
Does the PC program that is communicating with the PC recognize that the communication has stopped? If so maybe it can restart it.
I could get the Python code to recognise that a signal is no longer being received, but I'm not sure where to go from there. Currently, I have to physically disconnect and reconnect the USB cable to a different USB port before I can re-upload the sketch. If I simply re-upload the sketch (without disconnecting/reconnecting, or even disconnecting and reconnecting but to the same USB port), I get an error: "avrdude: ser_open(): can't open device "\.\COM9": Access is denied."
Robin2:
What operating system are you using on the PC? If you are using Windows maybe the COM port changes?
I am using Windows XP SP3. In Windows' Device Manager, the Arduino Uno remains connected even after it hangs (and does not change port). It just stops sending messages to the Serial Monitor.
Robin2:
Would it help to power the PC and the transformer from the same (or adjacent) outlets?
Previously, they were connected to adjacent outlets (about 2 m apart). However, when connected to the same outlet, the Arduino no longer hangs. Crazy! Any ideas about why this is happening? I'm also worried about other devices in the lab which may drive power surges through the room and therefore, a software solution is still desirable.
jeremyg:
but the Arduino is not sending information via serial (the TX light stops flashing and turns off).
Is the Arduino program expecting data from the PC?
I am no longer familiar with Windows - I have been using Linux for years.
You mention the watchdog timer and the makes me think you have a complex Arduino program. If so write a very simple Arduino program without any watchdog and with the minimum of code to test the connection.