目录
一 设计原型
二 后台代码
一 设计原型
模拟工具:
二 后台代码
主程序:
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace 扫码称重上位机
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SerialPort serialPort = new SerialPort();
TcpClient tcpClient = new TcpClient();
bool IsRun = false;
NetworkStream GetStream = null;
Datas datas = new Datas();
bool isCode = false;
bool isWeight = false;
private void 参数设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
Parameter parameter = new Parameter();
parameter.ShowDialog();
}
private void 运行ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (!serialPort.IsOpen)
{
serialPort.Open();
}
if (!tcpClient.Connected)
{
tcpClient.ConnectAsync(IPAddress.Parse("127.0.0.1"), 8899);
GetStream = tcpClient.GetStream();
}
IsRun = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void 停止ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (serialPort.IsOpen)
{
serialPort.Close();
}
//if (tcpClient.Connected)
//{
// tcpClient.Close();
//}
//if (GetStream != null)
//{
// GetStream.Close();
//}
IsRun = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (IsRun)
{
MessageBox.Show("设备正在运行,请停止后在退出系统");
return;
}
this.Close();
System.Environment.Exit(0);
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
serialPort.PortName = "COM1";
serialPort.Parity = Parity.None;
serialPort.BaudRate = 115200;
serialPort.StopBits = StopBits.One;
serialPort.DataBits = 8;
serialPort.DataReceived += SerialPort_DataReceived;
}
catch (Exception)
{
}
Task.Run(() =>
{
while (true)
{
if (IsRun)
{
try
{
if (GetStream != null)
{
byte[] bytes = new byte[1024];
GetStream.Read(bytes, 0, bytes.Length);
datas.weight = Encoding.UTF8.GetString(bytes);
isWeight = true;
}
if (isCode && isWeight)
{
datas.Result = "OK";
datas.DateTime = DateTime.Now.ToShortDateString();
datas.Note = "测试数据";
this.BeginInvoke(() =>
{
dataGridView1.Rows.Add(datas.Code, datas.weight, datas.Result, datas.DateTime, datas.Note);
isCode = false;
isWeight = false;
});
}
}
catch (Exception)
{
}
}
}
});
}
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] bytes = new byte[1024];
serialPort.Read(bytes, 0, bytes.Length);
datas.Code = Encoding.UTF8.GetString(bytes);
isCode = true;
}
}
public class Datas
{
public string Code { get; set; } = "";
public string weight { get; set; } = "";
public string Result { get; set; } = "";
public string DateTime { get; set; } = "";
public string Note { get; set; } = "";
}
}
参数页面程序:
using System.IO.Ports;
using System.Text.Json;
namespace 扫码称重上位机
{
public partial class Parameter : Form
{
public Parameter()
{
InitializeComponent();
}
string path = Directory.GetCurrentDirectory() + "\\PM.json";
private void Parameter_Load(object sender, EventArgs e)
{
try
{
port.Items.AddRange(SerialPort.GetPortNames());
object[] parityList = { Parity.None, Parity.Odd };
ck.Items.AddRange(parityList);
object[] arrs = { 115200, 9600 };
bau.Items.AddRange(arrs);
object[] stp = { StopBits.One, StopBits.None };
stop.Items.AddRange(stp);
object[] ds = { 6, 7, 8 };
data.Items.AddRange(ds);
ip.Items.Add("127.0.0.1");
tcpport.Items.Add(9999);
ParameterClass parameter = new ParameterClass();
string str = File.ReadAllText(path);
parameter = JsonSerializer.Deserialize<ParameterClass>(str);
if (parameter != null)
{
port.SelectedItem = parameter.Com;
ck.SelectedItem = parameter.Parity;
bau.SelectedItem = parameter.BaudRate;
stop.SelectedItem = parameter.StopBits;
data.SelectedItem = parameter.DataBits;
ip.SelectedItem = parameter.IP;
tcpport.SelectedItem = parameter.TCPPort;
}
}
catch (Exception)
{
}
}
private void SAVE_Click(object sender, EventArgs e)
{
try
{
ParameterClass parameter = new ParameterClass();
parameter.Com = port.Text;
parameter.Parity = (Parity)ck.SelectedItem;
parameter.BaudRate = int.Parse(bau.Text);
parameter.StopBits = (StopBits)stop.SelectedItem;
parameter.DataBits = int.Parse(data.Text);
parameter.IP = ip.Text;
parameter.TCPPort = int.Parse(tcpport.Text);
File.WriteAllText(path, JsonSerializer.Serialize(parameter));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public class ParameterClass
{
public string Com { get; set; }
public Parity Parity { get; set; }
public int BaudRate { get; set; }
public StopBits StopBits { get; set; }
public int DataBits { get; set; }
public string IP { get; set; }
public int TCPPort { get; set; }
}
}