FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Netduino-gcode/gcodeviewer/CncDeviceClient.cs at master · dsuarezv/Netduino-gcode · GitHub
dsuarezv
Netduino-gcode
Repository navigation
Code
Issues
1
(1)
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Netduino-gcode
/
gcodeviewer
/
CncDeviceClient.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
182 lines (147 loc) · 4.8 KB
Breadcrumbs
Netduino-gcode
/
gcodeviewer
/
CncDeviceClient.cs
Copy path
File metadata and controls
182 lines (147 loc) · 4.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Text
;
using
System
.
Net
;
using
System
.
Net
.
Sockets
;
using
System
.
Windows
.
Forms
;
namespace
gcodeparser
{
public
class
CncDeviceClient
{
private
ConnectionState
mState
;
private
Control
mUiControl
;
public
event
CncDeviceDelegate
Connected
;
public
event
CncDeviceDelegate
Disconnected
;
public
event
CncDeviceDelegate
ErrorRaised
;
public
event
CncDeviceDelegate
DataSent
;
public
event
CncDeviceResponseDelegate
DataReceived
;
public
CncDeviceClient
(
string
hostName
,
int
port
,
Control
uiControl
)
{
// El tcp client se conecta directamente. Hay que usar un socket sin más.
mUiControl
=
uiControl
;
mState
=
new
ConnectionState
(
hostName
,
port
)
;
mState
.
Socket
=
new
Socket
(
AddressFamily
.
InterNetwork
,
SocketType
.
Stream
,
ProtocolType
.
Tcp
)
;
}
public
void
Connect
(
)
{
mState
.
Socket
.
BeginConnect
(
mState
.
HostName
,
mState
.
Port
,
new
AsyncCallback
(
OnConnect
)
,
mState
)
;
}
public
void
Send
(
string
line
)
{
if
(
!
mState
.
Socket
.
Connected
)
throw
new
Exception
(
"The device is not connected"
)
;
byte
[
]
buffer
=
Encoding
.
UTF8
.
GetBytes
(
line
)
;
mState
.
Socket
.
BeginSend
(
buffer
,
0
,
buffer
.
Length
,
SocketFlags
.
None
,
new
AsyncCallback
(
OnDataSent
)
,
mState
)
;
}
private
void
Receive
(
)
{
byte
[
]
buffer
=
new
byte
[
4000
]
;
mState
.
Socket
.
BeginReceive
(
buffer
,
0
,
buffer
.
Length
,
SocketFlags
.
None
,
new
AsyncCallback
(
OnDataReceived
)
,
buffer
)
;
}
public
void
Disconnect
(
)
{
if
(
!
mState
.
Socket
.
Connected
)
return
;
mState
.
Socket
.
BeginDisconnect
(
false
,
new
AsyncCallback
(
OnDisconnect
)
,
mState
)
;
}
// __ Callbacks _______________________________________________________
private
void
InvokeEvent
(
Delegate
d
,
params
object
[
]
args
)
{
if
(
mUiControl
!=
null
)
{
mUiControl
.
Invoke
(
d
,
args
)
;
}
else
{
d
.
DynamicInvoke
(
args
)
;
}
}
private
void
OnConnect
(
IAsyncResult
ar
)
{
try
{
//mState = (ConnectionState)ar.AsyncState;
mState
.
Socket
.
EndConnect
(
ar
)
;
// Start receive call to read welcome message. Is this full duplex capable?
Receive
(
)
;
InvokeEvent
(
Connected
,
mState
)
;
}
catch
(
Exception
ex
)
{
NotifyError
(
ex
)
;
}
}
private
void
OnDisconnect
(
IAsyncResult
ar
)
{
try
{
mState
.
Socket
.
EndDisconnect
(
ar
)
;
InvokeEvent
(
Disconnected
,
mState
)
;
}
catch
(
Exception
ex
)
{
NotifyError
(
ex
)
;
}
}
private
void
OnDataSent
(
IAsyncResult
ar
)
{
try
{
int
sentBytes
=
mState
.
Socket
.
EndSend
(
ar
)
;
InvokeEvent
(
DataSent
,
mState
)
;
}
catch
(
Exception
ex
)
{
NotifyError
(
ex
)
;
}
}
private
void
OnDataReceived
(
IAsyncResult
ar
)
{
try
{
int
numbytes
=
mState
.
Socket
.
EndReceive
(
ar
)
;
byte
[
]
buffer
=
(
byte
[
]
)
ar
.
AsyncState
;
string
s
=
Encoding
.
UTF8
.
GetString
(
buffer
,
0
,
numbytes
)
;
InvokeEvent
(
DataReceived
,
s
)
;
// When disconnecting, a running receive call is finished.
// If the socket is then disconnected, we don't want to
// receive again, because it starts spinning with 0 bytes
// received.
if
(
!
mState
.
Socket
.
Connected
)
return
;
// Keep receiving
Receive
(
)
;
}
catch
(
Exception
ex
)
{
NotifyError
(
ex
)
;
}
}
private
void
NotifyError
(
Exception
ex
)
{
mState
.
LastError
=
ex
;
InvokeEvent
(
ErrorRaised
,
mState
)
;
}
}
public
class
ConnectionState
{
public
string
HostName
;
public
int
Port
;
public
Socket
Socket
;
public
Exception
LastError
;
public
ConnectionState
(
string
hostName
,
int
port
)
{
HostName
=
hostName
;
Port
=
port
;
}
}
public
delegate
void
CncDeviceDelegate
(
ConnectionState
state
)
;
public
delegate
void
CncDeviceResponseDelegate
(
string
response
)
;
}
Back
|
FazBrowse Home
|
New Git URL