001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.ComponentModel;
004.
using
System.Data;
005.
using
System.Drawing;
006.
using
System.Linq;
007.
using
System.Text;
008.
using
System.Windows.Forms;
009.
using
System.Runtime.InteropServices;
010.
using
System.Net;
011.
using
System.Net.Sockets;
012.
using
System.IO;
013.
using
System.Threading;
014.
015.
namespace
Application2
016.
{
017.
public
partial
class
Form1 : Form
018.
{
019.
private
StreamWriter swSender;
020.
private
StreamReader srReceiver;
021.
private
TcpClient tcpServer;
022.
private
delegate
void
UpdateLogCallback(
string
strMessage);
023.
private
delegate
void
CloseConnectionCallback(
string
strReason);
024.
private
Thread thrMessaging;
025.
private
IPAddress ipAddr;
026.
private
bool
Connected;
027.
int
stand = 888;
028.
Boolean swith =
false
;
029.
int
lastValue;
030.
031.
public
class
PortAccess
032.
{
033.
[DllImport(
"inpout32.dll"
, EntryPoint =
"Out32"
)]
034.
public
static
extern
void
Output(
int
adress,
int
value);
035.
}
036.
037.
private
void
setLED(
int
iValue, Label lable)
038.
{
039.
if
(lastValue != iValue) { swith =
false
; }
040.
reset();
041.
if
(swith ==
false
){
042.
lable.BackColor = Color.RoyalBlue;
043.
SendMessage(iValue);
044.
swith =
true
;
045.
}
else
{
046.
lable.BackColor = Color.White ;
047.
SendMessage(0);
048.
swith =
false
;
049.
}
050.
lastValue = iValue;
051.
}
052.
053.
private
void
reset()
054.
{
055.
label1.BackColor = Color.White;
056.
label2.BackColor = Color.White;
057.
label3.BackColor = Color.White;
058.
label4.BackColor = Color.White;
059.
label5.BackColor = Color.White;
060.
label6.BackColor = Color.White;
061.
label7.BackColor = Color.White;
062.
label8.BackColor = Color.White;
063.
}
064.
065.
public
Form1()
066.
{
067.
InitializeComponent();
068.
}
069.
070.
private
void
button10_Click(
object
sender, EventArgs e)
071.
{
072.
PortAccess.Output(stand, 0);
073.
reset();
074.
Environment.Exit(0);
075.
}
076.
077.
private
void
InitializeConnection()
078.
{
079.
ipAddr = IPAddress.Parse(
"192.168.1.11"
);
080.
tcpServer =
new
TcpClient();
081.
tcpServer.Connect(ipAddr, 1986);
082.
083.
Connected =
true
;
084.
085.
swSender =
new
StreamWriter(tcpServer.GetStream());
086.
swSender.WriteLine(
"Start"
);
087.
swSender.Flush();
088.
089.
thrMessaging =
new
Thread(
new
ThreadStart(ReceiveMessages));
090.
thrMessaging.Start();
091.
}
092.
093.
private
void
ReceiveMessages()
094.
{
095.
srReceiver =
new
StreamReader(tcpServer.GetStream());
096.
string
ConResponse = srReceiver.ReadLine();
097.
if
(ConResponse[0] ==
'1'
)
098.
{
099.
100.
}
101.
else
102.
{
103.
string
Reason =
"Not Connected: "
;
104.
Reason += ConResponse.Substring(2, ConResponse.Length - 2);
105.
this
.Invoke(
new
CloseConnectionCallback(
this
.CloseConnection),
new
object
[] { Reason });
106.
return
;
107.
}
108.
while
(Connected)
109.
{
110.
111.
}
112.
}
113.
114.
private
void
CloseConnection(
string
Reason)
115.
{
116.
Connected =
false
;
117.
swSender.Close();
118.
srReceiver.Close();
119.
tcpServer.Close();
120.
}
121.
122.
private
void
SendMessage(
int
msg)
123.
{
124.
swSender.WriteLine(msg);
125.
swSender.Flush();
126.
}
127.
128.
public
void
OnApplicationExit(
object
sender, EventArgs e)
129.
{
130.
reset();
131.
if
(Connected ==
true
)
132.
{
133.
Connected =
false
;
134.
swSender.Close();
135.
srReceiver.Close();
136.
tcpServer.Close();
137.
}
138.
}
139.
140.
private
void
Form1_Load(
object
sender, EventArgs e)
141.
{
142.
InitializeConnection();
143.
}
144.
145.
private
void
Form1_Closed(
object
sender, EventArgs e)
146.
{
147.
Environment.Exit(0);
148.
}
149.
150.
private
void
button9_Click(
object
sender, EventArgs e)
151.
{
152.
SendMessage(0);
153.
reset();
154.
}
155.
156.
private
void
button1_Click(
object
sender, EventArgs e)
157.
{
158.
setLED(1, label1);
159.
}