Topics
All
MacOS
(Only)
Windows
(Only)
Linux
(Only, Not)
iOS
(Only, Not)
Components
Crossplatform Mac & Win
Server
Client
Old
Deprecated
Guides
Examples
Videos
New in version:
9.3
9.4
9.5
10.0
10.1
10.2
10.3
10.4
10.5
10.6
Statistic
FMM
Blog
SerialPort.Read
Reads bytes from serial port and returns them as text.
| Component | Version | macOS | Windows | Linux | Server | FileMaker iOS SDK |
| SerialPort | 3.0 | Yes | Yes | No | Yes | No |
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.
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
- IsError
- SerialPort.AvailableBytes
- SerialPort.Close
- SerialPort.List
- SerialPort.Open
- SerialPort.ReadByte
- SerialPort.ReadLine
- SerialPort.Resume
- SerialPort.SetDataAvailableEvaluate
- SerialPort.SetDataAvailableHandler
Blog Entries
- MBS Filemaker Plugin, version 4.5pr1
- MBS Filemaker Plugin, version 3.2pr6
- MBS Filemaker Plugin, version 3.2pr3
- Serial Port functions for MBS Filemaker Plugin
Release notes
- Version 7.3
- Fixed issue with SerialPort.ReadLine being off by one.
- Version 7.0
- Added SerialPort.HasLine and SerialPort.ReadLine functions.
Created 18th August 2014, last changed 11st May 2018
SerialPort.Open - SerialPort.ReadByte
Feedback: Report problem or ask question.
Links
MBS Xojo blog