Back

CreateUart

The CreateUart function creates an object which manages UART input and output for two pins on the IOIO board.

 uart = ioio.CreateUart( pinNumIn, pinNumOut, baud, parity, stopBits, mode );

Put the pin numbers you wish to use in the pinNumIn and pinNumOut parameters (Pins 3-7, 10-14, 27-32, 34-40 and 45-48 can be used with UART). Use null instead of a pin number if you don't need one of the pins.

The baud parameter should contain your chosen communication speed which can be 9600, 19200, 38400 or 115200. Set the parity parameter to "None", "Odd" or "Even" and stopBits to the number of stop bits you require (usually 1 or 2).

Options for the mode parameter are "OpenDrain" which affects the output pin, "PullUp" or "PullDown" which affects the input pin, Text which allows transmission of text characters or Hex which allows you to use comma seperated hex codes. Multiple modes are combined using a comma (eg. "PullUp,OpenDrain,Hex").

If you leave out the mode parameter, the output pin will be opened in normal mode where pulses will switch between 0 and 3.3V. If you require a 5V output you will need to add the "OpenDrain" option to the mode parameter and use a 10K resistor between the the 5V supply and the output pin (be careful as only the pins marked with circles are 5V tolerant).

A maximum of 4 UART's can be created at the same time.

See http://github.com/ytai/ioio/wiki/UART for more details.

Once the UART object is created, you can use the SetOnReceive function to set a callback function which will be called every time data is received in the input pin.

 uart.SetOnReceive( callbackFunc );

If you want the UART object to automatically split your input data into chunks and only call the callback when a full chunk has been received, you can use the SetSplitMode function. This is useful if the device you are communicating with sends messages of a fixed length or certain start/end codes.

 uart.SetSplitMode( "Size", 2 );
 uart.SetSplitMode( "End", 0xFC );
 uart.SetSplitMode( "End", "FF" );
 uart.SetSplitMode( "End", "." );
 uart.SetSplitMode( "Start-End", 0xfa, 0xfc );

You can optionally set a timeout value using the SetTimeout function which will cause the internal buffer used by the splitter to be automatically cleared when nothing is received for a given number of milliseconds.

 uart.SetTimeout( millisecs );

The following examples simply require pins 7 and 10 connected to each other.

Example - Send and recieve text with end code

function OnStart()
{
  ioio = app.CreateIOIO();
  ioio.SetOnConnect( ioio_OnConnect );
  ioio.CheckConnection();
}

function ioio_OnConnect()
{
  uart = ioio.CreateUart( 7, 10, 9600, "None", 1, "Text" );
  uart.SetOnReceive( uart_OnReceive );
  uart.SetSplitMode( "End", "." );
  uart.Write( "Hello." );
}

function uart_OnReceive( data )
{
  app.ShowPopup( data );
}
  Copy   Copy All    Run   

Example - Send and recieve data with end code

function OnStart()
{
  ioio = app.CreateIOIO();
  ioio.SetOnConnect( ioio_OnConnect );
  ioio.CheckConnection();
}

function ioio_OnConnect()
{
  uart = ioio.CreateUart( 7, 10, 9600, "None", 1 );
  uart.SetOnReceive( uart_OnReceive );
  uart.SetSplitMode( "End", 0xFF );
  uart.Write( [0xA1,0xA2,0xA3,0xFF] );
}

function uart_OnReceive( data )
{
  app.ShowPopup( data );
}
  Copy   Copy All    Run   

Example - Send and recieve data of fixed length

function OnStart()
{
  ioio = app.CreateIOIO();
  ioio.SetOnConnect( ioio_OnConnect );
  ioio.CheckConnection();
}

function ioio_OnConnect()
{
  uart = ioio.CreateUart( 7, 10, 9600, "None", 1 );
  uart.SetOnReceive( uart_OnReceive );
  uart.SetSplitMode( "Size", 3 );
  uart.Write( [0xA1,0xA2,0xA3] );
}

function uart_OnReceive( data )
{
  app.ShowPopup( data );
}
  Copy   Copy All    Run