Android Question Add Event on serial input

Discussion in 'Android Questions' started by freetoair, Sep 30, 2015.

  1. freetoair

    freetoair Member Licensed User

    Is it possible to add an event when new data arrives at "serinp = File.OpenInput ("", "/ dev / ttyS0")" ?
     
  2. Roycefer

    Roycefer Well-Known Member Licensed User

    Have you looked at the Serial library and AsyncStreams?
     
    Dysanix likes this.
  3. freetoair

    freetoair Member Licensed User

  4. Roycefer

    Roycefer Well-Known Member Licensed User

    Well, do. It might be for what you are looking.
     
    Dysanix likes this.
  5. freetoair

    freetoair Member Licensed User

    Code:
    sout = File.OpenOutput("","/dev/ttyS0",True' open serial output
          sinp = File.OpenInput("","/dev/ttyS0"' open serial input
        astreams.Initialize(sinp,sout, "astreams")
     
  6. freetoair

    freetoair Member Licensed User

    Whether this can be written, but I do not know which buffer to use?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Whether it will work or not depends on the file handle behavior. AsyncStreams tries to read all the available data and raises the NewData whenever there is data available.

    Try it and see what happens (there is no buffer involved).
     
  8. freetoair

    freetoair Member Licensed User

    Yes, on the transmit side is ok, but I had to add "sout.Flush" after each "astreams.Write (Array As Byte (Convert.outpkt (2)))" .But the rx side seems to astreams not deleted input buffer (sinp) when reading. At least, it seems to me when I follow the flow of data on to the serial terminal.
     
  9. freetoair

    freetoair Member Licensed User

    Now I've tried and the service module, and do not know if this is right?
    Code:
    #Region  Service Attributes
        
    #StartAtBoot: 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.
       
    '    sout = File.OpenOutput("","/dev/ttyS0",True) ' open serial output
    '       sinp = File.OpenInput("","/dev/ttyS0") ' open serial input
    '   
        Dim inpkt(128As Byte ' serial comm input packet buffer
        Dim sout As OutputStream ' serial out stream
          Dim sinp As InputStream ' serial in stream
        Dim c, rxlen As Int
        
    Dim s As String
    End Sub

    Sub Service_Create
        sout = 
    File.OpenOutput("","/dev/ttyS0",True' open serial output
          sinp = File.OpenInput("","/dev/ttyS0"' open serial input
        End Sub

    Sub Service_Start (StartingIntent As Intent)

        rxlen = sinp.BytesAvailable 
    ' check if bytes available on serial input
       If rxlen > 0 Then
         sinp.ReadBytes(inpkt,
    0,rxlen) ' read pending characters
          s = "" ' clear string
          For c = 0 To rxlen-1 ' build string from characters
             s = s & Chr(inpkt(c))
          
    Next
             
    'Log(s)
            ToastMessageShow (s,True)
       
    End If

    End Sub

    Sub Service_Destroy

    End Sub
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Why aren't you using AsyncStreams?

    Shouldn't you use a timer to check whether there are bytes available?
     
  11. freetoair

    freetoair Member Licensed User

    Yes, I used AsyncStreams but never rises Astreams_newdata. When is connect to " astreams.Initialize (usb.GetInputStream, usb.GetOutputStream, "astreams")" everything is working normally. I was able to find documentation from the manufacturer (DWIN model: DMT80480T070_32WT) of the modules and their example is operating normally. But unfortunately example and API is written in Java so for me is not a lot of help.
     
  12. freetoair

    freetoair Member Licensed User

    I want to connect to an audio processor that is controlled by the microcontroller which has a connection via the serial port, so do this Android machine looked ideal for it. But I guess I'll have to use a USB to COM adapter.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice