FSharp.Plc.Ads


Reading and writing of TIME/DATE variables

Sample based on Beckhoff Infosys - Sample 05

Task

A .Net application should read and write a date and a time.

Description

The PLC contains the TIME variable MAIN.Time1 and the DT variable MAIN.Date1.

PLC program

1: 
2: 
3: 
4: 
5: 
ROGRAM MAIN
VAR 
    Time1:TIME := T#21h33m23s231ms;
    Date1:DT:=DT#1993-06-12-15:36:55.40;
END_VAR

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: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
#r "FSharp.Plc.Ads.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 dt = DateTime.Now
let time = dt.TimeOfDay

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

plc {
  readAny "MAIN.Time1"
}
|> function 
  | Choice1Of3 (time: TimeSpan) -> ()// handle success
  | Choice2Of3 errMsg -> ()// handle error
  | Choice3Of3 (adsCode,additionalInfo) -> ()// handle ADS error

plc {
  readAny "MAIN.Date1"
}
|> function 
  | Choice1Of3 (date: DateTime) -> ()// 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: Readwritedatetime.amsNetId
val amsPort : int

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

Full name: Readwritedatetime.plc
val dt : DateTime

Full name: Readwritedatetime.dt
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
DateTime()
   (+0 other overloads)
DateTime(ticks: int64) : unit
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
   (+0 other overloads)
property DateTime.Now: DateTime
val time : TimeSpan

Full name: Readwritedatetime.time
property DateTime.TimeOfDay: TimeSpan
union case Choice.Choice1Of3: 'T1 -> Choice<'T1,'T2,'T3>
union case Choice.Choice2Of3: 'T2 -> Choice<'T1,'T2,'T3>
union case Choice.Choice3Of3: 'T3 -> Choice<'T1,'T2,'T3>
Multiple items
type TimeSpan =
  struct
    new : ticks:int64 -> TimeSpan + 3 overloads
    member Add : ts:TimeSpan -> TimeSpan
    member CompareTo : value:obj -> int + 1 overload
    member Days : int
    member Duration : unit -> TimeSpan
    member Equals : value:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member Hours : int
    member Milliseconds : int
    member Minutes : int
    ...
  end

Full name: System.TimeSpan

--------------------
TimeSpan()
TimeSpan(ticks: int64) : unit
TimeSpan(hours: int, minutes: int, seconds: int) : unit
TimeSpan(days: int, hours: int, minutes: int, seconds: int) : unit
TimeSpan(days: int, hours: int, minutes: int, seconds: int, milliseconds: int) : unit
Fork me on GitHub