#Region Project Attributes
#ApplicationLabel: SisoftVendingComunicationTest
#VersionCode: 1
#VersionName:
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: True
#IncludeTitle: True
#End Region
#BridgeLogger: True
'Obbiettivo dell'applicazione:
'Sarà un'app di test volta alla creazione di un codice di comunicazione di una chiavetta DONGLE USB IRDA
'che manderà messaggi in formato DDCMP
'passi apparentemente da seguire:
'-Collegare usb al device e riconoscerne il VID e PID [FATTO MEDIANTE CODICE JAVA]
'-Costruire un messaggio in formato DDCMP
'-Mandare il messagigo costruito mediante la comunicazione della USB To IRDA collegata al Modulo Irda nelle vicinanze
'-stampare i risultati
Sub Process_Globals
Private xui As XUI
End Sub
Sub Globals
Private btnTest As Button
Private btnsetting As Button
Private txtaudit As EditText
Dim screen As PreferenceScreen
Dim baudrate As String
Dim Maxbaudrate As String
Dim manager As PreferenceManager
Dim timeout As String
Dim usb1 As UsbSerial
Dim astreams1 As AsyncStreams
Dim START() As Byte
Dim WHOAREYOU() As Byte
Dim STD_ACK() As Byte
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
CreatePreferenceScreen
If manager.GetAll.Size = 0 Then SetDefaults
End If
Activity.LoadLayout("Layout")
End Sub
Sub Activity_Resume
HandleSettings
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Private Sub btnsetting_Click
StartActivity(screen.CreateIntent)
End Sub
Public Sub StartTest
Log("entra")
Dim device As Int = 1
Dim info As String
Dim deviceName As String
Dim deviceId As String
Dim productId As String
Dim vendorId As String
Dim product As String
Dim manufacture As String
'Guida BaudRate "2400", "4800", "9600", "19200", "38400", "57600", "115200"
'Controllo se è presente un dispositivo USB
If usb1.UsbPresent(device) = usb1.USB_NONE Then : Msgbox("Nessuna USB-IrDa Rilevaya, Collegane una e riprova", "Attenzione") : Return : End If
'mi da errore con: DRIVER_CDCACM | DRIVER_PROLIFIC
'non da errore con: DRIVER_FTDI | DRIVER_SILABS
'con DRIVER_SILABS ha inviato ma da questo errore: Error: java.lang.NullPointerException: Attempt to invoke interface method 'int com.hoho.android.usbserial.driver.UsbSerialDriver.read(byte[], int)' on a null object reference
'usb1.SetCustomDevice(usb1.DRIVER_SILABS, 0x66F , 0x4200) modello y60 worka ma Error: java.lang.NullPointerException: Attempt to invoke interface method 'int com.hoho.android.usbserial.driver.UsbSerialDriver.read(byte[], int)' on a null object reference
'usb1.SetCustomDevice(usb1.DRIVER_SILABS, 0x18EC, 0x3118) UsbToIrda worka ma Error: java.lang.NullPointerException: Attempt to invoke interface method 'int com.hoho.android.usbserial.driver.UsbSerialDriver.read(byte[], int)' on a null object reference
'Se è presente, ne verifico i permessi
If (usb1.HasPermission(device)) Then
' Stampare VID e PID ed eventuali altre informazioni
Log("Presence Device = " & usb1.UsbPresent(device))
info = usb1.DeviceInfo(device)
'Log(info)
product = ExtractValue(info, "Product: " )
manufacture = ExtractValue(info, "Manufacture: ")
deviceName = ExtractValue(info, "DeviceName : ")
deviceId = ExtractValue(info, "Device ID : " )
productId = ExtractValue(info, "ProductId : " )
vendorId = ExtractValue(info, "VendorId : " )
Log("product : " & product)
Log("Manufacture : " & manufacture)
Log("Device Name : " & deviceName)
Log("Device ID : " & deviceId)
Log("ProductId : " & productId)
Log("VendorId : " & vendorId)
If(productId = "0x4200" And vendorId == "0x66F") Then
usb1.SetCustomDevice(usb1.DRIVER_SILABS, 0x66F, 0x4200)
Log("Impostata USB - Model Y con Driver_Silabs")
End If
If(productId = "0x3118" And vendorId == "0x18EC") Then
usb1.SetCustomDevice(usb1.DRIVER_SILABS, 0x18EC, 0x3118)
Log("Impostata USB - Model UsbToIrda con Driver_Silabs")
End If
Dim dev As Int = usb1.Open(baudrate,1) : Log ("USB Open : " & dev)
If dev <> usb1.USB_NONE Then
Log("USB Connessa")
'inizializzo lo Stream
Log("InputStream: " & usb1.GetInputStream) : Log("OutputStream: " & usb1.GetOutputStream)
astreams1.Initialize(usb1.GetInputStream, usb1.GetOutputStream, "astreams1")
Else
Log("Errore nella connessione della USB")
End If
Else
usb1.RequestPermission(device)
Return
End If
'Configurare i parametri seriali
usb1.SetParameters(baudrate, usb1.DATABITS_8, usb1.STOPBITS_1, usb1.PARITY_NONE)
'Messaggio da inviare
BuildStartMessage
WHOAREYOU = Array As Byte(0x81, 0x10, 0x40, 0x00, 0x01, 0x01, 0x0A, 0x42, 0x77,0xE0)
STD_ACK = Array As Byte(0x05, 0x01, 0x40, 0x01, 0x00, 0x01, 0xB8, 0x55)
If(Not(astreams1.IsInitialized)) Then
Log("asstream1 non inizializzato, impossibile inviare il msg")
Return
Else
Log("asstream1 inizializzato")
usb1.UsbTimeout = timeout
If(astreams1.OutputQueueSize == 0) Then
SendMessage(START,"START")
Sleep(1000)
SendMessage(START,"START 2")
Sleep(1000)
SendMessage(WHOAREYOU,"WHOAREYOU")
Sleep(1000)
SendMessage(STD_ACK,"STD_ACK")
Sleep(1000)
End If
Log("Dati Output in attesa: " & astreams1.OutputQueueSize)
End If
End Sub
Sub BuildStartMessage()
Select baudrate
Case "1200"
START = Array As Byte(0x05, 0x06, 0x40, 0x00, 0x01, 0x01, 0x5B, 0x95)
Case "2400"
START = Array As Byte(0x05, 0x06, 0x40, 0x00, 0x02, 0x01, 0x5B, 0x95)
Case "4800"
START = Array As Byte(0x05, 0x06, 0x40, 0x00, 0x03, 0x01, 0x5B, 0x95)
Case "9600"
START = Array As Byte(0x05, 0x06, 0x40, 0x00, 0x04, 0x01, 0x5B, 0x95)
Case "19200"
START = Array As Byte(0x05, 0x06, 0x40, 0x00, 0x05, 0x01, 0x5B, 0x95)
Case "38400"
START = Array As Byte(0x05, 0x06, 0x40, 0x00, 0x06, 0x01, 0x5B, 0x95)
Case "57600"
START = Array As Byte(0x05, 0x06, 0x40, 0x00, 0x07, 0x01, 0x5B, 0x95)
Case "115200"
START = Array As Byte(0x05, 0x06, 0x40, 0x00, 0x08, 0x01, 0x5B, 0x95)
Case Else
START = Array As Byte(0x05, 0x06, 0x40, 0x00, 0x08, 0x01, 0x5B, 0x95)
End Select
End Sub
Sub SendMessage(msg() As Byte,msglbl As String)
astreams1.Write(msg)
' Converte il messaggio in una stringa esadecimale formattata
Dim hexMsg As String = ""
For Each b As Byte In msg
hexMsg = hexMsg & Bit.ToHexString(Bit.And(b, 0xFF)).ToUpperCase & ","
Next
hexMsg = hexMsg.SubString2(0, hexMsg.Length - 1) ' Rimuove l'ultima virgola finale
Log("Messaggio " & msglbl & " inviato " & hexMsg)
End Sub
Sub btnTest_Click
txtaudit.Text = ""
StartTest
End Sub
Sub astreams1_NewData (Buffer() As Byte)
Dim hexMsg As String = ""
For Each b As Byte In Buffer
hexMsg = hexMsg & Bit.ToHexString(Bit.And(b, 0xFF)).ToUpperCase & ","
Next
hexMsg = hexMsg.SubString2(0, hexMsg.Length - 1) ' Rimuove l'ultima virgola finale
Log("Messaggio ricevuto " & hexMsg)
txtaudit.Text = txtaudit.Text & " " & hexMsg
End Sub
Sub astreams1_DataAvailable (Buffer() As Byte)
Dim receivedData As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log("DataAvailable ricevuti: " & receivedData)
End Sub
Sub astreams1_Error
Log("Error: " & LastException.Message)
astreams1.Close
End Sub
Sub astreams1_Terminated
Log("Terminated")
astreams1.Close
End Sub
Sub SetDefaults
manager.SetString("start_baud_rate_string", "2400")
manager.SetString("max_baud_rate_string", "115200")
manager.SetString("timeout_string", "500")
baudrate = manager.GetString("start_baud_rate_string")
Maxbaudrate = manager.GetString("max_baud_rate_string")
timeout = manager.GetString("timeout_string")
PrintInfo
End Sub
Sub CreatePreferenceScreen
'BAUD RATE [115200,19200,2400,38400,4800,57600,9600]
Dim moduleSettings As PreferenceCategory
screen.Initialize("", "")
moduleSettings.Initialize("")
moduleSettings.AddEditText("security_string","Security","DDCMP security code","") 'SECURITY CODE FOR DDCMP
moduleSettings.AddEditText("password_string","Password","DDCMP password code","") 'PASSWORD CODE FOR DDCMP
moduleSettings.AddEditText("timeout_string","timeout","START timeout","") 'timeout for start message
moduleSettings.AddList("max_baud_rate_string", "Max Baud", "Default", "Default", _
Array As String("Default", "2400", "4800", "9600", "19200", "38400", "57600", "115200","460800"))
moduleSettings.AddList("start_baud_rate_string", "Start Baud", "Default", "Default", _
Array As String("Default", "2400", "4800", "9600", "19200", "38400", "57600", "115200" , "460800"))
screen.AddPreferenceCategory(moduleSettings)
End Sub
Sub HandleSettings
baudrate = manager.GetString("start_baud_rate_string")
Maxbaudrate = manager.GetString("max_baud_rate_string")
timeout = manager.GetString("timeout_string")
manager.SetString("start_baud_rate_string", baudrate)
manager.SetString("max_baud_rate_string" , Maxbaudrate)
manager.SetString("timeout_string" , timeout)
PrintInfo
End Sub
Sub PrintInfo
Log("BaudRate " & baudrate)
Log("Max BaudRate " & Maxbaudrate)
Log("Start TimeOut " & timeout)
End Sub
Sub ExtractValue(info As String, label As String) As String
Dim startPos As Int = info.IndexOf(label) + label.Length
Dim endPos As Int = info.IndexOf2(Chr(10), startPos) ' Cerca la fine della riga
If endPos = -1 Then endPos = info.Length ' Se non trova il termine, prende tutto fino alla fine
Return info.SubString2(startPos, endPos).Trim
End Sub