Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

SerialPort.Read

Reads bytes from serial port and returns them as text.

Component Version macOS Windows Linux Server iOS SDK
SerialPort 3.0 ✅ Yes ✅ Yes ❌ No ✅ Yes, on macOS and Windows ❌ No
MBS( "SerialPort.Read"; PortReference; length { ; Encoding } )   More

Parameters

Parameter Description Example Flags
PortReference The reference number of the port. You get this number from SerialPort.Open when you open the port. $port
length Maximum number of bytes to read. 10
Encoding The text encoding for result.
Default is native.
This function can also handle UTF-16 as well as UTF-16LE and UTF-16BE for little/big endian byte order.
Possible encoding names: ANSI, ISO-8859-1, Latin1, Mac, Native, UTF-8, DOS, Hex, Base64 or Windows. More listed in the FAQ.
UTF8 Optional

Result

Returns text or error message.

Description

Reads bytes from serial port and returns them as text.
The text returned may be shorter than length bytes.

You may need to put in script pauses between sending a request and reading the response as the other device may need a second to process the request, answer and for the answer to be received.
See also SerialPort.AvailableBytes function.

Examples

Read up to 8 bytes and decode them from Windows text encoding into FileMaker:

$value = MBS( "SerialPort.Read"; $port; 8; "windows" )

Read in loop as long as data comes in:

# read data until end
Set Variable [$data; Value:""]
Loop
    Pause/Resume Script [Duration (seconds): ,1]
    #exit when no more data
    Exit Loop If [not (MBS( "SerialPort.AvailableBytes"; $port ) > 0)]
    #read some data
    Set Variable [$newdata; Value:MBS( "SerialPort.Read"; $port; 10000; "windows" )]
    Set Variable [$data; Value:$data & $newdata]
    #next
    Pause/Resume Script [Duration (seconds): ,1]
End Loop

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

Release notes

Blog Entries

This function is free to use.

Created 18th August 2014, last changed 7th November 2023


SerialPort.Path - SerialPort.ReadByte