-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeBehind.cs
155 lines (133 loc) · 5.31 KB
/
CodeBehind.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Net;
using System.Net.Sockets;
using ABB.Robotics.Math;
using ABB.Robotics.RobotStudio;
using ABB.Robotics.RobotStudio.Stations;
using System;
using System.Collections.Generic;
using System.Text;
namespace MatrixComm
{
/// <summary>
/// Code-behind class for the MatrixComm Smart Component.
/// </summary>
/// <remarks>
/// The code-behind class should be seen as a service provider used by the
/// Smart Component runtime. Only one instance of the code-behind class
/// is created, regardless of how many instances there are of the associated
/// Smart Component.
/// Therefore, the code-behind class should not store any state information.
/// Instead, use the SmartComponent.StateCache collection.
/// </remarks>
public class CodeBehind : SmartComponentCodeBehind
{
#region Properties
private IPEndPoint endPoint { get; set; }
private Socket commSocket { get; set; }
const int msgSize = 64;
private static byte[] msgBuffer = new byte[msgSize];
private SmartComponent _component { get; set; }
#endregion
#region Initialization
public CodeBehind()
{
// code behind initialization
}
private void InitializeSocket(String IP, int port)
{
endPoint = new IPEndPoint(address: IPAddress.Parse(IP), port: port);
commSocket = new Socket(endPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
commSocket.Connect(endPoint);
}
private void CloseSocket()
{
//commSocket.Shutdown(SocketShutdown.Both);
commSocket.Close();
}
#endregion
/// <summary>
/// Called when the value of a dynamic property value has changed.
/// </summary>
/// <param name="component"> Component that owns the changed property. </param>
/// <param name="changedProperty"> Changed property. </param>
/// <param name="oldValue"> Previous value of the changed property. </param>
public override void OnPropertyValueChanged(SmartComponent component, DynamicProperty changedProperty, Object oldValue)
{
}
public override void OnLoad(SmartComponent component)
{
Logger.AddMessage(new LogMessage($"{component.Name} component loaded! Simulation must be running in order for it to work!"));
component.Properties["Matrix"].Value = "Simulation must be started and component activated!";
}
public override void OnSimulationStop(SmartComponent component)
{
component.IOSignals["Activate"].Value = false;
}
/// <summary>
/// Called when the value of an I/O signal value has changed.
/// </summary>
/// <param name="component"> Component that owns the changed signal. </param>
/// <param name="changedSignal"> Changed signal. </param>
public override void OnIOSignalValueChanged(SmartComponent component, IOSignal changedSignal)
{
if (changedSignal.Name.Equals("Activate"))
{
if (changedSignal.Value.Equals(1))
{
try
{
InitializeSocket((String)component.Properties["IP"].Value, (int)component.Properties["port"].Value);
}
catch (SocketException)
{
component.IOSignals["Activate"].Value = false;
}
catch (Exception)
{
throw;
}
}
else
{
CloseSocket();
}
}
}
/// <summary>
/// Called during simulation.
/// </summary>
/// <param name="component"> Simulated component. </param>
/// <param name="simulationTime"> Time (in ms) for the current simulation step. </param>
/// <param name="previousTime"> Time (in ms) for the previous simulation step. </param>
/// <remarks>
/// For this method to be called, the component must be marked with
/// simulate="true" in the xml file.
/// </remarks>
public override void OnSimulationStep(SmartComponent component, double simulationTime, double previousTime)
{
// if activated
if (component.IOSignals["Activate"].Value.Equals(1))
{
// if there is data to be read
if (commSocket.Available > 0)
{
commSocket.Receive(msgBuffer, msgSize, SocketFlags.None);
var matrix = "";
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
matrix += (String.Format("{0:0.00}", BitConverter.ToSingle(msgBuffer, 4 * i + j))).PadRight(12);
}
if (i < 3)
{
matrix += "\r\n";
}
}
component.Properties["Matrix"].Value = matrix;
commSocket.Send(msgBuffer);
}
}
}
}
}