Sending Serial communication from PC to Arduino via usb to rs232 to TTL

Hi Guys,

I need your help. I want to display a number using this program from my PC to arduino and to my u8g display. Currently, I am testing using AccessPort (from PC) to send data via usb to rs232 to TTL (arduino).

My code is pasted below. When I tried to send digit 1 to my arduino the receive signal is different it generates different numbers like 49, 10, 13 and keeps looping.

What could be the problem? I am using string and char but it is still the same.

I hope you could help me guys to display it correctly.

Thank you


#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 4); // RX, TX

String incomingByte = ""; // for incoming serial data

void setup() {
  mySerial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (mySerial.available() > 0) {
    // read the incoming byte:
    incomingByte = mySerial.read();
  }
    // say what you got:
    mySerial.print("I received: ");
    mySerial.println(incomingByte);
    delay(1000);
  }

There is a some misunderstanding. RS232 is not TTL. You don't need (I would say you MUST NOT) use RS232 to send data to arduino because it incompatible voltage levels.

1 Like

It is perfectly normal. It seems that you send it not as numbers, but as text with line ending after digit:

"1\r\n"

ASCII code of the '1' is 49, ascii codes of the "\r\n" are 10 and 13.

Hi,

Thank you for the clarification. So I was reading it correctly i tried to make it char and not as string and it worked but I still have some bugs in my program.


#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 4); // RX, TX

char incomingByte; // for incoming serial data

void setup() {
  mySerial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (mySerial.available() > 0) {
    // read the incoming byte:
    incomingByte = mySerial.read();
  }
    // say what you got:
    mySerial.print("I received: ");
    mySerial.println(incomingByte);
    delay(1000);
  }

What are the bugs?

Check your braces!

Let the IDE format your code: CTRL-T

using the AccessPort and writing the data it does not immediately get the data. For example, I sent "1". it will say "I received: 1". However, when I send "2", it does not change the "I received: 1".

That's correct behavior, you have to write the code to change the output.

This will be my last reply if you ignore my #6.

I will try CTRL-T and give you feedback. Thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.