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.
370 lines
12 KiB
370 lines
12 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using DevExpress.XtraEditors;
|
|
using System.Data.SqlClient;
|
|
using ICSSoft.Frame.APP;
|
|
using System.Configuration;
|
|
using ICSSoft.Frame.APP.Helper;
|
|
using ICSSoft.Frame.APP.Model;
|
|
using DevExpress.XtraGrid.Columns;
|
|
using System.Linq;
|
|
using System.IO.Ports;
|
|
|
|
namespace ICSSoft.Frame.APP
|
|
{
|
|
public partial class FormCom : DevExpress.XtraEditors.XtraForm, ComResult
|
|
{
|
|
List<SelectItemExt> ItemList = new List<SelectItemExt>();
|
|
string[] PostNames;
|
|
SerialPort port;
|
|
public string result { get; set; }//返回值
|
|
List<Color> color = new List<Color>()
|
|
{
|
|
Color.BlueViolet,
|
|
Color.Orange,
|
|
Color.Black,
|
|
Color.Blue,
|
|
Color.YellowGreen
|
|
};
|
|
public FormCom()
|
|
{
|
|
InitializeComponent();
|
|
|
|
this.txtXonXoff.SelectedIndex = 0;
|
|
this.txtAddress.SelectedIndex = 0;
|
|
this.txtStop.SelectedIndex = 0;
|
|
this.txtDataBits.SelectedIndex = 0;
|
|
this.txtParitv.SelectedIndex = 0;
|
|
}
|
|
|
|
private void btnOK_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
|
|
this.DialogResult = DialogResult.OK;
|
|
this.result = txtResult.Text;
|
|
this.Close();
|
|
this.port.Dispose();
|
|
}
|
|
|
|
private void FormEcAdd_Load(object sender, EventArgs e)
|
|
{
|
|
SetRichTextboxContent(RTBox_Log1, "开始获取可用串口信息 ", true);
|
|
PostNames = SerialPort.GetPortNames();
|
|
if (PostNames.Count() <= 0)
|
|
SetRichTextboxContent(RTBox_Log1, "暂无可用串口!", false);
|
|
else
|
|
{
|
|
SetRichTextboxContent(RTBox_Log1, "获取可用串口信息成功", true);
|
|
if (this.txtCom.Properties.Items.Count > 0)
|
|
this.txtCom.Properties.Items.Clear();
|
|
|
|
this.txtCom.Properties.Items.AddRange(PostNames);
|
|
this.txtCom.SelectedIndex = 0;
|
|
}
|
|
|
|
btnOpenOrClose.TextChanged += (object senders, EventArgs es) =>
|
|
{
|
|
btnOpenOrClose.BackColor = Color.Green;
|
|
};
|
|
}
|
|
|
|
private void btnBigenOrSuspend_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
CheckPort();
|
|
|
|
if (port != null && port.IsOpen)//关闭串口
|
|
{
|
|
port.Close();
|
|
port = null;
|
|
SetRichTextboxContent(RTBox_Log1,"关闭成功", true);
|
|
btnOpenOrClose.Text = "打开串口";
|
|
}
|
|
|
|
else if (port == null)
|
|
{
|
|
port = new SerialPort();
|
|
port.BaudRate =Convert.ToInt32(txtBaudRate.EditValue);
|
|
port.PortName = txtCom.EditValue.ToString();
|
|
if (txtDataBits.EditValue != null)
|
|
port.DataBits = Convert.ToInt32(txtDataBits.EditValue);
|
|
if (txtParitv.EditValue != null)
|
|
{
|
|
switch(txtParitv.EditValue.ToString())
|
|
{
|
|
case "None":
|
|
port.Parity = Parity.None;
|
|
break;
|
|
case "Even":
|
|
port.Parity = Parity.Even;
|
|
break;
|
|
case "Mark":
|
|
port.Parity = Parity.Mark;
|
|
break;
|
|
case "Odd":
|
|
port.Parity = Parity.Odd;
|
|
break;
|
|
case "Space":
|
|
port.Parity = Parity.Space;
|
|
break;
|
|
}
|
|
}
|
|
if (txtStop.EditValue != null)
|
|
{
|
|
|
|
switch (txtParitv.EditValue.ToString())
|
|
{
|
|
case "0":
|
|
port.StopBits = StopBits.None;
|
|
break;
|
|
case "1":
|
|
port.StopBits = StopBits.One;
|
|
break;
|
|
case "2":
|
|
port.StopBits = StopBits.Two;
|
|
break;
|
|
case "1.5":
|
|
port.StopBits = StopBits.OnePointFive;
|
|
break;
|
|
}
|
|
|
|
}
|
|
if (txtXonXoff.EditValue.ToString() == "On")
|
|
port.Handshake = Handshake.XOnXOff;
|
|
else
|
|
port.Handshake = Handshake.None;
|
|
|
|
|
|
port.DataReceived += (object senders, SerialDataReceivedEventArgs es) =>
|
|
{
|
|
|
|
var returns = port.ReadLine();
|
|
|
|
SetRichTextboxContent(RTBox_Log, "接收:"+returns, true);
|
|
|
|
|
|
if(IsHexFormat(returns.Replace(" ","").Replace("\r","").Replace("\n","")))
|
|
{
|
|
returns = HexToString(returns.Replace("\r", "").Replace("\n", ""));//如果是16进制数据 转为字符串
|
|
|
|
}
|
|
if (returns.StartsWith("ST")||returns.StartsWith("US")|| returns.StartsWith("OV"))
|
|
{
|
|
|
|
|
|
this.txtResult.Invoke(new Action(() =>
|
|
{
|
|
var value= returns.Substring(7, 10).Replace(" ", "");
|
|
if(!value.ToUpper().EndsWith("KG")&& !value.ToUpper().EndsWith("G"))
|
|
SetRichTextboxContent(RTBox_Log1, "返回重量格式不正确,请调试致KG/G再称重!当前值:"+value+"", true);
|
|
|
|
this.txtResult.Text = value;
|
|
}));
|
|
|
|
}
|
|
};
|
|
port.Open();
|
|
|
|
|
|
SetRichTextboxContent(RTBox_Log1, "打开成功", true);
|
|
btnOpenOrClose.Text = "关闭串口";
|
|
|
|
foreach (var control in groupBox1.Controls)
|
|
{
|
|
if (control is ComboBoxEdit)
|
|
((ComboBoxEdit)control).Properties.ReadOnly = true;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetRichTextboxContent(RTBox_Log1, ex.Message, false);
|
|
if (port.IsOpen)
|
|
port.Close();
|
|
port = null;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
private void CheckPort()
|
|
{
|
|
try
|
|
{
|
|
if (txtCom.EditValue == null)
|
|
throw new Exception("请先选择通讯口");
|
|
if (txtBaudRate.EditValue == null)
|
|
throw new Exception("请先选择波特率");
|
|
if (txtDataBits.EditValue == null)
|
|
throw new Exception("请先选择数据位");
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
throw ex;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
private void btnEditDel_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
ICSBaseSimpleCode.AppshowMessageBox(ex.Message);
|
|
}
|
|
}
|
|
|
|
private void btnCancle_Click(object sender, EventArgs e)
|
|
{
|
|
this.DialogResult = DialogResult.Cancel;
|
|
this.Close();
|
|
}
|
|
|
|
private void btnClose_Click(object sender, EventArgs e)
|
|
{
|
|
if (this.port != null)
|
|
this.port.Dispose();
|
|
|
|
this.DialogResult = DialogResult.Cancel;
|
|
this.Close();
|
|
}
|
|
|
|
private void groupBox1_Enter(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// RichTextBox记录
|
|
/// </summary>
|
|
/// <param name="rtb">RichTextBox</param>
|
|
/// <param name="txt1">基本信息</param>
|
|
/// <param name="ok">Pass Or Fail</param>
|
|
/// <param name="keys">特殊颜色所标识的关键字列表</param>
|
|
private void SetRichTextboxContent(RichTextBox rtb, string txt1, bool ok = true, params string[] keys)
|
|
{
|
|
if (rtb.InvokeRequired)
|
|
{
|
|
rtb.Invoke(new Action(() => { SetRichTextboxContent(rtb, txt1, ok, keys); }));
|
|
}
|
|
else
|
|
{
|
|
if (rtb.Lines.Length > 200)
|
|
{
|
|
rtb.Clear();
|
|
}
|
|
rtb.SelectionColor = Color.Black;
|
|
rtb.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff "));
|
|
rtb.SelectionColor = ok ? Color.LimeGreen : Color.Red;
|
|
rtb.AppendText(txt1);
|
|
for (int i = 0; i < keys.Length; i++)
|
|
{
|
|
rtb.SelectionColor = color[i];
|
|
rtb.AppendText(" " + keys[i]);
|
|
}
|
|
rtb.AppendText("\r\n");
|
|
rtb.SelectionStart = rtb.Text.Length;
|
|
rtb.ScrollToCaret();
|
|
}
|
|
}
|
|
|
|
public void SendCommand(string command)
|
|
{
|
|
try
|
|
{
|
|
if (port.IsOpen)
|
|
{
|
|
port.WriteLine(command); // 发送指令
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("串口未打开.");
|
|
}
|
|
SetRichTextboxContent(RTBox_Log, "发送:"+command , false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetRichTextboxContent(RTBox_Log1, "发送指令时出错: " + ex.Message, false);
|
|
}
|
|
}
|
|
|
|
|
|
string StringToHex(string input)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (char c in input)
|
|
{
|
|
sb.Append(Convert.ToInt32(c).ToString("X2")).Append(" ");
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
string HexToString(string hex)
|
|
{
|
|
string[] hexValuesSplit = hex.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (string hexValue in hexValuesSplit)
|
|
{
|
|
sb.Append((char)Convert.ToInt32(hexValue, 16));
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
private void groupBox2_Enter(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
// 判断字符串是否是十六进制格式
|
|
static bool IsHexFormat(string data)
|
|
{
|
|
foreach (char c in data)
|
|
{
|
|
if (!Uri.IsHexDigit(c))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
private void Common_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
try
|
|
{
|
|
if ((sender as Control).Tag == null)
|
|
return;
|
|
if (port == null || !port.IsOpen)
|
|
return;
|
|
|
|
SendCommand((sender as Control).Tag.ToString()+"\r\n");//发送指令
|
|
|
|
}
|
|
catch ( Exception ex)
|
|
{
|
|
|
|
SetRichTextboxContent(RTBox_Log1, ex.Message, false);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}
|