This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
UrgDeviceEthernet.cs
255 lines (227 loc) · 7.14 KB
/
UrgDeviceEthernet.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*!
* \file
* \brief Get distance data from Ethernet type URG
* \author Jun Fujimoto
* $Id: get_distance_ethernet.cs 403 2013-07-11 05:24:12Z fujimoto $
*/
using UnityEngine;
using System.Threading;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using SCIP_library;
public class UrgDeviceEthernet : UrgDevice
{
// private Thread listenThread;
private Thread clientThread;
TcpClient tcpClient;
public List<long> distances;
public List<long> strengths;
// private Queue messageQueue;
private string ip_address = "192.168.0.10";
private int port_number = 10940;
public void StartTCP(string ip = "192.168.0.10", int port = 10940)
{
// messageQueue = Queue.Synchronized(new Queue());
ip_address = ip;
port_number = port;
distances = new List<long>();
strengths = new List<long>();
try
{
tcpClient = new TcpClient();
var result = tcpClient.BeginConnect(ip_address, port_number, null, null);
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));
if (!success)
{
throw new Exception("Failed to connect.");
}
// we have connected
tcpClient.EndConnect(result);
// tcpClient.Connect(ip_address, port_number);
Debug.Log("Connect setting = IP Address : " + ip_address + " Port number : " + port_number.ToString());
// this.listenThread = new Thread(new ThreadStart(ListenForClients));
// this.listenThread.Start();
ListenForClients();
}
catch (Exception ex)
{
Debug.Log(ex.Message);
}
finally
{
}
}
void OnDisable()
{
DeInit();
}
void OnApplicationQuit()
{
DeInit();
}
void DeInit()
{
if (tcpClient != null)
{
if (tcpClient.Connected)
{
NetworkStream stream = tcpClient.GetStream();
if (stream != null)
{
stream.Close();
}
}
tcpClient.Close();
}
if (this.clientThread != null)
{
this.clientThread.Abort();
}
}
public void Write(string scip)
{
NetworkStream stream = tcpClient.GetStream();
write(stream, scip);
}
private void ListenForClients()
{
clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(tcpClient);
}
private void HandleClientComm(object obj)
{
try
{
using (TcpClient client = (TcpClient)obj)
{
using (NetworkStream stream = client.GetStream())
{
// NetworkStream clientStream = client.GetStream();
while (true)
{
long time_stamp = 0;
string receive_data = read_line(stream);
// messageQueue.Enqueue( receive_data );
string cmd = GetCommand(receive_data);
lock (distances)
{
if (cmd == GetCMDString(CMD.MD))
{
//measure distance only
distances.Clear();
SCIP_Reader.MD(receive_data, ref time_stamp, ref distances);
}
else if (cmd == GetCMDString(CMD.ME))
{
//measure distance and strength
distances.Clear();
strengths.Clear();
SCIP_Reader.ME(receive_data, ref time_stamp, ref distances, ref strengths);
}
else
{
Debug.Log(">>" + receive_data);
}
}
}
// client.Close();
}
}
}
catch (System.Exception ex)
{
Debug.LogWarning("error: " + ex);
}
}
string GetCommand(string get_command)
{
string[] split_command = get_command.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
return split_command[0].Substring(0, 2);
}
bool CheckCommand(string get_command, string cmd)
{
string[] split_command = get_command.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
return split_command[0].StartsWith(cmd);
}
// void Update()
// {
// lock(messageQueue.SyncRoot){
// if(messageQueue.Count > 0){
// string receive_data = messageQueue.Dequeue().ToString();
// long time_stamp;
// if(CheckCommand(receive_data, "MD")){
// distances.Clear();
// time_stamp = 0;
//
// SCIP_Reader.MD(receive_data, ref time_stamp, ref distances);
// //Debug.Log("time stamp: " + time_stamp.ToString() + " / count: "+distances.Count);
// }else if(CheckCommand(receive_data, "GD")){
// distances.Clear();
// time_stamp = 0;
//
// SCIP_Reader.GD(receive_data, ref time_stamp, ref distances);
// }else{
// Debug.Log(">>"+receive_data);
// }
// }
// }
//
// }
/// <summary>
/// Read to "\n\n" from NetworkStream
/// </summary>
/// <returns>receive data</returns>
static string read_line(NetworkStream stream)
{
if (stream.CanRead)
{
StringBuilder sb = new StringBuilder();
bool is_NL2 = false;
bool is_NL = false;
do
{
char buf = (char)stream.ReadByte();
if (buf == '\n')
{
if (is_NL)
{
is_NL2 = true;
}
else
{
is_NL = true;
}
}
else
{
is_NL = false;
}
sb.Append(buf);
} while (!is_NL2);
return sb.ToString();
}
else
{
return null;
}
}
/// <summary>
/// write data
/// </summary>
static bool write(NetworkStream stream, string data)
{
if (stream.CanWrite)
{
byte[] buffer = Encoding.ASCII.GetBytes(data);
stream.Write(buffer, 0, buffer.Length);
return true;
}
else
{
return false;
}
}
}