IOIO UART Problem

eric010101

Member
Licensed User
Longtime User
Hi,

I am new in B4A. I need to send command and read ID from a RFID reader that is based on TTL-level serial communications.I use a IOIO board from sparkfun, and imported lib from this forum. so far, I can get the LED on/off and can sent command to RFID, but I don't know how to read data back from UART.
below are some lines from my project.
'define pin and open uart port.
UART1= YOYO.OpenUart(4, uart1.IP_FLOATING,5,uart1.OP_NORMAL,115200,uart1.PARITY_NONE,uart1.STOPBIT_ONE)

'using textreader to read buffer
TextReader1.Initialize(UART1.InputStream)
TextWriter1.Initialize(UART1OutputStream)
'using a timer to check if data arrived in 20msec
If TextReader1.Ready Then 'check if there is any data waiting to be read
Label1.Text = TextReader1.ReadLine & CRLF
End If

when the RFID send back data to IOIO and the program hanged for a about 30 second, and an error message pump up to force terminate the program.

I googled the entired web and searched this forum for about 2 full days, and has no luck, can someone teach me how to deal with UART with IOIO in B4A. or can someone tell me if is it possible to read data back from IOIO with B4A
Sample code and drawing will be very much appeciated.

THANK You!
 

eric010101

Member
Licensed User
Longtime User
TextReader.ReadLine reads all characters until it reaches an end of line character. Your device probably never sends such a character.
Are you working with characters at all?

I recommend you to use AsyncStreams instead: Basic4android Search: AsyncStreams Tutorial

Hi Erel,

I did send CRLF to the char stream from PC for the test. I also checked the signal with logic analyzer, and the signal did reach to the input pin. Since I am pretty new to B4A, and I am not sure if I missed any necessary lines in my code. I did try AsyncStream and has no luck as well. Maybe I should post my code later, so you can help to fix my problem.

By the way, I am not sure the line YOYO.OpenUart(4, uart1.IP_FLOATING,5,uart1.OP_NORMAL,115200,uart1.PARITY_NONE,uart1.STOPBIT_ONE) is correct ot not, should I use IP_Pull)up or Pull_down? any specific hardware connection ? I have clue, any there are little info in B4A help. hope you or someone used B4A with IOIO and read UART data before. maybe you/he/she can give me some sample code that I can follow.
Thanks!
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The Basic4android IOIO library is a very thin wrapper for the actual IOIO library so you are talking direct to the Java API. All the information at Wiki for ytai's IOIO should be directly relevant.

The input pin mode required will depend upon the circuitry of your interface hardware.
 
Upvote 0

eric010101

Member
Licensed User
Longtime User
still not working with IOIO UART Receiving

'Activity module
Sub Process_Globals
Dim TextReader1 As TextReader
Dim TextWriter1 As TextWriter
Dim Timer1 As Timer
Dim connected As Boolean
Dim YOYO As IOIO
Dim led As DigitalOutput
Dim pin9 As DigitalInput
Dim uart1 As Uart
Dim ledflag As Boolean
Dim AStreams As AsyncStreams

End Sub

Sub Globals
Dim txtLog As EditText
Dim txtSend As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Timer1.Initialize("Timer1", 2000)
YOYO.Initialize
YOYO.WaitForConnect()
led = YOYO.OpenDigitalOutput( 0,led.OP_NORMAL ,True) ' Enable LED_PIN for output
uart1= YOYO.OpenUart(9,uart1.IP_FLOATING,4,uart1.OP_NORMAL,9600,uart1.PARITY_NONE,uart1.STOPBIT_ONE)
If uart1.IsInitialized Then
AStreams.Initialize(uart1.InputStream, uart1.OutputStream, "AStreams")
timer1.Enabled = True
connected = True
End If
End If
Activity.LoadLayout("1")
End Sub
Sub Activity_Resume

End Sub

Sub Timer1_Tick
If ledflag= True Then
ledflag=False
Else
ledflag=True
End If
led.Write(ledflag)

If connected Then
txtSend.Text="Hello"
If AStreams.IsInitialized = False Then Return
If txtSend.Text.Length > 0 Then
Dim buffer() As Byte
buffer = txtSend.Text.GetBytes("UTF8")
AStreams.Write(buffer)
txtSend.SelectAll
End If
End If
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
txtLog.Text = txtLog.Text & msg
End Sub
Sub AStreams_Error
ToastMessageShow(LastException.Message, True)
End Sub

I can send "hello" to pc with an max232 level converter, but I just can't receive any byte when I send yest from PC to IOIO board. can you help if I missed something?
 
Upvote 0

eric010101

Member
Licensed User
Longtime User
I had the ans

I solved the problem after serveral days fighting.

uart1= YOYO.OpenUart(5,uart1.IP_PULL_UP,4,uart1.OP_NORMAL,9600,uart1.PARITY_NONE,uart1.STOPBIT_ONE)
in1=uart1.InputStream
out1=uart1.OutputStream

If in1.BytesAvailable>0 Then
....
in1.readbyte
end if
 
Upvote 0

eric010101

Member
Licensed User
Longtime User
Uart Sample Project

here you can find my first sample project which can send and read UART via IOIO board. codes are tested with real IOIO board with a PC and a MAX232 Level converter. Hope someone can tell me how to use Astream with IOIO.
 

Attachments

  • B4A_IOIO_UART_TEST1.zip
    8.7 KB · Views: 684
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
Hi guys,

I am using the IOIO-OTG with v1.91 B4A lib, everything connects fine but is there a simple example of receiving a UART data input and displaying the TEXT that has been received???

What i want to do is keep the IOIO connected with the port open and receive a text data stream continuously.

I have :
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
   Dim YOYO As IOIO 
   Dim astreams As AsyncStreams
End Sub

Sub Globals
   Dim uart1 As Uart
   Dim instrm As InputStream
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Main")
   YOYO.Connect("yoyo",False,0,0) 
   YOYO.OpenUart("uart",11,uart1.IP_FLOATING,12,uart1.OP_NORMAL,4800,uart1.PARITY_NONE,uart1.STOPBIT_ONE)
End Sub

Sub yoyo_connected(noerror As Boolean)
   'connected event, if noerror than connection made
   If noerror Then
      Log("yoyo Connected")
      Log("yoyo Hardware: " & YOYO.GetIOIOVersion(YOYO.VER_HARDWARE))
      Log("yoyo Bootloader: " & YOYO.GetIOIOVersion(YOYO.VER_BOOTLOADER))
      Log("yoyo Firmware: " & YOYO.GetIOIOVersion(YOYO.VER_FIRMWARE))
      Log("IOIO Library: " & YOYO.GetIOIOVersion(YOYO.VER_IOIOLIB))
      Log("B4A Library: v"& YOYO.Version)
   Else
      Dim trouble As Exception
      trouble=LastException
      Log("yoyo "&trouble.Message) '&"yoyo State: "&yoyo.State&CRLF&CRLF)
   End If
End Sub

Sub uart_open(noerror As Boolean, result As Object, input As Object, output As Object)
   'get input and output stream as here well
   If noerror Then 
      uart1=result
      outstrm=output
      instrm=input
      
      Dim bufferout(5) As Byte
      Dim bufferin(5) As Byte
      'send bytes out on pin 11
      bufferout(0)=0x00
      bufferout(1)=0x01
      bufferout(2)=0x02
      bufferout(3)=0x03
      bufferout(4)=0x04
      outstrm.Flush
      outstrm.WriteBytes(bufferout,0,5)
      
      Log("RX on Pin 11 and TX on pin 12")
      Log("00 01 02 03 04 sent on pin 12")
      Log(instrm)
   End If
   
End Sub

My questions is after the above code where do i get the received data, do i have to use a timer (if so how?) or is an event raised when new data is received??

Thanks

Stu
 
Upvote 0

kolbe

Active Member
Licensed User
Longtime User
You need to use the instrm to check for available bytes and then read the bytes that are available. Check the ioio workbench code again. You will see an example there. You can see it work if you just touch the TX and RX pins when you hit the UART toggle.

For a small amounts of bytes this works fine but I find large amount of data can get tricky because you don't have great control over timing. If the inputstream overflows than bad things happen. I have a linksprite cmos camera working but I'm struggling with error handling. It can send up to 64k.
 
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
I have connected a serial output from the PC to the IOIO board to check connection at 4800baud, but i cant seem to see anything?

i did use workbench and hyperterminal onto pins 11 and 12 set to 300baud but when i click the uart toggle i don't see any output on the hyperterminal screen
 
Last edited:
Upvote 0

kolbe

Active Member
Licensed User
Longtime User
I hope you didn't connect it directly! The PC serial is RS-232 which can be up to ±15 volts. You will need a converter to connect the IOIO to the PC. You can only connect the IOIO to a TTL level (5 volts) or CMOS (3.3 volt) UART. If 5 volts than use the 5v tolerant pins on the IOIO. To transmit at 5v you will need to set it up in Open Drain mode. You will need something like this or this. You may have damaged the IOIO if you did connect directly and voltages great that 5v where present.


I have connected a serial output from the PC to the IOIO board to check connection at 4800baud, but i cant seem to see anything?

i did use workbench and hyperterminal onto pins 11 and 12 set to 300baud but when i click the uart toggle i don't see any output on the hyperterminal screen
 
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
Thanks kolbe,

I have just ordered a MAX3232 type board, hopefully i haven't damaged by IOIO-OTG, otherwise i will be ordering another one :(
 
Upvote 0

kolbe

Active Member
Licensed User
Longtime User
On my v1 board if I simply touch the TX and RX pins with a finger (they are right next to each other) and then toggle the uart button on IOIO workbench you see the 5 bytes transmitted and the same bytes received. This is a simple way to see the uart working. If you don't touch the pins you don't see the same bytes received.

Thanks kolbe,

I have just ordered a MAX3232 type board, hopefully i haven't damaged by IOIO-OTG, otherwise i will be ordering another one :(
 
Last edited:
Upvote 0

Beja

Expert
Licensed User
Longtime User
many i/o examples are very specific to special boards (ioio, arduino.etc).. very little if any for h/w engineers who design their own boards, and to clean up the example code from those bords' jargon is a pain in the neck.
 
Upvote 0
Top