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.2   7.3   7.4   7.5   8.0   8.1   8.2   8.3   8.4   8.5    Statistic  

SerialPort.Read

Reads bytes from serial port and returns them as text.

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

MBS( "SerialPort.Read"; PortReference; length { ; Encoding } )

Parameters

Parameter Description Example value
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 Optional
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, Arabic-Mac, Arabic-Win, Baltic-Win, CentralEurope-Mac, ChineseSimp-Mac, ChineseSimp-Win, ChineseTrad-Mac, ChineseTrad-Win, Cyrillic-Mac, Cyrillic-Win, EasternEurope-Win, Greek-Mac, Greek-Win, Hebrew-Mac, Hebrew-Win, ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-15, Korean-Johab, Korean-Mac, Korean-Win, Latin1, Mac, Native, ShiftJIS-Mac, ShiftJIS-Win, Turkish-Mac, Turkish-Win, UTF-8, DOS or Windows. Pass native to use the native encoding of the current platform.
UTF8

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.
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

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


SerialPort.Open   -   SerialPort.ReadByte

Feedback: Report problem or ask question.




Links
MBS FileMaker blog