-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.cs
More file actions
123 lines (87 loc) · 3.5 KB
/
Session.cs
File metadata and controls
123 lines (87 loc) · 3.5 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Common.Debug;
using System;
namespace OneWireAPI
{
public class Session
{
public short PortNumber { get; private set; }
public short PortType { get; private set; }
public int SessionHandle { get; private set; }
public Network Network { get; private set; }
public byte[] StateBuffer { get; private set; }
public Session()
{
// Create the global state buffer
StateBuffer = new byte[(int) TMEX.StateBufferSize.NoEpromWriting];
short portNumber;
short portType;
// Get the default port number and type from the system
var result = TMEX.TMReadDefaultPort(out portNumber, out portType);
PortNumber = portNumber;
PortType = portType;
Tracer.WriteLine("TMReadDefaultPort - Return: {0}, Port Number: {1}, Port Type: {2}", result, PortNumber, PortType);
}
public Session(short portNumber, short portType)
{
// Create the global state buffer
StateBuffer = new byte[(int) TMEX.StateBufferSize.NoEpromWriting];
// Store the port number and type specified
PortNumber = portNumber;
PortType = portType;
}
public bool Acquire()
{
// Create a byte array to hold the version information
var version = new byte[80];
// Get the version
TMEX.Get_Version(version);
// Decode the version
var sVersion = System.Text.Encoding.Default.GetString(version, 0, version.Length);
// Strip everything up to the first null character
sVersion = sVersion.Substring(0, sVersion.IndexOf("\0", StringComparison.Ordinal));
Tracer.WriteLine("Version: {0}", sVersion);
Tracer.WriteLine("Starting Aquire");
// Start a session on the port
SessionHandle = TMEX.TMExtendedStartSession(PortNumber, PortType, IntPtr.Zero);
Tracer.WriteLine("TMExtendedStartSession - Return: {0}", SessionHandle);
// If we didn't get a session then throw an error
if (SessionHandle <= 0)
return false;
// Setup the port for the current session
var result = TMEX.TMSetup(SessionHandle);
Tracer.WriteLine("TMSetup - Return: {0}", result);
// Check the result
if (result != 1)
{
// Release the session
Release();
return false;
}
// Create the network object and pass ourself as the session
Network = new Network(this);
// Initialize the network
Network.Initialize();
// Initialize the static adapter code with the session
Adapter.Initialize(this);
return true;
}
public void Release()
{
Tracer.WriteLine("Starting Release");
// Terminate the network
if (Network != null)
{
Network.Terminate();
Network = null;
}
// Close the session
var result = TMEX.TMClose(SessionHandle);
Tracer.WriteLine("TMClose - Return: {0}", result);
// End the session
result = TMEX.TMEndSession(SessionHandle);
Tracer.WriteLine("TMEndSession - Return: {0}", result);
// Clear the session variable
SessionHandle = 0;
}
}
}