FSharp.Plc.Ads


Accessing an array in the PLC

Sample based on Beckhoff Infosys - Sample 01

Task

The PLC contains an array that is to be read by the .Net application using a read command.

Description

The PLC contains an array of 100 elements of type integer (2 bytes). The array in the PLC is to be filled with the values from 3500 to 3599.

PLC program

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
PROGRAM MAIN
VAR
  PLCVar : ARRAY [0..99] OF INT;
  Index: BYTE;
END_VAR
FOR Index := 0 TO 99 DO
  PLCVar[Index] := 3500 + INDEX;
END_FOR

F# program

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
#r "FSharp.Plc.Ads.dll"
//using IEC 61131-3 type aliases for convenience
#r "NuSoft.FSharp.IEC.DataTypes.dll"
open FSharp.Plc.Ads.Experimental
open System
let amsNetId = "192.168.68.132.1.1"
let amsPort = 801
//instantiate builder for specific TwinCAT instance
let plc = createClient amsNetId amsPort

let myInputArray = [| 3500s .. 3599s |]

plc {
  writeAny "MAIN.PLCVar" myInputArray
}
|> function 
  | Choice1Of3 _ -> ()// handle success
  | Choice2Of3 errMsg -> ()// handle error
  | Choice3Of3 (adsCode,additionalInfo) -> ()// handle ADS error

plc {
  readAny "MAIN.PLCVar"
}
|> function 
  | Choice1Of3 (readArray: INT array) -> ()// handle success
  | Choice2Of3 errMsg -> ()// handle error
  | Choice3Of3 (adsCode,additionalInfo) -> ()// handle ADS error
namespace Microsoft.FSharp
Multiple items
type ExperimentalAttribute =
  inherit Attribute
  new : message:string -> ExperimentalAttribute
  member Message : string

Full name: Microsoft.FSharp.Core.ExperimentalAttribute

--------------------
new : message:string -> ExperimentalAttribute
namespace System
val amsNetId : string

Full name: Accessarray.amsNetId
val amsPort : int

Full name: Accessarray.amsPort
val plc : (obj -> Choice<obj,obj,(obj * obj)>)

Full name: Accessarray.plc
val myInputArray : int16 []

Full name: Accessarray.myInputArray
union case Choice.Choice1Of3: 'T1 -> Choice<'T1,'T2,'T3>
union case Choice.Choice2Of3: 'T2 -> Choice<'T1,'T2,'T3>
val errMsg : obj
union case Choice.Choice3Of3: 'T3 -> Choice<'T1,'T2,'T3>
val adsCode : obj
val additionalInfo : obj
val readArray : obj
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
Fork me on GitHub