Skip to content

Commit ba7f812

Browse files
authored
TimeoutException F# snippet (#7883)
1 parent d97e7a1 commit ba7f812

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="to.fs" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//<snippet1>
2+
// This example demonstrates the use of the TimeoutException
3+
// exception in conjunction with the SerialPort class.
4+
5+
open System
6+
open System.IO.Ports
7+
8+
try
9+
// Set the COM1 serial port to speed = 4800 baud, parity = odd,
10+
// data bits = 8, stop bits = 1.
11+
let sp = new SerialPort("COM1", 4800, Parity.Odd, 8, StopBits.One)
12+
// Timeout after 2 seconds.
13+
sp.ReadTimeout <- 2000
14+
sp.Open()
15+
16+
// Read until either the default newline termination string
17+
// is detected or the read operation times out.
18+
let input = sp.ReadLine()
19+
20+
sp.Close()
21+
22+
// Echo the input.
23+
printfn $"{input}"
24+
25+
// Only catch timeout exceptions.
26+
with :? TimeoutException as e ->
27+
printfn $"{e}"
28+
(*
29+
This example produces the following results:
30+
31+
(Data received at the serial port is echoed to the console if the
32+
read operation completes successfully before the specified timeout period
33+
expires. Otherwise, a timeout exception like the following is thrown.)
34+
35+
System.TimeoutException: The operation has timed-out.
36+
at System.IO.Ports.SerialStream.ReadByte(Int32 timeout)
37+
at System.IO.Ports.SerialPort.ReadOneChar(Int32 timeout)
38+
at System.IO.Ports.SerialPort.ReadTo(String value)
39+
at System.IO.Ports.SerialPort.ReadLine()
40+
at Sample.main()
41+
*)
42+
//</snippet1>

xml/System/TimeoutException.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
7676
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/TimeoutException.class/cpp/to.cpp" id="Snippet1":::
7777
:::code language="csharp" source="~/snippets/csharp/System/TimeoutException/Overview/to.cs" id="Snippet1":::
78+
:::code language="fsharp" source="~/snippets/fsharp/System/TimeoutException/Overview/to.fs" id="Snippet1":::
7879
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/TimeoutException.class/VB/to.vb" id="Snippet1":::
7980
8081
]]></format>

0 commit comments

Comments
 (0)