Serial / UART Communication (2-way)

1,842 views
Skip to first unread message

xk

unread,
Jun 17, 2011, 12:55:35 PM6/17/11
to ioio-users
I have a question with the serial 2 way communication because it still
isn't very clear to me.
I have read the wiki, (http://codaset.com/ytai/ioio/wiki/UART) and
these 2 statements seem to contradict each other:

The first one says each UART can either be a TX or RX. So I would each
output/input independently to a different pin on the DB9 serial
connector.
"In order to establish two-way communications, two UART channels are
used, which are not synchronized in any way. Just two independent one-
way streams. Each end then has a TX (transmit) and an RX (receive)
channel."

A few lines down it says I can use 1 UART channel for both transmit
AND recieve (both).
"IOIO has 4 UART modules, each having a transmit channel and a receive
channel. You can use either channel or both. "

So which one is right? Should I connect 1 UART channel to RX and only
recieve, and another UART channel for only transmit?

I am trying to communicate first to a PC and then to a M16c
microcontroller.
Will the PC logic voltage level or the m16c damage the IOIO board? I
think both a PC and the M16c are at CMOS 5v level and not TTL.


Thanks alot!

Greg

unread,
Jun 17, 2011, 6:00:22 PM6/17/11
to ioio-...@googlegroups.com
I've been programming PICs (the chip that is on the IOIO) for years, so here's what I can tell you...
I'm including some basics that you may already know, so don't feel insulted, I'm just trying to help anyone else out there that may have questions.
UART stands for Universal Asynchronous (meaning there is no clock line) Receiver/Transmitter.
A UART is a module within the chip on the IOIO.  It has 1 pin that it uses to transmit, and 1 pin that it uses to receive.  There are 4 modules (channels) in the chip that was put on the IOIO board.  This means in theory, you could connect to 4 PCs at the same time, each on a separate UART, and all could talk to the IOIO at the same time.
If you so chose, you can use just the transmit (TX) pin of any UART module, or just the receive (RX) pin of any UART module, or as is usually the case, you use both.  The levels voltage levels coming out of the PIC is a 0-3V signal, but a PC uses -15V to +15V (or at least can use those voltages) which would destroy the PIC on the IOIO board.  There are chips made specifically for converting between these voltages like the MAX232 etc.
That being said, I know that some PCs will accept a 0-5V signal, so maybe some will accept the signal directly, but the voltage from the PC into the IOIO would still be a problem.  Some of the pins on PICs can tolerate up to 5V, but I don't think PCs commonly output a voltage that low.  You're best off to use a MAX232 or something like it to be on the safe side.
The other microcontroller you are using shouldn't need such a converter chip if they are running at the same voltage, which means you will probably want to use 1 UART channel for talking back and forth with the PC - this channel will use a level converter chip, and one channel for talking back and forth to your other micro - this will be directly connected.
I hope this helps.

Ytai

unread,
Jun 17, 2011, 6:49:43 PM6/17/11
to ioio-...@googlegroups.com
If you don't have a strong reason to use the serial ports on your PC, I really recommend a USB-UART adapter instead.
I'm using this one:
You can search ebay for "usb ttl uart" and you'll find even cheaper ones.

For $4-5 incl. shipping, it is almost a no-brainer :)
It exposes itself as a serial port on the PC, and you can then use a terminal application to send/receive data. This specific module can also provide 5V to power your IOIO when connected to the PC.

xk

unread,
Jun 20, 2011, 10:21:31 AM6/20/11
to ioio-users
Thanks for the explanation Greg!

So in theory I can use the 5v pins on the IOIO board to accept and tx
UART? Then I don't need a level shifter.
I'm pretty sure the M16c uC is a 5v output, I put a scope on the TX
pin with ground on the connector metal while sending every 1s but I
couldn't see anything..
Is there a good way to measure this?

@Ytai, the PC to IOIO was just for debugging, I want to use the m16c
in the end so USB<->UART wouln't have much use for me.






On Jun 17, 6:49 pm, Ytai <yta...@gmail.com> wrote:
> If you don't have a strong reason to use the serial ports on your PC, I
> really recommend a USB-UART adapter instead.
> I'm using this one:http://cgi.ebay.com/USB-2-0-UART-TTL-6-Pin-Module-Converter-CP2102-/2...

Ytai Ben-Tsvi

unread,
Jun 20, 2011, 11:00:40 AM6/20/11
to ioio-...@googlegroups.com
On Mon, Jun 20, 2011 at 5:21 PM, xk <ray...@gmail.com> wrote:
Thanks for the explanation Greg!

So in theory I can use the 5v pins on the IOIO board to accept and tx
UART? Then I don't need a level shifter.
I'm pretty sure the M16c uC is a 5v output, I put a scope on the TX
pin with ground on the connector metal while sending every 1s but I
couldn't see anything..
Is there a good way to measure this?
As long as you're sure it is not GREATER than 5V, you can just use a 5V tolerant pin and not worry about it.
For IOIO TX <-> M16c RX, if 3.3V is not enough (not likely) you can  a 5V-tolerant pin in open-drain mode and a pull-up to 5V. That would give you 5V output.

@Ytai, the PC to IOIO was just for debugging, I want to use the m16c
in the end so USB<->UART wouln't have much use for me.
Sure. Just thought that for $5 you might want it hanging around for the next time you'll need it :) 






On Jun 17, 6:49 pm, Ytai <yta...@gmail.com> wrote:
> If you don't have a strong reason to use the serial ports on your PC, I
> really recommend a USB-UART adapter instead.
> I'm using this one:http://cgi.ebay.com/USB-2-0-UART-TTL-6-Pin-Module-Converter-CP2102-/2...
> You can search ebay for "usb ttl uart" and you'll find even cheaper ones.
>
> For $4-5 incl. shipping, it is almost a no-brainer :)
> It exposes itself as a serial port on the PC, and you can then use a
> terminal application to send/receive data. This specific module can also
> provide 5V to power your IOIO when connected to the PC.

--
You received this message because you are subscribed to the Google Groups "ioio-users" group.
To post to this group, send email to ioio-...@googlegroups.com.
To unsubscribe from this group, send email to ioio-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ioio-users?hl=en.


fireraisr

unread,
Jun 24, 2011, 4:36:55 PM6/24/11
to ioio-users
Hello All,
I'm writing the UART interface for the IOIO and a seperate pic board.
My question is:
Is the IOIO a host or slave connection on the UART. Will I need a null
modem to communicate?
Thanks,
Mark

On Jun 20, 8:00 am, Ytai Ben-Tsvi <yta...@gmail.com> wrote:

Ytai Ben-Tsvi

unread,
Jun 24, 2011, 4:48:36 PM6/24/11
to ioio-...@googlegroups.com
AFAIK UART is a symmetric protocol - no host or slave. You should have no problem interfacing with a separate board - just connect TX<->RX and RX<->TX and send bytes back and forth.

fireraisr

unread,
Jun 24, 2011, 5:05:09 PM6/24/11
to ioio-users
That's what I thought but because that board is not ready yet I wanted
to be sure.
Do I need to close my stream after each read/write? I haven't
implemented that yet thinking it will work. I'm testing a loopback
today.
Just wondering if there are any pitfalls I should look out for.
Thanks again,
Mark

Ytai Ben-Tsvi

unread,
Jun 24, 2011, 5:20:19 PM6/24/11
to ioio-...@googlegroups.com
No, you shouldn't have to close the stream.
Not aware of any pitfalls. It's all very straightforward: open the UART with the exact same configuration (i.e. baud rate, stop bits, parity) on both ends. Send/receive data.

Eric42

unread,
Jun 25, 2011, 2:11:56 PM6/25/11
to ioio-users
Hi,

On 18 juin, 00:49, Ytai <yta...@gmail.com> wrote:
> If you don't have a strong reason to use the serial ports on your PC, I
> really recommend a USB-UART adapter instead.
> I'm using this one:http://cgi.ebay.com/USB-2-0-UART-TTL-6-Pin-Module-Converter-CP2102-/2...

Ytai, I just received a couple of them. I have a few questions
regarding their usage with the IOIO:
- You said we can power the IOIO from this device. Do you recommend
using the VIN or the 5V pin of the IOIO for connecting the 5V output
of the USB2UART? (I measure 5.14V on mine)
- Will it deliver enough power to charge the phone (or at least
preventing it to discharge)?
- The TX output of the USB2UART has a +/-3.5V level. Can I safely
connect it to any 3.3V IOIO input or should I always use a 5V tolerant
input?
- The USB2UART has a RST pin that I think stands for "RESET". What are
we supposed to do with it?

Thanks in advance,
Eric

Ytai Ben-Tsvi

unread,
Jun 25, 2011, 4:23:53 PM6/25/11
to ioio-...@googlegroups.com
- You said we can power the IOIO from this device. Do you recommend
using the VIN or the 5V pin of the IOIO for connecting the 5V output
of the USB2UART? (I measure 5.14V on mine)
5V, although either one is OK. 
- Will it deliver enough power to charge the phone (or at least
preventing it to discharge)?
Should be OK. The phone can take up to 500mA and the host has to provide at least 500mA. Since you have more load than just the phone, in theory you can exceed the rating. In practice, I do it all the time and it's OK. All hosts that I know of are protected against over-current, so should not be damaged either way.
- The TX output of the USB2UART has a +/-3.5V level. Can I safely
connect it to any 3.3V IOIO input or should I always use a 5V tolerant
input?
Should be OK on any pin. 
- The USB2UART has a RST pin that I think stands for "RESET". What are
we supposed to do with it?
I'm not sure what it's for. I've always left it floating.

xk

unread,
Jul 14, 2011, 5:53:34 PM7/14/11
to ioio-users
Hello Ytai,

I ordered this RS232 to TTL converter as seen here:
http://www.serialcomm.com/serial_rs232_converters/rs232_rs485_to_ttl_converters/rs232_to_3v_ttl_converter/rs232_to_3v_ttl.product_general_info.aspx
This one is powered by the computer's rs232 port and I tested it using
the loop back of RX to TX for the TTL side, it seemed to work.

Now I want to test the IOIO board by transmitting from the PC and
recieving on my Droid X. So I modified one of your example and played
around with it for a few days but I can't figure out why the
connection won't properly work. If you have a better example for UART
usage, I would really appreciate that. I had to dig up some posts in
this google group and find some usage with a GPS sensor.

I checked the rx, tx, and groud pins and they're all ok. I think the
issue is more on the software UART initialization.

Here is my code, please provide some feedback.. Thanks in advance :

public class MainActivity extends AbstractIOIOActivity {
private ToggleButton button_;
private TextView myTextView;
private static int counter = 0;


/**
* Called when the activity is first created. Here we normally
initialize
* our GUI.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button_ = (ToggleButton) findViewById(R.id.button);

myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setSingleLine(false);



}


/**
* This is the thread on which all the IOIO activity happens. It will
be run
* every time the application is resumed and aborted when it is
paused. The
* method setup() will be called right after a connection with the
IOIO has
* been established (which might happen several times!). Then, loop()
will
* be called repetitively until the IOIO gets disconnected.
*/
class IOIOThread extends AbstractIOIOActivity.IOIOThread {
/** The on-board LED. */
private DigitalOutput led_;

private Uart uart_;
private InputStream in;

private int bufferSize = 100;


/**
* Called every time a connection with IOIO has been established.
* Typically used to open pins.
*
* @throws ConnectionLostException
* When IOIO connection is lost.
*
* @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
*/



@Override
protected void setup() throws ConnectionLostException {
led_ = ioio_.openDigitalOutput(0, true);

myTextView.setText("Connecting.. \n");
uart_ = ioio_.openUart(47, 45, 115200, Uart.Parity.NONE,
Uart.StopBits.ONE);

in = uart_.getInputStream();




}

/**
* Called repetitively while the IOIO is connected.
*
* @throws ConnectionLostException
* When IOIO connection is lost.
*
* @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
*/
@Override
protected void loop() throws ConnectionLostException {
led_.write(!button_.isChecked());

runOnUiThread(new Runnable() {
@Override
public void run() {
myTextView.setText("hello" +
Integer.toString(counter));
}
});


counter++;


try {
int availableBytes =in.available();

myTextView.setText("available bytes " +
Integer.toString(availableBytes));

if (availableBytes > 0) {
byte[] readBuffer = new byte[bufferSize];
in.read(readBuffer, 0, availableBytes);
char[] Temp = (new String(readBuffer, 0,
availableBytes)).toCharArray();
String Temp2 = new String(Temp);

myTextView.setText("Received: " + Temp2);
}

sleep(10);

}
catch (InterruptedException e) {
log("Error: " +e );
}
catch (IOException e) {
// TODO Auto-generated catch block
//
log("Error: " +e );
e.printStackTrace();
}




}

private void log(String logString) {
myTextView.setText(myTextView.getText() + logString + "\n");
}


}

/**
* A method to create our IOIO thread.
*
* @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
*/
@Override
protected AbstractIOIOActivity.IOIOThread createIOIOThread() {
return new IOIOThread();
}
}


Ytai Ben-Tsvi

unread,
Jul 14, 2011, 6:02:59 PM7/14/11
to ioio-...@googlegroups.com

One quick problem I saw was calling setText from IOIO the thread (setup). What are you seeing? Have you checked logcat for exceptions or other hints?

Henrique

unread,
Jul 14, 2011, 8:00:13 PM7/14/11
to ioio-users
Hi xk,

Check it out this topic:
http://groups.google.com/group/ioio-users/browse_thread/thread/60f91ad78e3c834f

I have just tested using one Terminal software, a MAX232 IC and
Android.

Cheers!

Henrique



On 14 jul, 18:53, xk <raye...@gmail.com> wrote:
> Hello Ytai,
>
> I ordered this RS232 to TTL converter as seen here:http://www.serialcomm.com/serial_rs232_converters/rs232_rs485_to_ttl_...

Krishna

unread,
Aug 2, 2011, 8:25:13 PM8/2/11
to ioio-...@googlegroups.com
Just looking for a clarification on the UART documentation on the Wiki

"IOIO has 4 UART modules, each having a transmit channel and a receive channel."

However on the IOIO board Legend (Getting to Know your IOIO)
  • PPSi: pin can be used as input for remappable peripehreals (input capture, UART, SPI)
  • PPSo: pin can be used as output for remappable peripehreals (UART, SPI, comparator output)
There are around 28 pins are supporting UART either as PPSi or PPSo or both.

Is there any significance to the 4 UART modules and the pin combination or is it has no bearing?

Does the 4 UART module signify that only 4 active UART concurrent connections are possible ?

Not familiar with the PIC hence asking a basic question.


Ytai Ben-Tsvi

unread,
Aug 3, 2011, 3:27:02 AM8/3/11
to ioio-...@googlegroups.com
4 UARTs means that you can use up to 4 UART (RX+TX) modules concurrently.
Each one of them can use any of the pins that support PPSo as TX and any of the pins that support PPSi as RX.
The underlying reason for that is that the PIC that's used has 4 hardware UART modules, which is considered a lot.

--
You received this message because you are subscribed to the Google Groups "ioio-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/ioio-users/-/6MWOrxk2OuQJ.
Reply all
Reply to author
Forward
0 new messages