Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Util Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Util/SerialPort
This example is the version from Fri, 4th Aug 2022.
Project "SerialPort.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control PopupDevice Inherits PopupMenu
ControlInstance PopupDevice Inherits PopupMenu
EventHandler Sub Change()
ConnectButton.Enabled = me.SelectedRowIndex >= 0
End EventHandler
End Control
Control ConnectButton Inherits PushButton
ControlInstance ConnectButton Inherits PushButton
EventHandler Sub Action()
port = Nil // close old
port = new MySerialPort
port.list = list
port.Open(PopupDevice.Text)
port.DataBits = 8
port.BaudRate = 9600
SendButton.Enabled = true
Exception r as RuntimeException
MessageBox "Failed: "+r.message
End EventHandler
End Control
Control Input Inherits TextArea
ControlInstance Input Inherits TextArea
End Control
Control SendButton Inherits PushButton
ControlInstance SendButton Inherits PushButton
EventHandler Sub Action()
if port <> nil then
dim v as string = ConvertEncoding(input.Text, encodings.UTF8)
dim n as integer = port.Write(v)
InfoLabel.Text = str(n)+" Bytes sent."
else
MessageBox "Open port first!"
end if
End EventHandler
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control InfoLabel Inherits Label
ControlInstance InfoLabel Inherits Label
End Control
EventHandler Sub Open()
dim names() as string = MySerialPort.List
for each n as string in names
PopupDevice.AddRow n
next
End EventHandler
Property Port As MySerialPort
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MySerialPort Inherits SerialPortMBS
EventHandler Sub DataAvailable()
dim n as integer = me.AvailableBytes
if n > 0 then
dim s as string = me.Read(n)
dim a as string = ReplaceNonPrintableCharactersMBS(s)
List.AddRow "Read "+str(s)+" of "+str(n)+" bytes from buffer."
List.AddRow "Text: "+a
List.AddRow "Hex: "+s
List.AddRow ""
List.ScrollPosition = list.RowCount
end if
End EventHandler
Property List As listbox
End Class
End Project
The items on this page are in the following plugins: MBS Util Plugin.