Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server (Not)   Client   Old   Guides   Examples
New in version: 6.5   7.0   7.1   7.2   7.3   7.4   7.5   8.0   8.1   8.2    Statistic  

SerialPort.Open

Opens a serial port.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
SerialPort 3.0 Yes Yes Yes No No

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

Parameters

Parameter Description Example value
PortName The name of the serial port to open. "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.
0

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.

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("String.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("String.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) ]

See also

Example Databases


SerialPort.List   -   SerialPort.Read

Feedback: Report problem or ask question.




Links
MBS FileMaker tutorial videos