Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server   Client   Old   Guides   Examples
New in version: 7.3   7.4   7.5   8.0   8.1   8.2   8.3   8.4   8.5   8.6    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 } )   More

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) ]

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

Example Databases

Blog Entries

Created 18th August 2014, last changed 11st May 2018


SerialPort.List   -   SerialPort.Read

Feedback: Report problem or ask question.




Links
MBS Xojo blog