天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

233 lines
8.3 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QMAPP.Entity;
namespace QMAPP.WinForm.Forms.DeviceInterface
{
public partial class LeakInspection : Form
{
public FJC.Entity.Basic.MachineInfo Machine;
//获取服务代理
QMAPP.ServicesAgent.ServiceAgent _agent = ClientContext.GetServiceAgent();
public LeakInspection()
{
InitializeComponent();
}
private void LeakInspection_Load(object sender, EventArgs e)
{
cbPortName.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
cbParity.DataSource = Enum.GetValues(typeof(System.IO.Ports.Parity));
cbStopBit.DataSource = Enum.GetValues(typeof(System.IO.Ports.StopBits));
cbParity.SelectedIndex = 0;
cbStopBit.SelectedIndex = 1;
cbDataBits.SelectedIndex = 3;
cbBaudRate.SelectedIndex = 6;
btnStop.Enabled = false;
if (Machine != null)
{
if (!string.IsNullOrWhiteSpace(Machine.TERMINALPORT))
{
cbPortName.Text=Machine.TERMINALPORT;
btnStartListening_Click(this, e);
}
}
}
private void btnStartListening_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(cbPortName.Text))
{
MessageBox.Show("请选择端口!", "", MessageBoxButtons.OK, MessageBoxIcon.Question);
return;
}
try
{
serialPort1.BaudRate = int.Parse(cbBaudRate.Text);
serialPort1.DataBits = int.Parse(cbDataBits.Text);
serialPort1.DtrEnable = cbDTR.Checked;
serialPort1.Parity = (System.IO.Ports.Parity)cbParity.SelectedValue;
serialPort1.PortName = cbPortName.Text;
serialPort1.RtsEnable = cbRTS.Checked;
serialPort1.StopBits = (System.IO.Ports.StopBits)cbStopBit.SelectedValue;
serialPort1.Open();
cbPortName.Enabled = false;
cbParity.Enabled = false;
cbStopBit.Enabled = false;
cbParity.Enabled = false;
cbStopBit.Enabled = false;
cbDataBits.Enabled = false;
cbBaudRate.Enabled = false;
cbDTR.Enabled = false;
cbRTS.Enabled = false;
btnStop.Enabled = true;
btnStartListening.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
List<byte> receveBuffer = new List<byte>();
byte[] _msgSuccesed = new byte[4] { 0xCC, 0x04, 0x01, 0xC9 };
byte[] _msgFaild = new byte[4] { 0xCC, 0x04, 0x00, 0xC8 };
DateTime _headtime;
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
//if (serialPort1.BytesToRead > 0)
//{
// byte[] buffer=new byte[serialPort1.BytesToRead];
// serialPort1.Read(buffer, 0, buffer.Length);
// ShowInListbox(string.Join(" ", buffer.Select(p => p.ToString("X2"))));
//}
while (serialPort1.BytesToRead > 0)
{
//if (_IsClearInputBuffer)
//{
// _IsClearInputBuffer = false;
// receveBuffer.Clear();
//}
try
{
byte[] buffer = new byte[serialPort1.BytesToRead];
serialPort1.Read(buffer, 0, buffer.Length);
ShowInListbox(string.Join(" ", buffer.Select(p => p.ToString("X2"))));
receveBuffer.AddRange(buffer);
while (receveBuffer.Count >= 2)
{
if (receveBuffer[0] == 0xAA)//如果接收到消息头
{
if (receveBuffer.Count >= receveBuffer[1])//判断接收消息长度
{
var receive = receveBuffer.GetRange(0, receveBuffer[1]).ToArray();
if (CalculateChecksum(receive) == 0) //计算校验位
{
serialPort1.Write(_msgSuccesed, 0, _msgSuccesed.Length);//向设备返回成功消息
ShowInListbox("数据格式正确,接收成功!");
AnalyzeData(receive);
}
else
{
serialPort1.Write(_msgFaild, 0, _msgFaild.Length);//向设备返回失败消息
ShowInListbox("数据格式错误,接收失败!");
}
receveBuffer.RemoveRange(0, receive.Length);//将已处理的数据从缓存中移除
}
else
{
break;
}
}
else
{
receveBuffer.RemoveAt(0);
}
}
}
catch
{
}
}
}
private void AnalyzeData(byte[] receive)
{
string result = receive[2] > 0 ? "合格" : "不合格";
string barcode = System.Text.Encoding.ASCII.GetString(receive, 3, receive.Length - 4);
string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
FJC.Entity.InterfaceData.LeakInspectRecord record = new FJC.Entity.InterfaceData.LeakInspectRecord
{
CREATEDATE = DateTime.Now,
CREATEUSER = QMAPP.WinForm.ClientContext.LoginInfo.UserID,
PRODUCTCODE = barcode,
RESULT = receive[2] + "",
PID = Guid.NewGuid().ToString()
};
var ret=_agent.InvokeServiceFunction<DataResult<int>>("LeakInspectRecordBLL_Insert", record);
this.Invoke((Action)(() =>
{
lvCheckResults.Items.Insert(0, new ListViewItem(new string[] { barcode, result, date,ret.Result>0?"同步成功":"同步失败" }));
if (lvCheckResults.Items.Count > 50)
{
lvCheckResults.Items.RemoveAt(50);
}
}));
}
private void ShowInListbox(string text)
{
this.Invoke((Action)(() =>
{
lbReceivedData.Items.Insert(0, text);
if (lbReceivedData.Items.Count > 50)
{
lbReceivedData.Items.RemoveAt(50);
}
}));
}
private void LeakInspection_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
}
private void btnStop_Click(object sender, EventArgs e)
{
serialPort1.Close();
cbPortName.Enabled = true;
cbParity.Enabled = true;
cbStopBit.Enabled = true;
cbParity.Enabled = true;
cbStopBit.Enabled = true;
cbDataBits.Enabled = true;
cbBaudRate.Enabled = true;
cbDTR.Enabled = true;
cbRTS.Enabled = true;
btnStop.Enabled = false;
btnStartListening.Enabled = true;
}
public static byte CalculateChecksum(byte[] data)
{
byte checksum = 0;
if (data != null)
{
foreach (byte b in data)
{
checksum = (byte)(checksum ^ b);
}
}
//byte checksum = (byte)(CHECKSUM_BASE ^ LEN);
//checksum = (byte)(checksum ^ CHECKSUM_BASE);
return checksum;
}
}
}