SerialPort.Open
---------------

Opens a serial port.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [SerialPort](component_SerialPort.md) | [3.0](newinversion30.md) | ✅ Yes | ✅ Yes | ❌ No | ✅ Yes, on macOS and Windows | ❌ No |

MBS( "SerialPort.Open"; PortName { ; Index } ) 

**MBS**( **"SerialPort.Open";** /\* Opens a serial port. \*/  
**$PortName**; /\* The name of the serial port to open.e.g. "COM1" \*/   
**$Index**) /\* Optional; 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.e.g. 0 \*/ 

### 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](component_SerialPort.md) functions.

### Description

Opens a serial port.  
You get the names from [SerialPort.List](SerialPortList.md) function. You can even open ports, if they are not in the list.  
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 &gt; COM9 on Windows.  
### Examples

Open port and close it:

 $port = MBS ( "SerialPort.Open"; "COM1" )  
$r = MBS ( "[SerialPort.Write](SerialPortWrite.md)"; $port ; "Hello")  
$r = MBS ( "[SerialPort.Close](SerialPortClose.md)"; $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](SerialPortWrite.md)"; $serialPort ; "@") \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 13) \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 10) \]   
\# Disable keyboard   
Set Variable \[ $r ; Value: MBS ("[SerialPort.Write](SerialPortWrite.md)"; $serialPort ; "K 3") \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 13) \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 10) \]   
\# Set scale to zero   
Set Variable \[ $r ; Value: MBS ("[SerialPort.Write](SerialPortWrite.md)"; $serialPort ; "Z") \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 13) \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 10) \]   
\# Set to measure in gramms   
Set Variable \[ $r ; Value: MBS ("[SerialPort.Write](SerialPortWrite.md)"; $serialPort ; "M21 1 0") \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 13) \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 10) \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.Write](SerialPortWrite.md)"; $serialPort ; "M21 0 0") \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 13) \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 10) \]   
\# Send expected weight and tolerance   
Set Variable \[ $r ; Value: MBS ("[SerialPort.Write](SerialPortWrite.md)"; $serialPort ; "PMW REL "&amp; Scale::WeightExpected &amp; " " &amp; Scale::Tolerance &amp; " " &amp; Scale::Tolerance &amp; " g") \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 13) \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $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](SerialPortRead.md)"; $serialPort ; 1000) \]   
 If \[ MBS ("IsError") = 0 \]   
 Set Variable \[ $data ; Value: $data &amp; $newData \]   
 End If  
 Exit Loop If \[ Position($Data ; "¶"; 1; 1)&gt;0 \]   
 End Loop  
 Set Variable \[ $data ; Value: MBS ("[Text.ReplaceNewLine](TextReplaceNewLine.md)"; $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](SerialPortWrite.md)"; $serialPort ; "S") \]   
 Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 13) \]   
 Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 10) \]   
 # Wait for Answer  
 Set Variable \[ $data ; Value: "" \]   
 Loop  
 Pause/Resume Script \[ Duration (seconds): ,01 \]   
 Set Variable \[ $newdata ; Value: MBS ("[SerialPort.Read](SerialPortRead.md)"; $serialPort ; 1000) \]   
 If \[ MBS ("IsError") = 0 \]   
 Set Variable \[ $data ; Value: $data &amp; $newData \]   
 End If  
 Exit Loop If \[ Position($Data ; "¶"; 1; 1)&gt;0 \]   
 End Loop  
 Set Variable \[ $data ; Value: MBS ("[Text.ReplaceNewLine](TextReplaceNewLine.md)"; $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) &gt;0 \]   
 Set Variable \[ $Weight ; Value: $Weight \* 1000 \]   
 End If  
 Set Field \[ Scale::Status ; "Weight read: " &amp; $Weight \]   
 Set Field \[ Scale::WeightGemessen ; $Weight \]   
 If \[ ($Weight &gt;= Scale::WeightExpected - Scale::Tolerance ) and ($Weight &lt;= 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](SerialPortWrite.md)"; $serialPort ; "K 2") \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 13) \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 10) \]   
\# Reset scale   
Set Variable \[ $r ; Value: MBS ("[SerialPort.Write](SerialPortWrite.md)"; $serialPort ; "@") \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $serialPort ; 13) \]   
Set Variable \[ $r ; Value: MBS ("[SerialPort.WriteByte](SerialPortWriteByte.md)"; $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](SerialPortClose.md)"; $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](SerialPortList.md)") \]  
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 " &amp; $serialPort ; Default Button: “OK”, Commit: “Yes” \]  
 Exit Script \[ \]  
End If  
  
Set Variable \[ $baudSet ; Value:MBS ("[SerialPort.SetBaudRate](SerialPortSetBaudRate.md)"; $serialPort ; 9600) \]  
Set Variable \[ $count ; Value:0 \]  
  
// New Record/Request  
  
Loop  
 Set Variable \[ $newScaleData ; Value:MBS ("[SerialPort.Read](SerialPortRead.md)"; $serialPort ; 1000) \]  
 Exit Loop If \[ $newScaleData ≠ "" \]  
 Exit Loop If \[ $count &gt; 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](SerialPortClose.md)"; $serialPort ) \]  
  
If \[ MBS ("IsError") \]  
 Show Custom Dialog \[ Title: "Error"; Message: "Serial port error " &amp; $result ; Default Button: “OK”, Commit: “Yes” \]  
Else  
 Show Custom Dialog \[ Title: "Info"; Message: "Serial port closed " &amp; $result ; Default Button: “OK”, Commit: “Yes” \]  
End If  
Opens the port and set a few settings:

 Set Variable \[ $Port ; Value: MBS ("SerialPort.Open"; “COM1”) \]  
Set Variable \[ $r ; Value: MBS ( "[SerialPort.SetDataBits](SerialPortSetDataBits.md)"; $port ; 7 ) \]  
Set Variable \[ $r ; Value: MBS ( "[SerialPort.SetBaudRate](SerialPortSetBaudRate.md)"; $port ; 4800 ) \]  
Set Variable \[ $r ; Value: MBS ( "[SerialPort.SetStopBits](SerialPortSetStopBits.md)"; $port ; 2 ) \]  
Set Variable \[ $r ; Value: MBS ("[SerialPort.SetParity](SerialPortSetParity.md)"; $port ; 2) // even \]  
### See also

- [SerialPort.Clear](SerialPortClear.md)
- [SerialPort.Read](SerialPortRead.md)
- [SerialPort.SetParity](SerialPortSetParity.md)
- [SerialPort.SetRTS](SerialPortSetRTS.md)
- [SerialPort.SetStopBits](SerialPortSetStopBits.md)
- [SerialPort.SetTag](SerialPortSetTag.md)
- [SerialPort.Setup](SerialPortSetup.md)
- [SerialPort.SetXON](SerialPortSetXON.md)
- [SerialPort.Suspend](SerialPortSuspend.md)
- [SerialPort.WriteByte](SerialPortWriteByte.md)

### Release notes

- **Version 13.4**
    - Added [SerialPort.OpenPath](https://www.mbsplugins.eu/SerialPortOpenPath.shtml) function, so you can use [SerialPort](https://www.mbsplugins.eu/component_SerialPort.shtml) functions to open special files or pipes.
    - Added [SerialPort.WaitNamedPipe](https://www.mbsplugins.eu/SerialPortWaitNamedPipe.shtml) function to wait for a pipe and use with [SerialPort.OpenPath](https://www.mbsplugins.eu/SerialPortOpenPath.shtml) on Windows.
- **Version 10.1**
    - Improved error message for [SerialPort.Open](https://www.mbsplugins.eu/SerialPortOpen.shtml) to include port name.
- **Version 9.2**
    - Added workaround for [SerialPort.Open](http://www.mbsplugins.eu/SerialPortOpen.shtml) to avoid Windows bug with COM ports &gt;9
    - Improved error message for [SerialPort.Open](http://www.mbsplugins.eu/SerialPortOpen.shtml) to include name of the COM port.
- **Version 8.3**
    - Fixed [SerialPort.Open](http://www.mbsplugins.eu/SerialPortOpen.shtml) for Windows to work with COM10 now.

### Example Databases

- [Network/Serial Port](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/Serial%20Port.shtml#4ScriptAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 10.1pr6](https://www.mbsplugins.de/archive/2020-03-02/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 9.2pr7](https://www.mbsplugins.de/archive/2019-05-15/MBS_FileMaker_Plugin_version_9/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 9.2pr6](https://www.mbsplugins.de/archive/2019-05-03/MBS_FileMaker_Plugin_version_9/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 8.3pr5](https://www.mbsplugins.de/archive/2018-07-10/MBS_FileMaker_Plugin_version_8/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 6.2pr10](https://www.mbsplugins.de/archive/2016-05-20/MBS_FileMaker_Plugin_version_6/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 5.3pr1](https://www.mbsplugins.de/archive/2015-08-26/MBS_FileMaker_Plugin_version_5/monkeybreadsoftware_blog_filemaker)
- [Serial Port functions for MBS Filemaker Plugin](https://www.mbsplugins.de/archive/2012-12-01/Serial_Port_functions_for_MBS_/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 18th August 2014 , last changed 16th September 2025

  
[SerialPort.List](SerialPortList.md) - [SerialPort.OpenPath](SerialPortOpenPath.md)

[HTML Version](SerialPortOpen.shtml)