Android Question Need help on IOIO uart

chaiwatt

Member
Licensed User
Longtime User
Hi experts,
I need getting string data streaming from my external RS232 device by using the IOIO uart method. I searched around forum but not found exact solution. Present, I can connect to IOIO board and calling openUart but I can't get data streaming, I attached code and diagram as below. Appreciated much for all reply.
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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  'IOIO V1.7
Dim uart1 As Uart
Dim in1 As InputStream
Dim out1 As OutputStream
Dim TimerRX As Timer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private Button4 As Button
    Private lbStatus As Label
    Private txtRX As EditText
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("1")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    yoyo.Initialize (True)
    yoyo.WaitForConnect ()
Msgbox("connected","")

End Sub
Sub Button2_Click
    yoyo.Disconnect
End Sub
Sub Button3_Click
    uart1=yoyo.OpenUart (5,uart1.IP_FLOATING ,4,uart1.OP_NORMAL,9600,uart1.PARITY_NONE ,uart1.STOPBIT_ONE )
    in1=uart1.InputStream
    out1=uart1.OutputStream
    TimerRX.Initialize("TimerRX",100)
    TimerRX.Enabled=True
End Sub
Sub Button4_Click
    uart1.Close
    in1.Close
    out1.Close
End Sub

Sub TimerRX_Tick()

If in1.BytesAvailable>0 Then

    TimerRX.Enabled=False
    Log ("IN")
   
End If
TimerRX.Enabled=True
End Sub
 

Attachments

  • diagram.jpg
    diagram.jpg
    7.3 KB · Views: 270
  • connection.zip
    478.7 KB · Views: 323

Tom Christman

Active Member
Licensed User
Longtime User
This works with IOIO Jar & XML 2.0
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: YOYOUART
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: True
#End Region

'Activity module
'YOYO UART

Sub Process_Globals

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim period As Int
    Dim TimerLoop As Timer
    Dim TimerRX As Timer

    Dim btnSend As Button
    'Dim btnSetUp As Button
    Dim Label1 As Label
    Dim Label10 As Label
    Dim Label3 As Label
    Dim Label4 As Label
    Dim Label5 As Label
    Dim Label6 As Label
    Dim Label7 As Label
    Dim Label8 As Label
    Dim Label9 As Label
    Dim rbtn_loop As RadioButton
    Dim rbtn_Single As RadioButton
   
    Dim txtRX As EditText
    Dim txtSend As EditText

    'Dim PeriodMap As Map
    Dim p_uart As Panel
    Dim panel1 As Panel
    Dim lbStatus As Label
   
    Dim YOYO As IOIO
    Dim led As DigitalOutput
    Dim uart1 As Uart
    Dim in1 As InputStream
    Dim out1 As OutputStream
    Dim go As Boolean
    Dim LedFlag As Boolean
    Dim looped As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim panels(5) As Panel
Activity.LoadLayout("1")
Activity.Title="Tom's UART TEST"
If FirstTime Then
    go=False
    looped=False
    rbtn_Single.Checked=True   
   
    YOYO.Initialize
    YOYO.WaitForConnect()
       
    TimerLoop.Initialize("TimerLoop",10)
    TimerRX.Initialize("TimerRX",100)

    led = YOYO.OpenDigitalOutput( 0,led.OP_NORMAL ,True) ' Enable LED_PIN for output
    uart1= YOYO.OpenUart(3,uart1.IP_FLOATING,4,uart1.OP_NORMAL,9600,uart1.PARITY_NONE,uart1.STOPBIT_ONE)
   
    in1=uart1.InputStream
    out1=uart1.OutputStream

End If
   
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub rbtn_Single_CheckedChange(Checked As Boolean)
If Checked Then
    looped=    False
End If
End Sub
Sub rbtn_loop_CheckedChange(Checked As Boolean)
If Checked Then
    looped=True
End If
End Sub
Sub btnSetUp_Click()
If go=True Then
    go=False
    'btnSetUp.Text="SET/GO"
    'btnSetUp.Background=black
    TimerLoop.Enabled=False
    TimerRX.Enabled=False
Else
    go=True
    'btnSetUp.Text="STOP"
    uart1.Close
    in1.Close
    out1.Close
    uart1= YOYO.OpenUart(3,uart1.IP_FLOATING,4,uart1.OP_NORMAL,9600,uart1.PARITY_NONE,uart1.STOPBIT_ONE)   
    in1=uart1.InputStream
    out1=uart1.OutputStream
    If rbtn_Single.Checked=False Then
        TimerLoop.Initialize("TimerLoop",100)
        TimerLoop.Enabled=True
    End If
    TimerRX.Enabled=True
    Msgbox("READY TO GO","")
End If
End Sub
Sub btnSend_Click
'If rbtn_Single.Checked=True Then
If uart1.IsInitialized Then
    'getSetUPValue
    'uart1.Close
    'IN1.Close
    'OUT1.Close
    'uart1= YOYO.OpenUart(RXPIN,RXPINTYPE,TXPIN,TXPINTYPE,BAUDRATE,uart1.PARITY_NONE,uart1.STOPBIT_ONE)
    'in1=uart1.InputStream
    'out1=uart1.OutputStream
     If txtSend.Text.Length > 0 Then
        Dim buffer() As Byte
        Dim ST As String
        ST=txtSend.Text & CRLF
        buffer =ST.GetBytes("UTF8")
        out1.WriteBytes(buffer,0,buffer.Length)
        out1.Flush
        txtSend.SelectAll
    End If
End If
End Sub
Sub TimerLoop_Tick
If LedFlag= True Then
     LedFlag=False
Else
    LedFlag=True
End If
led.Write(LedFlag)
If  rbtn_Single.Checked=False Then
    If txtSend.Text.Length > 0 Then
        Dim buffer() As Byte
        Dim ST As String
        ST=txtSend.Text & CRLF
        buffer =ST.GetBytes("UTF8")
        out1.WriteBytes(buffer,0,buffer.Length)
        out1.Flush
        txtSend.SelectAll
    End If
End If
End Sub
Sub TimerRX_Tick()
If in1.BytesAvailable>0 Then
    TimerRX.Enabled=False
    '*** need to assign the exact buffer size as in BytesAvailable or it won't work
    Dim buffer(in1.BytesAvailable) As Byte
    Dim count As Int
    count = in1.ReadBytes(buffer, 0,buffer.Length)
    lbStatus.Text="Read " & count & " bytes. @ " & DateTime.Time(DateTime.Now)& " "& DateTime.Date(DateTime.Now)
    txtRX.Text=BytesToString(buffer, 0, count, "UTF8")
End If
TimerRX.Enabled=True
End Sub
Tom
 
Upvote 0

chaiwatt

Member
Licensed User
Longtime User
Hi Tom, I can capture the data coming. But seem they're unreadble format. It's look something like %%@** .
 
Upvote 0
Top