I would like to read what is arriving on the serial1 port of my Arduino Micro but I get some strange things.
See attachment for my hardware setup.
When I send "a" to the Serial1 port, then Serial port monitor outputs "79"
When I send "b" to the Serial1 port, then Serial port monitor outputs "39"
When I send "b" to the Serial1 port, then Serial port monitor outputs "78"
and so on. This is my code:
String incomingByte; // for incoming serial data
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
Serial1.begin (9600);
}
void loop() {
// send data only when you receive data:
if (Serial1.available() > 0) {
// read the incoming byte:
incomingByte = Serial1.read();
// say what you got: //Serial.print("I received: ");
Serial.print(incomingByte);
}
}
Can anyone explain why I don't receive the same text as I sent?
Serial.read() does not return a String. In fact, you should avoid using Strings, because they cause memory problems and program crashes on AVR based Arduinos.
Change this line:
String incomingByte; // for incoming serial data
to
int incomingByte; // for incoming serial data
If you want to see the ASCII character that is read, rather than its integer value, print it with:
Thank you for your reply, I will keep this in mind.
However, I still do not get the same character than I have sent:
When I send "a" then my serial monitor outputs "0"
When I send "b" then my serial monitor outputs " ' "
When I send "c" then my serial monitor outputs "N"
My code is now:
int incomingByte; // for incoming serial data
void setup() {
Serial.begin (9600);
Serial1.begin (9600);
}
void loop() {
if (Serial1.available() > 0) {
// read the incoming byte:
incomingByte = Serial1.read();
Try this: start the Arduino IDE twice. What I mean by this is to actually start the application twice, rather than having two windows of the same IDE instance open, as happens when you do File > New. This will allow you to set a different port in the Tools > Port menu of each instance of the Arduino IDE and then open two Serial Monitor windows, one for Serial and the other for Serial1.
Does the same problem occur as when you were using Hyperterminal for Serial1?
pert:
What is the box connected between your computer and Serial1 in your diagram?
It is an USB to serial converter. Another detail is that I have drawn 2 laptops on the drawing but in fact it is one and the same computer, but I split it up on the drawing to make it more clear. I go from USB port 1 through a usb-serial converter (pins 3 and 5) to the Arduino's serial1 port (pins 4 and 6 as marked on the schematic). Then the other connection is through the microUSB connector of the arduino to USB port 2 of my laptop
My intention is to read out serial telegrams from a RS232 device, but as I currently don't have this device, I use hyperterminal to send the telegrams to the arduino.
Is this a USB to RS-232 converter? If so, you are subjecting your Arduino to damaging voltage levels. You need to use a USB to TTL serial adapter (like the FTDI 232RL or CH340) or an RS-232 to TTL converter (like the MAX232). The TTL logic levels are compatible with the safe range of 0-5 V you can subject the Arduino Micro's IO pins to.
pert:
Is this a USB to RS-232 converter? If so, you are subjecting your Arduino to damaging voltage levels. You need to use a USB to TTL serial adapter (like the FTDI 232RL or CH340) or an RS-232 to TTL converter (like the MAX232). The TTL logic levels are compatible with the safe range of 0-5 V you can subject the Arduino Micro's IO pins to.
Hmm you're right... I disconnected the arduino from the USB-RS232 converter and measured 8V, so that must be the problem... Is there another quick solution to emulate data on your serial1 port? A software solution? Or should I just use Serial instead of Serial1 while programming?
You're going to need a RS-232 to TTL converter eventually when you get your RS-232 device so I recommend ordering one of those now.
Until then, certainly you can just use Serial and Serial Monitor for your development work. In order to make it easy to make the switch from Serial to Serial1 once you get your device, you can define a HardwareSerial object for that serial interface:
HardwareSerial &RS232Device = Serial;
int incomingByte; // for incoming serial data
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
RS232Device.begin (9600);
}
void loop() {
// send data only when you receive data:
if (RS232Device.available() > 0) {
// read the incoming byte:
incomingByte = RS232Device.read();
// say what you got:
//Serial.print("I received: ");
Serial.print((char) incomingByte);
}
}
Then to switch to Serial1 you only need to do this:
HardwareSerial &RS232Device = Serial1;
Keep in mind that there is a chance you damaged your Arduino Micro by subjecting it to voltage levels far beyond the absolute maximum rating. It's no fun to spend forever trying to debug your software when the problem is caused by bad hardware.
ON3WVS:
When I send "a" then my serial monitor outputs "0"
When I send "b" then my serial monitor outputs " ' "
When I send "c" then my serial monitor outputs "N"
In my following setup of MEGA --
I send a b c from Serial Monitor, I receive back a b c via this loop: (SM --->RX0 --->TX1--->RX2--->TX0--->SM)
Setup:
TX1 ----> RX2 //short by jumper
RX1 <--- TX2 //short by jumper