Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private ComboBox As ComboBox
Private TextAreaInfo As TextArea
'
Private SerialManager As Serial
Private aStream As AsyncStreams
'
Private aesKey(16) As Byte
Private BtGenerateKeys As Button
Private BtReadKeys As Button
'
Private const CMD_GENERATE_KEYS As Byte = 0x1A
Private const CMD_READ_KEYS As Byte = 0x1B
'
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show
SerialManager.Initialize("SerialManager")
loadPorts
End Sub
Private Sub loadPorts
ComboBox.SelectedIndex = -1
ComboBox.Items.Clear
ComboBox.Items.AddAll(SerialManager.ListPorts)
End Sub
#region "Form controls"
Sub BtRefresh_Click
loadPorts
End Sub
Sub ComboBox_SelectedIndexChanged(Index As Int, Value As Object)
If Index = -1 Then Return
If Index >= SerialManager.ListPorts.Size Then Return
'
ConnectSerial(SerialManager.ListPorts.Get(Index))
End Sub
Sub AddTextLine(text As String)
TextAreaInfo.text = text & CRLF & TextAreaInfo.text
End Sub
Sub BtGenerateKeys_Click
AddTextLine("Generate new key request")
SendCommandToSerial(CMD_GENERATE_KEYS)
End Sub
Sub BtReadKeys_Click
AddTextLine("Key request")
SendCommandToSerial(CMD_READ_KEYS)
End Sub
#end region
#region "Serial manager"
Sub ConnectSerial(port As String)
AddTextLine("Connection to: " & port)
SerialManager.Open(port)
'SerialManager.SetParams(SerialManager.BAUDRATE_38400, SerialManager.DATABITS_8, SerialManager.STOPBITS_1, SerialManager.PARITY_NONE)
SerialManager.SetParams(SerialManager.BAUDRATE_38400, 8, 1, 0)
aStream.Initialize(SerialManager.GetInputStream, SerialManager.GetOutputStream, "aStream")
End Sub
private Sub SendCommandToSerial(command As Byte)
If aStream.IsInitialized Then
aStream.Write(Array As Byte(command))
Dim hexStr As String = Bit.ToHexString(command).ToUpperCase
hexStr = hexStr.SubString(hexStr.Length -2)
End If
End Sub
Sub AStream_NewData (Buffer() As Byte)
If Buffer.Length = 0 Then Return
If Buffer(0) = CMD_GENERATE_KEYS Then
AddTextLine("Keys generated. Ask for new keys")
'Keys generated: send read command
SendCommandToSerial(CMD_READ_KEYS)
else if Buffer(0) = CMD_READ_KEYS Then
'
Dim str As String = "0x"
For i=0 To 16-1
'Add key values
aesKey(i) = Buffer(i+1)
'
str = str & Bit.ToHexString(aesKey(i)).ToUpperCase.Replace("FFFFFF", "")
Next
AddTextLine("Received Keys: " & str)
'
Else
'Received data not handled
End If
End Sub
Sub AStream_Error
Log("Error: " & LastException)
AddTextLine("Connection error: " & LastException)
aStream.Close
AStream_Terminated
End Sub
Sub AStream_Terminated
Log("Connection is broken.")
AddTextLine("Connection broken")
'
loadPorts
End Sub
#end region