Italian inviare dati a porta seriale

allbykes

Member
ciao a tutti.
qualcuno mi può suggerire come fare per inviare una stringa (ad es. "abcd") alla porta seriale ?
 

bgsoft

Well-Known Member
Licensed User
Longtime User
Ciao

Creare un layout con Designer, chiamata "1" e mettere tre pulsanti ovunque con i nomi: btnSend, btnOpen, btnClose

B4X:
Sub Process_Globals
    Dim usb As UsbSerial
    Dim astreams As AsyncStreams
End Sub

Sub Globals

    Dim btnSend, btnOpen, btnClose As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    btnClose.Enabled = False
    btnSend.Enabled = False
End Sub

Sub btnOpen_Click
    If usb.UsbPresent = usb.USB_NONE Then
        Log("Msgbox - no device")
        Msgbox("No USB device or accessory detected!", "Error")
        Log("Msgbox - returned")
        Return
    End If
    Log("Checking permission")
    If (usb.HasPermission) Then
        Msgbox(usb.DeviceInfo, "Device Information")
        Dim dev As Int
        dev = usb.Open(115200)       
        If dev <> usb.USB_NONE Then
            Log("Connected successfully!")
            btnOpen.Enabled = False
            btnClose.Enabled = True
            btnSend.Enabled = True           
            astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")
        Else
            Log("Error opening USB port")
        End If
    Else
        usb.RequestPermission
    End If
End Sub

Sub Astreams_NewData (Buffer() As Byte)
    Log("NewData")
    Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub btnClose_Click
    astreams.Close
    btnOpen.Enabled = True
    btnClose.Enabled = False
    btnSend.Enabled = False   
End Sub

Sub btnSend_Click
    astreams.Write("abcde".GetBytes("UTF8"))
End Sub

Sub btnExit_Click
    ExitApplication
End Sub

Sub AStreams_Error
    Log("Error: " & LastException)
    astreams.Close
End Sub
Sub Astreams_Terminated
    Log("Terminated")
    astreams.Close
End Sub

Saluti
 

tetox

New Member
Salve...tenete in considerazione che sono solo " uno smanettone"...
Volevo provare il codice qua' sopra postato ..ho caricato queste librerie " Core Version 2.71 ,Network Version 1.24,RandomAccesFile 1.34 , Serial Version 1.23 ,USB Version 0.97 "..malgrado tutto questo mi segnala errore su
" Dim usb As UsbSerial " usb e UsbSerial " sono in rosso.
Cosa altro occorre ??? grazie
Stefano
 

bgsoft

Well-Known Member
Licensed User
Longtime User
" Dim usb As UsbSerial " usb e UsbSerial " sono in rosso.

Devi fare il usbserial libreria

Ciao
 
Top