Components | All | New | MacOS | Windows | Linux | iOS | ||||
Examples | Mac & Win | Server | Client | Guides | Statistic | FMM | Blog | Deprecated | Old |
SerialPort.Open
Opens a serial port.
Component | Version | macOS | Windows | Linux | Server | iOS SDK |
SerialPort | 3.0 | ✅ Yes | ✅ Yes | ❌ No | ✅ Yes, on macOS and Windows | ❌ No |
Parameters
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 |
Result
Returns the port reference value which you pass to other SerialPort functions.
Description
Opens a serial port.You get the names from SerialPort.List function.
The port is opened with default settings.
Serial Port reference numbers are starting at 28000 and counting up for each new port.
Even if port is not showing in list, it may still open here, e.g. virtual COM ports on Windows.
You can open several ports with different connections, e.g. by calling SerialPort.Open once for each connection.
Version 9.2 introduced a workaround to make this work for port numbers > COM9 on Windows.
Examples
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
See also
- SerialPort.Clear
- SerialPort.Read
- SerialPort.SetDataBits
- SerialPort.SetParity
- SerialPort.SetRTS
- SerialPort.SetStopBits
- SerialPort.SetTag
- SerialPort.SetXON
- SerialPort.Suspend
- SerialPort.WriteByte
Release notes
- Version 13.4
- Added SerialPort.OpenPath function, so you can use SerialPort functions to open special files or pipes.
- Added SerialPort.WaitNamedPipe function to wait for a pipe and use with SerialPort.OpenPath on Windows.
- Version 10.1
- Improved error message for SerialPort.Open to include port name.
- Version 9.2
- Added workaround for SerialPort.Open to avoid Windows bug with COM ports >9
- Improved error message for SerialPort.Open to include name of the COM port.
- Version 8.3
- Fixed SerialPort.Open for Windows to work with COM10 now.
Example Databases
Blog Entries
- MBS FileMaker Plugin, version 10.1pr6
- MBS FileMaker Plugin, version 9.2pr7
- MBS FileMaker Plugin, version 9.2pr6
- MBS FileMaker Plugin, version 8.3pr5
- MBS FileMaker Plugin, version 6.2pr10
- MBS FileMaker Plugin, version 5.3pr1
- Serial Port functions for MBS Filemaker Plugin
This function checks for a license.
Created 18th August 2014, last changed 6th May 2019