#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.
Private usbserial As felUsbSerial
Private manager As UsbManager
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 Spinner1 As Spinner
Private Button2 As Button
Private Button3 As Button
Private EditText1 As EditText
Dim usblist As List
Type DeviceInfo ( DeviceID As String, _
Baud As Int, _
StopBits As Int , _
DataBits As Int , _
Parity As Int , _
Flow As Int )
Dim devInfo As DeviceInfo
Dim D_str
Private Label1 As Label
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("t1")
manager.Initialize
usblist.Initialize
devInfo.Initialize
devInfo.DeviceID = ""
devInfo.Baud = 115200
devInfo.StopBits = usbserial.STOP_BITS_1
devInfo.DataBits = usbserial.DATA_BITS_8
devInfo.Parity = usbserial.PARITY_EVEN
devInfo.Flow =usbserial.FLOW_CONTROL_RTS_CTS
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Button3_Click
Dim b(4) As Byte
b=StringToHas(EditText1.Text)
If usbserial.IsInitialized Then
usbserial.Write(b)
Label1.Text=b(0)&b(1)&b(2)&b(3)
End If
End Sub
Sub StringToHas(StrValue As String)As Byte()
StrValue = StrValue.Replace(Chr(13),"")
StrValue = StrValue.Replace(Chr(10),"")
Dim str As String
Dim buffstr() As String
buffstr=split(" ",StrValue)
Dim b(buffstr.Length) As Byte
Log(buffstr.Length)
Log(b.Length)
For i=0 To buffstr.Length-1
Dim sum As Int
str=buffstr(i)
If str.CompareTo("")<>0 Then
For j=0 To str.Length-1
If HasToInt(Asc(str.CharAt(j)))<>17 Then
Log(Power(16,str.Length-j-1))
sum=sum+HasToInt(Asc(str.CharAt(j)))*Power(16,str.Length-j-1)
End If
Next
b(i)=sum
End If
Next
Label1.Text=(0)&b(1)&b(2)&b(3)
Return b
End Sub
Sub HasToInt(i As Int) As Int
If i >47 And i<58 Then
i=i-48
Else if i>64 And i<71 Then
i=i-65+10
Else if i>96 And i<103 Then
i=i-97+10
Else
i=17
End If
Return i
End Sub
Sub split(Cutting As Char,buff As String)As String()
Dim str As String
Dim buffstr(buff.Length) As String
Dim j As Int = 0
For i=0 To buff.Length-1
If buff.CharAt(i)=Cutting Then
If str.Length <>0 Then
buffstr(j)=str
j=j+1
str=""
End If
Else
str=str&buff.CharAt(i)
End If
buffstr(j)=str
Next
Dim buffstr1(j+1) As String
For i=0 To buffstr1.Length-1
buffstr1(i)=buffstr(i)
Next
Return buffstr1
End Sub
Sub Button1_Click
If manager.GetDevices.Length >0 Then
For i=0 To manager.GetDevices.Length-1
Spinner1.Add(manager.GetDevices(i).DeviceName)
usblist.Add(manager.GetDevices(i))
Next
End If
End Sub
Sub Button2_Click
Dim usb_D As UsbDevice
usb_D=manager.GetDevices(0)
If usb_D.DeviceName.CompareTo(D_str)=0 Then
If manager.HasPermission(usb_D) = False Then
ToastMessageShow("Please allow connection and click again.", True)
manager.RequestPermission(usb_D)
Else
usbserial.Initialize2("serial", usb_D, -1,"CH34xSerialDevice")
usbserial.BaudRate = devInfo.Baud
usbserial.DataBits = devInfo.DataBits
usbserial.StopBits = devInfo.StopBits
usbserial.Parity = devInfo.Parity
usbserial.FlowControl = devInfo.Flow
usbserial.StartReading '开始监听
Label1.Text="开始监听"
End If
End If
Label1.Text=Spinner1.SelectedItem
End Sub
Private Sub serial_DataAvailable (Buffer() As Byte)As String
Msgbox("usbserial_DataAvailable","usbserial_DataAvailable")
Label1.Text=Buffer.Length
Dim str,RText As String
RText=""
If Buffer.Length = 0 Then
Msgbox( "return_s","show_text.Text")
Return Null
End If
For i=0 To Buffer.Length-1
str=Bit.ToHexString(Buffer(i))&" "
If str.Length<>3 Then
If str.Length>2 Then
str=str.SubString(str.Length-3)
Else
str="0"&str
End If
End If
RText= RText&str
Next
Label1.Text=RText
Return RText
End Sub
Sub Spinner1_ItemClick (Position As Int, Value As Object)
D_str=Spinner1.GetItem(Position)
End Sub