Topics
All
MacOS
(Only)
Windows
(Only)
Linux
(Only, Not)
iOS
(Only, Not)
Components
Crossplatform Mac & Win
Server
Client
Old
Deprecated
Guides
Examples
Videos
New in version:
12.1
12.2
12.3
12.4
12.5
13.0
13.1
13.2
13.3
13.4
Statistic
FMM
Blog
Opens a serial port.
Component | Version | macOS | Windows | Linux | Server | iOS SDK |
SerialPort | 3.0 | ✅ Yes | ✅ Yes | ❌ No | ✅ Yes, on macOS and Windows | ❌ No |
Parameter | Description | Example | Flags |
---|---|---|---|
PortName | The name of the serial port to open. | "COM1" | |
Index | In case you have several ports with same name, you can specify an index here. Zero for first port with given name, 1 for the second port with given name. | 0 | Optional |
Returns the port reference value which you pass to other SerialPort functions.
Open port and close it:
$port = MBS( "SerialPort.Open"; "COM1" )
$r = MBS( "SerialPort.Write"; $port; "Hello")
$r = MBS( "SerialPort.Close"; $port )
A script to measure at a specific scale:
# Open port
Set Variable [ $serialPort ; Value: MBS("SerialPort.Open"; Scale::Port) ]
If [ MBS("IsError") ]
Show Custom Dialog [ "Failed to open serial port" ; $serialPort ]
Exit Script [ Text Result: ]
End If
# Send reset command
Set Variable [ $r ; Value: MBS("SerialPort.Write"; $serialPort; "@") ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 13) ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 10) ]
# Disable keyboard
Set Variable [ $r ; Value: MBS("SerialPort.Write"; $serialPort; "K 3") ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 13) ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 10) ]
# Set scale to zero
Set Variable [ $r ; Value: MBS("SerialPort.Write"; $serialPort; "Z") ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 13) ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 10) ]
# Set to measure in gramms
Set Variable [ $r ; Value: MBS("SerialPort.Write"; $serialPort; "M21 1 0") ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 13) ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 10) ]
Set Variable [ $r ; Value: MBS("SerialPort.Write"; $serialPort; "M21 0 0") ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 13) ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 10) ]
# Send expected weight and tolerance
Set Variable [ $r ; Value: MBS("SerialPort.Write"; $serialPort; "PMW REL "& Scale::WeightExpected & " " & Scale::Tolerance & " " & Scale::Tolerance & " g") ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 13) ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 10) ]
Set Variable [ $Counter ; Value: 1 ]
Set Variable [ $finished ; Value: 0 ]
Loop
Loop
# Wait for Answer
Set Variable [ $data ; Value: "" ]
Loop
Pause/Resume Script [ Duration (seconds): ,01 ]
Set Variable [ $newdata ; Value: MBS("SerialPort.Read"; $serialPort; 1000) ]
If [ MBS("IsError") = 0 ]
Set Variable [ $data ; Value: $data & $newData ]
End If
Exit Loop If [ Position($Data; "¶"; 1; 1)>0 ]
End Loop
Set Variable [ $data ; Value: MBS("Text.ReplaceNewline"; $data; 1) ]
If [ ValueCount ( $data ) = 1 ]
Set Variable [ $data ; Value: GetValue($data; 1) ]
End If
# Process Answer
If [ $data = "K A" ]
# Answer from keyboard turn off
# okay
Else If [ $data = "PMW A" ]
# Answer PMW
# okay
Else If [ $data = "K C 4" ]
# Return pressed on scale
Set Variable [ $r ; Value: MBS("SerialPort.Write"; $serialPort; "S") ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 13) ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 10) ]
# Wait for Answer
Set Variable [ $data ; Value: "" ]
Loop
Pause/Resume Script [ Duration (seconds): ,01 ]
Set Variable [ $newdata ; Value: MBS("SerialPort.Read"; $serialPort; 1000) ]
If [ MBS("IsError") = 0 ]
Set Variable [ $data ; Value: $data & $newData ]
End If
Exit Loop If [ Position($Data; "¶"; 1; 1)>0 ]
End Loop
Set Variable [ $data ; Value: MBS("Text.ReplaceNewline"; $data; 1) ]
If [ ValueCount ( $data ) = 1 ]
Set Variable [ $data ; Value: GetValue($data; 1) ]
End If
# Process Answer
If [ Left($Data; 3) = "S S" ]
Set Variable [ $Data ; Value: Substitute($Data; ","; ".") ]
Set Variable [ $Data ; Value: Substitute($Data; "."; ",") ]
Set Variable [ $Weight ; Value: GetAsNumber( Middle($Data; 4; 20)) ]
If [ Position ( $data; "kg"; 1;1) >0 ]
Set Variable [ $Weight ; Value: $Weight * 1000 ]
End If
Set Field [ Scale::Status ; "Weight read: " & $Weight ]
Set Field [ Scale::WeightGemessen ; $Weight ]
If [ ($Weight >= Scale::WeightExpected - Scale::Tolerance) and ($Weight <= Scale::WeightExpected + Scale::Tolerance) ]
Set Variable [ $finished ; Value: 1 ]
Show Custom Dialog [ "Thanks" ]
Exit Loop If [ 1 ]
Else
Show Custom Dialog [ "Please measure again" ]
End If
End If
Exit Loop If [ $finished ]
End If
Exit Loop If [ $finished ]
End Loop
Exit Loop If [ $finished ]
End Loop
# Enable keyboard
Set Variable [ $r ; Value: MBS("SerialPort.Write"; $serialPort; "K 2") ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 13) ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 10) ]
# Reset scale
Set Variable [ $r ; Value: MBS("SerialPort.Write"; $serialPort; "@") ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 13) ]
Set Variable [ $r ; Value: MBS("SerialPort.WriteByte"; $serialPort; 10) ]
# short wait to let commands run to device
Pause/Resume Script [ Duration (seconds): ,01 ]
# close port
Set Variable [ $r ; Value: MBS("SerialPort.Close"; $serialPort) ]
Record weight in predefined field:
#Change to reflect actual Layout
Go to Layout [ “MPO201 Input Weight” (MPORawMaterialColourInputs_MPO RM inputs) ]
Set Variable [ $portList; Value:MBS("SerialPort.List") ]
Show Custom Dialog [ Title: "Serial Ports"; Message: $portList; Default Button: “OK”, Commit: “Yes”; Button 2: “Cancel”, Commit: “No” ]
If [ Get ( LastMessageChoice ) = 2 ]
Exit Script [ ]
End If
#Change to reflect actual COM port
Set Variable [ $port; Value:"COM4" ]
Set Variable [ $serialPort; Value:MBS("SerialPort.Open"; $port) ]
If [ MBS("IsError") ]
Show Custom Dialog [ Title: "Error"; Message: "Serial port error " & $serialPort; Default Button: “OK”, Commit: “Yes” ]
Exit Script [ ]
End If
Set Variable [ $baudSet; Value:MBS("SerialPort.SetBaudRate"; $serialPort; 9600) ]
Set Variable [ $count; Value:0 ]
// New Record/Request
Loop
Set Variable [ $newScaleData; Value:MBS("SerialPort.Read"; $serialPort; 1000) ]
Exit Loop If [ $newScaleData ≠ "" ]
Exit Loop If [ $count > 100 ]
Pause/Resume Script [ Duration (seconds): .01 ]
Set Variable [ $count; Value:$count + 1 ]
End Loop
#Change to reflect fields
Set Field [ MPORawMaterialColourInputs_MPO RM inputs::mpoiw Input Weight Grams; $newScaleData ]
Set Field [ MPORawMaterialColourInputs_MPO RM inputs::mpoiw Input Weight Grams; Trim ( Left ( $newScaleData ; 9) ) ]
Commit Records/Requests
Set Variable [ $result; Value:MBS("SerialPort.Close"; $serialPort) ]
If [ MBS("IsError") ]
Show Custom Dialog [ Title: "Error"; Message: "Serial port error " & $result; Default Button: “OK”, Commit: “Yes” ]
Else
Show Custom Dialog [ Title: "Info"; Message: "Serial port closed " & $result; Default Button: “OK”, Commit: “Yes” ]
End If
This function checks for a license.
Created 18th August 2014, last changed 6th May 2019