110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.IO.Ports;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace PLCTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void butTh1Open_Click(object sender, EventArgs e)
{
//线程一
new Thread(() =>
{
UdpSend("192.168.1.55", 4567, "UDP线程一发送数据");
//TCPSend("192.168.1.55", 4567, "TCP线程一发送数据");
})
{ IsBackground = true }.Start();
}
private void butTh2Open_Click(object sender, EventArgs e)
{
//线程二
new Thread(() =>
{
UdpSend("192.168.1.55", 4567, "UDP线程二发送数据");
//TCPSend("192.168.1.55", 4567, "TCP线程二发送数据");
})
{ IsBackground = true }.Start();
}
private void butTh3Open_Click(object sender, EventArgs e)
{
//线程三
new Thread(() =>
{
UdpSend("192.168.1.55", 4567, "UDP线程三发送数据");
//TCPSend("192.168.1.55", 4567, "TCP线程三发送数据");
})
{ IsBackground = true }.Start();
}
private void UdpSend(string ipAddress, int port, string content)
{
UdpClient udp = new UdpClient(ipAddress, port);
byte[] data = Encoding.Default.GetBytes(content);
UpdateTextBox(textBox1, udp.Send(data, data.Length) > 0 ? "UDP发送数据成功" : "UDP发送数据失败");
if (udp != null) udp.Close();
}
private void TCPSend(string ipAddress, int port, string content)
{
TcpClient client = null;
try
{
client = new TcpClient(ipAddress, port);
byte[] data = Encoding.Default.GetBytes(content);
if (client.Connected)
{
NetworkStream stream = client.GetStream();
client.ReceiveTimeout = 3000;
stream.Write(data, 0, data.Length);
UpdateTextBox(textBox1, "TCP发送数据成功");
//这里没有做TCP的服务端
}
}
catch (Exception)
{
UpdateTextBox(textBox1, "TCP发送数据失败");
}
finally { if (client != null) client.Close(); }
}
private void UpdateTextBox(TextBox sender, string content)
{
//因为开线程,所以用委托更新界面
new Action(() =>
{
this.Invoke(new Action(() =>
{
sender.AppendText(content + Environment.NewLine);
}));
}).Invoke();
}
}
}
using System;
using System.IO.Ports;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace PLCTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void butTh1Open_Click(object sender, EventArgs e)
{
//线程一
new Thread(() =>
{
UdpSend("192.168.1.55", 4567, "线程一发送数据");
})
{ IsBackground = true }.Start();
}
private void butTh2Open_Click(object sender, EventArgs e)
{
//线程二
new Thread(() =>
{
UdpSend("192.168.1.55", 4567, "线程二发送数据");
})
{ IsBackground = true }.Start();
}
private void butTh3Open_Click(object sender, EventArgs e)
{
//线程三
new Thread(() =>
{
UdpSend("192.168.1.55", 4567, "线程三发送数据");
})
{ IsBackground = true }.Start();
}
private void UdpSend(string ipAddress, int port, string content)
{
UdpClient udp = new UdpClient(ipAddress, port);
byte[] data = Encoding.Default.GetBytes(content);
UpdateTextBox(textBox1, udp.Send(data, data.Length) > 0 ? "发送数据成功" : "发送数据失败");
if (udp != null) udp.Close();
}
private void UpdateTextBox(TextBox sender, string content)
{
//因为开线程,所以用委托更新界面
new Action(() =>
{
this.Invoke(new Action(() =>
{
sender.AppendText(content + Environment.NewLine);
}));
}).Invoke();
}
}
}
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace PLCTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void butTh1Open_Click(object sender, EventArgs e)
{
//线程一
new Thread(() =>
{
ComSend("COM2");
})
{IsBackground = true}.Start();
}
private void butTh2Open_Click(object sender, EventArgs e)
{
//线程二
new Thread(() =>
{
ComSend("COM3");
})
{ IsBackground = true }.Start();
}
private void butTh3Open_Click(object sender, EventArgs e)
{
//线程三
new Thread(() =>
{
ComSend("COM4");
})
{ IsBackground = true }.Start();
}
private void ComSend(string com)
{
System.IO.Ports.SerialPort sp = null;
sp = new System.IO.Ports.SerialPort();
sp.PortName = com;
sp.BaudRate = 9600;
sp.DataBits = 8;
sp.StopBits = StopBits.One;
sp.ReadTimeout = 500;
if (!sp.IsOpen) sp.Open();
if (sp.IsOpen)
{
Byte[] Buf = Encoding.Default.GetBytes(com);
sp.Write(Buf, 0, Buf.Length);
UpdateTextBox(textBox1, "发送数据" + com);
sp.Close();
}
}
private void UpdateTextBox(TextBox sender, string content)
{
//因为开线程,所以用委托更新界面
new Action(() =>
{
this.Invoke(new Action(() =>
{
sender.AppendText(content + Environment.NewLine);
}));
}).Invoke();
}
}
}