0
\$\begingroup\$

I'm getting frustrated with my electronic knowledge, because I want to do this simple thing:

Olimex 328/ATMega328 reads data from UART (Olimex MOD-PULSE or whatever) and forwards it to the USB Port (my computer).

I don't understand how I can do this. I wrote following program:

void setup()         // run once, when the sketch starts
{
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop()                       // run over and over again
{
  Serial.println(Serial1.read());  // prints hello with ending line break 
}

But it doesn't work, since Serial1 does not exist. Could you give me a hint? I can't find anything that I can understand on the Internet.

\$\endgroup\$
3
\$\begingroup\$

Apparently the site didn't like my attempt to complete my sign-up. So further to your comment on my previous answer(as user46333):

On the Uno (the board I am using for development), it is possible to communicate with the board using the UART in two ways. There is the USB connection, and there are the Rx/Tx pins (2 and 3, respectively). It is possible to connect a device (ie RS232->TTL connection) to the Rx and Tx pins to communicate serially with the controller. When the USB is connected to the computer, it communicates on the same lines, indirectly, by going through the ATmega16U2. The two connect to the ATmega328 via the same pins, as demonstrated by the RED lines in the picture below (schematic from Arduino Uno documentation): enter image description here

So to answer your question

do they read/write on the same cable?

They read/write on the same pins.

Also, to correct my code, and simplify it:

void setup() {
    Serial.begin(9600);
}

void loop() {
    if (Serial.available() > 0) {
        Serial.println(Serial.readString);
    }
    else {Serial.println("HELLO");}
    delay(2000);
}

This will receive any data sent to the controller on the Rx pin, and then immediately print it to the Tx pin.

\$\endgroup\$
  • 1
    \$\begingroup\$ ah i see.. thanks for the great and extensive answer!!! \$\endgroup\$ – BvuRVKyUVlViVIc7 Jun 27 '14 at 20:18
1
\$\begingroup\$

ATMega328 has only one hardware serial port, i.e. using Serial1 makes no sense. How do you suppose you transfer it to USB CDC device? You need serial2USB bridge (prabably via second UART) or chip supporting USB (e.g. ATMEGA1286/7).

\$\endgroup\$
  • \$\begingroup\$ But I connect the Arduino via USB to my computer..? Is it not possible to receive data from UART and send it to the USB port? \$\endgroup\$ – BvuRVKyUVlViVIc7 Jun 27 '14 at 8:45
1
\$\begingroup\$

On the Arduino Uno (example that uses the ATmega328), data can be sent to and from the device via the UART. Example code:

void setup() {
    Serial.begin(9600);
}

void loop() {
    if (Serial.available() > 0) {
        String getString = Serial.readString();
        Serial.println(getString);
    }
    else {Serial.println("HELLO");}
    delay(2000);
}

Will look for incoming serial data, and print it back to the serial connection, otherwise printing simply hello. This code can be modified to accept different variable types, and still print them out to the serial connection.

Also, it is possible to use other I/O ports to simulate a UART connection, using compiled code libraries (one exists on Arduino's site), which should be more than adequate with a low baud rate.

\$\endgroup\$
  • \$\begingroup\$ So this receives from UART and sends it to the USB port? Its the same class you use..? Sorry, im totally newbie here.. \$\endgroup\$ – BvuRVKyUVlViVIc7 Jun 27 '14 at 8:46
  • \$\begingroup\$ Yes, if you are sending code and/or receiving serial communications from the Arduino via a USB port, that is exactly what it does. In the case of the Uno, the USB connection is the UART connection. \$\endgroup\$ – user46333 Jun 27 '14 at 10:55
  • \$\begingroup\$ but there are 2 contacts for cables? the UART connection and the mini-USB connection? Or do they read/write on the same cable? \$\endgroup\$ – BvuRVKyUVlViVIc7 Jun 27 '14 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.