Arduino Sends Old Data Through Serial - arduino

I think my problem would best be explained with an example. Here is some Arduino Code:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(millis());
delay(1);
}
...and here is the output:
Now, those numbers at the beginning aren't just your regular old first-bytes-of-serial-data-are-always-messed-up numbers. Those numbers are left over from the last time I opened the serial monitor. How do I know this? Because I counted how many seconds had gone by since I last opened the serial monitor, and it all matched up.
So here's the big questioroonie,
How do I make this stop? It's breaking my java program that's supposed to read data from the Arduino's current "run instance"(I don't know the correct term), and not the last one.

There is nothing wrong with your Arduino. Remember, the USB connection is not a real RS-232 serial interface, it's simulated through USB. And that USB connection is controlled by a chip with its own buffer.
Bad news is, nothing you can do if you're using the USB cable to carry your RS-232 signals, other than to send some "filler characters" to purge the buffer. Too many buffers everywhere :) Send some terminal emulation commands like "clear the screen" and "form feed" etc. to empty the pipeline of stuff in the buffer.
Which reminds me -- we should all quit using the crummy Arduino serial monitor and be vocal about it. Instead, use TeraTerm or Putty or any other good-quality terminal emulator of your choice.
Or connect with real RS232 and an adapter and cable. My development machine has three serial ports, but that's what I do. Often computers today don't even have one handy.

Related

HC-05 Bluetooth module help, can not recieive data

I am writing some code for my arduino nano and I am using an HC-05 module for wireless transfer of data. I am sending my data as such. This is from the serial monitor, using a usb and regular print statements. I want to be able to recieve this data, and the store it in a csv (I know how to store incoming data). I need help recieveing my data.
22:17:46.765 -> =============================================================
22:17:47.770 -> Sleep timer:242
22:17:47.770 -> Light Sleep: 0 Deep Sleep: 0
22:17:47.805 -> Total Light Sleep: 0 Total Deep Sleep: 0
22:17:47.838 -> Total Sleep: 0
22:17:47.871 -> =============================================================
For some reason, when I connect my HC-05 to my windows desktop via bluetooth, and open a serial monitor in com ports 3 or 4, I get no read out.
My ports
I have it set up as so.
My wireless intialization
Then, I use
MySerial.print()
, to send data, yet I get no output, same thing occurs if I use
Serial.print()
.
I tried getting a new HC-05 module, Switched out all the hardware, tried different prints including Serial and MySerial. I though i could get an output.
Python code I tried to use python to extract the data, yet I get no output. I am not sure what to do here.
Not enough information, but I'll answer biased on the experience I've had with these modules.
Here are instructions for changing or verifying the baud rate on the module:
https://www.instructables.com/Change-the-Baud-Rate-of-HC-05-Bluetooth-Module-Usi/
For my module, I could just send AT commands by default without putting it into AT mode. Send 'AT' to the module and it should respond with "OK".
It helps a ton if you have an ftdi cable for troubleshooting. Connect the ftdi to the HC-05, verify the baud rate and that the HC-05 is responding, open a serial monitor for the bluetooth on the pc and one for the ftdi cable. Try sending data back and forth. Also, be 100% sure you're using the correct com port for your bluetooth by unpairing and repairing and see which com port shows up.
It actually is possible to use the HC-05 for programming the Arduino as well if you set the baud rate to 115200 and figure out a way to hit reset on the Arduino as soon as the bluetooth connects(status/state goes solid). I used an ATTiny for that, but you could just use a pin on the nano connected to reset and another pin to monitor the "state" pin which is connected to the led on the HC-05. That way you can have wireless data and wireless programming if you wanted that.

Reading serial data from Arduino fails

I have written a program for Arduino that reads some analog signals and sends them to the computer when it receives a command from the master computer. I wondered why this didn't work on the computer it was intended to run on. On my own computer it runs fine.
I uploaded a simple test code in the Arduino.
void setup() {
Serial.begin(9600);
}
void loop() {
if(Serial.available()) {
Serial.println(Serial.read());
}
}
This doesn't run on the second computer either. When I use Arduino serial monitor for transfering data, I see the RX led blink but not the TX. With the computer it's working on, I can see both of the leds flash. Arduino receives the data on both computers, but the second computer doesn't receive Arduino's responses. What might be wrong?
Edit. I forgot the Arduino hooked up to the problematic PC for a few minutes and tried it again. Then it worked! It seems like it needed some time to warm up. Why's that?
Sometimes it can take a second for the Arduino and computer to establish the Serial handshake, especially at 9600 baud. I'm glad you got it working!

Arduino: Incoming USB serial data when USB is disabled hoses the connection

I'm writing a sketch for an Arduino Leonardo that occasionally needs to communicate bidirectionally via USB serial, and also needs to be power efficient. To save power I'd like to disable the USB when it's not needed. I can successfully use power_usb_disable() and power_usb_enable() to do this, BUT only if no data is sent to the Arduino when USB is disabled.
Here's how to reproduce the problem: I run the following sketch on the Leonardo while it's connected to a serial comms program on my computer (I've tried the Arduino serial monitor and Minicom in Linux and PuTTY in Windows). It works as expected, printing out the messages, until I type something right after the "Disabling USB..." message. Once I do that, I will receive no further data from the Leo. The funny thing is, the TX and RX LEDs continue to blink as if everything is fine. The only way to get the connection working again is to reset the board.
Now, I don't really care about dropping the incoming data when USB is disabled, that's to be expected...but I would like the serial connection to continue to work correctly once USB is enabled again.
I don't know enough about how USB serial works to know how to troubleshoot this.
#include <avr/power.h>
void setup() {
Serial.begin(9600);
delay(3000);
}
void loop() {
Serial.println("USB is enabled!");
delay(1000);
// If I send characters here, everything is fine.
Serial.println("Disabling USB...");
delay(25);
power_usb_disable();
delay(1000);
// If I send characters here, it hoses the USB connection somehow.
power_usb_enable();
delay(25);
}
I guess for now the best answer to this problem is "don't do that." I can arrange not to disable the USB if the connection is open, using the "if (Serial)" thing. So if somebody has a serial monitor open and looks as if they're about to type something, I leave USB enabled. Seems to work OK, although it doesn't address the underlying general question of how to robustly disable and re-enable USB from any state it might be in. I would still love to hear expert advice on that.

Arduino Uno rejects uploads, changes ports

After experimenting with some Serial communication (which may or may not be relevant), the Arduino IDE throws a "Error opening serial port" error when I try to upload new code to the Arduino or use Serial Monitor. This has happened in both Windows and Linux.
In Linux, after a failed upload I tried disconnecting, then reconnecting the board. It was reassigned to the next open serial port. For example, it starts out as /dev/ttyACM0, then moves to /dev/ttyACM1, and so on.
When plugged in, the "TX" LED lights up, then goes dark. When I try to upload to it, the "TX" LED flickers twice, then nothing.
Any ideas?
Open the file ~/.arduino/preferences.txt file(linux). There you will find a string serial.debug_rate. Check its value. Typically its value is 9600(it denotes the baud rate of the serial communication). So if this value is something else, that might be causing the problem.
I have no definite answer for what might cause the value to change. But in my case changing it back to 9600 worked fine.
Having something physically plugged into the serial port on the arduino will keep sketches from uploading.

How to send/receive serial communication

How can I send a string via serial communications from Java or Python? I have a very little arduino program that will recieve a CSV string of ints that each control different relays (I adapted it from their example).
I just got an Arduino, and I am trying to use it to open and close a relay remotely. I have it opening and closing the relay, no problem. But I really have no idea where to start as far as using serial communication.
I have a bluetooth dongle that I can connect to, but once I do that I'm lost.
Also, this is my first time using serial communications AND my first time using an Arduino. So I'm sorry if I am completely off on some of the things I am asking. All I know is what I've learned from Google.
Thank you very much
You can use any Java or Python serial port classes and methods. There is however one thing you need to be aware of.
The newer arduino's have a feature known as AutoReset. This works by connecting the DTR signal from the serial port to the Reset line on the chip using a capacitor or such. The IDE uses this to reset the board to activate the bootloader and initiate uploading code to the arduino. The serial monitor in the IDE is aware of this and does not toggle the DTR line when comunicating with the device (except when it initially connects).
There is are several ways to defeat this feature and you can read about them here.
As for actually comunicating with the device, there are several tutorials about serial communication in both Java and Python.
Pyserial includes the option to not use the DTR lines when opening a port.
Here is a website explaining how to use RXTX (a Java library) to comunicate with an Arduino
Best of luck.

Resources