using System;
|
using System.Collections.Generic;
|
using System.IO.Ports;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Windows.Forms;
|
|
//电气柜MODEBUS的寄存器(科迪)
|
namespace TProduct.Link.Debugger
|
{
|
public partial class DebugMsg_KEDI_江大_Ctrl : UserControl, ILinkDebugger
|
{
|
public DebugMsg_KEDI_江大_Ctrl()
|
{
|
InitializeComponent();
|
spinEdit参数.Visible = label参数.Visible = false;
|
button发送指令.Visible = false;
|
}
|
|
private void MsgBuild科迪测试台_Load(object sender, EventArgs e)
|
{
|
//设置当前电脑有哪个串口可用的Combox控件
|
InitialDispContrl(comboBoxCOM);
|
}
|
public static bool InitialDispContrl(System.Windows.Forms.ComboBox ctrl)
|
{
|
ctrl.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
|
if (ctrl.Items.Count == 0)
|
{
|
MessageBox.Show("没有COM端口");
|
return false;
|
}
|
ctrl.SelectedIndex = 0;
|
return true;
|
}
|
private SerialPort _seriesPort = null;
|
public void DisposePort()
|
{
|
if (_seriesPort != null && _seriesPort.IsOpen)
|
{
|
_seriesPort.Close();
|
}
|
}
|
|
//连接设备(下发命令)
|
private void btnConnPoll_Click(object sender, EventArgs e)
|
{
|
if (comboBoxType.SelectedIndex == 0)
|
{//
|
|
}
|
if (comboBoxType.SelectedIndex == 1)
|
{//无纸数据记录仪
|
|
}
|
if (comboBoxType.SelectedIndex == 2)
|
{//流量转速
|
var commandSend = TProduct.Link.Kedi.MsgItem_犇流_转速.BuildQueryMessage(1);
|
textBoxSend.Text = BitConverter.ToString(commandSend, 0, commandSend.Length);
|
}
|
if (comboBoxType.SelectedIndex == 3)
|
{//出口阀门
|
var commandSend = TProduct.Link.Kedi.MsgItem_力夫_阀门.BuildMessage出口(
|
Convert.ToDouble(spinEdit参数.Value), 8);
|
textBoxSend.Text = BitConverter.ToString(commandSend, 0, commandSend.Length);
|
}
|
if (comboBoxType.SelectedIndex == 4)
|
{// 直流电源(开)
|
|
}
|
if (comboBoxType.SelectedIndex == 5)
|
{// 直流电源(开)
|
|
}
|
if (comboBoxType.SelectedIndex == 6)
|
{//进口阀门
|
var commandSend = TProduct.Link.Kedi.MsgItem_力夫_阀门.BuildMessage进口(
|
Convert.ToDouble(spinEdit参数.Value), 8);
|
textBoxSend.Text = BitConverter.ToString(commandSend, 0, commandSend.Length);
|
}
|
if (comboBoxType.SelectedIndex == 7)
|
{//力夫用电参数仪
|
|
}
|
if (comboBoxType.SelectedIndex == 8)
|
{//力夫温度控制器
|
|
}
|
if (comboBoxType.SelectedIndex == 9)
|
{//力夫PLC控制启
|
|
}
|
if (comboBoxType.SelectedIndex == 10)
|
{//力夫PLC控制停
|
|
}
|
if (comboBoxType.SelectedIndex == 11)
|
{//江大扭矩仪
|
var commandSend = TProduct.Link.Kedi.MsgItem_江大_扭矩仪.GetPollSendMessage(1);//1代表:本从机号X
|
textBoxSend.Text = BitConverter.ToString(commandSend, 0, commandSend.Length);
|
}
|
|
}
|
|
private void comboBoxType_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
if (comboBoxType.SelectedIndex == 0)
|
{//
|
|
}
|
else if (comboBoxType.SelectedIndex == 1)
|
{//无纸数据记录仪
|
spinEdit参数.Text = "8";
|
label参数.Text = "端口总数";
|
label参数.Location = new System.Drawing.Point(256, 12);
|
spinEdit参数.Visible = label参数.Visible = true;
|
}
|
else if (comboBoxType.SelectedIndex == 2)
|
{
|
//spinEdit参数.Text = "1";
|
//label参数.Text = "地址"; label参数.Location = new System.Drawing.Point(268, 12);
|
//spinEdit参数.Visible = label参数.Visible = true;
|
}
|
else if (comboBoxType.SelectedIndex == 3)
|
{//出口阀门
|
label参数.Text = "开度";
|
spinEdit参数.Visible = label参数.Visible = true;
|
}
|
else if (comboBoxType.SelectedIndex == 6)
|
{//进口阀门
|
label参数.Text = "开度";
|
spinEdit参数.Visible = label参数.Visible = true;
|
}
|
else if (comboBoxType.SelectedIndex == 4)
|
{// 直流电源(开)
|
label参数.Text = "电压";
|
spinEdit参数.Visible = label参数.Visible = true;
|
}
|
else
|
{
|
spinEdit参数.Visible = label参数.Visible = false;
|
}
|
}
|
|
private void btnClearText_Click(object sender, EventArgs e)
|
{
|
this.textBoxReceive.Text = "";
|
}
|
|
|
private void buttonAna_Click(object sender, EventArgs e)
|
{
|
if (string.IsNullOrEmpty(this.textBoxReceive.Text))
|
return;
|
|
System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");
|
string error_info = null;
|
var byteMessage = StringToHexValuve(textBoxReceive.Text, out error_info);
|
|
if (comboBoxType.SelectedIndex == 0)
|
{//
|
|
}
|
if (comboBoxType.SelectedIndex == 1)
|
{// 无纸数据记录仪
|
|
double inelt_press_ma; double outlet_press_ma;
|
var error = new TProduct.Link.Kedi.SiMsg压力().AbstactByteValue(byteMessage.ToArray(),
|
out inelt_press_ma, out outlet_press_ma);
|
if (!string.IsNullOrEmpty(error))
|
{
|
MessageBox.Show(error);
|
return;
|
}
|
StringBuilder sb = new StringBuilder();
|
sb.AppendFormat("进口压力:{0} ma", inelt_press_ma); sb.AppendLine();
|
sb.AppendFormat("出口压力:{0} ma", outlet_press_ma); sb.AppendLine();
|
|
textBoxResult.Text = sb.ToString();
|
|
}
|
if (comboBoxType.SelectedIndex == 2)
|
{//转速
|
var info = TProduct.Link.Kedi.MsgItem_犇流_转速.GetReceiveMessageValues(byteMessage.ToArray() );
|
textBoxResult.Text = info;
|
}
|
}
|
|
|
|
/// <summary>
|
/// 字符串转16进制字符2
|
/// </summary>
|
/// <param name="content">字符串</param>
|
/// <param name="encode">编码格式</param>
|
/// <returns></returns>
|
private List<byte> StringToHexValuve(string content, out string error_info)
|
{
|
//去掉空格
|
string[] arr = content.Split(' ');
|
if (arr.Length < 3)
|
{
|
arr = content.Split('-');
|
if (arr.Length < 0)
|
{
|
error_info = "字符无法解析";
|
return null;
|
}
|
}
|
List<byte> result = new List<byte>();
|
for (int i = 0; i < arr.Length; i++)
|
{
|
if (string.IsNullOrWhiteSpace(arr[i]))
|
continue;
|
|
var dddd = Convert.ToByte(arr[i], 16);
|
result.Add(dddd);
|
}
|
error_info = null;
|
return result;
|
}
|
|
private void btnOpenCom_Click(object sender, EventArgs e)
|
{
|
try
|
{
|
if (_seriesPort != null && _seriesPort.IsOpen)
|
{
|
MessageBox.Show("串口已打开,请关闭后再打开");
|
return;
|
}
|
//
|
//com.BaudRate = 115200;
|
if (_seriesPort == null)
|
{
|
_seriesPort = new SerialPort();
|
_seriesPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.OnReceiveMessage);
|
|
}
|
|
_seriesPort.BaudRate = 9600;// Convert.ToInt32(comboBoxbtl.Text);
|
_seriesPort.PortName = comboBoxCOM.Text;
|
_seriesPort.DataBits = 8;
|
_seriesPort.Parity = Parity.None;
|
_seriesPort.StopBits = StopBits.One;
|
_seriesPort.ReceivedBytesThreshold = 1;
|
if (comboBoxType.SelectedIndex == 9)
|
{//力夫PLC控制启
|
_seriesPort.DataBits = 7;
|
_seriesPort.Parity = Parity.Even;
|
}
|
if (comboBoxType.SelectedIndex == 10)
|
{//力夫PLC控制停
|
_seriesPort.DataBits = 7;
|
_seriesPort.Parity = Parity.Even;
|
}
|
//打开COM接口
|
_seriesPort.Open();
|
|
//
|
Properties.Settings.Default.PortName = comboBoxCOM.Text;
|
|
//!设置按钮
|
button发送指令.Visible = true;
|
|
|
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show("串口信息提示:" + ex.Message, "信息提示",
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
}
|
}
|
|
private void button发送指令_Click(object sender, EventArgs e)
|
{
|
if (string.IsNullOrEmpty(textBoxSend.Text) || textBoxSend.Text.Length < 1)
|
return;
|
textBoxResult.Text = "";
|
System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");
|
string error_info = null;
|
var byteMessage = StringToHexValuve(textBoxSend.Text, out error_info);
|
if (byteMessage == null)
|
return;
|
|
if (_seriesPort == null)
|
return;
|
try
|
{
|
//清除
|
_seriesPort.DiscardOutBuffer();
|
_seriesPort.DiscardInBuffer();
|
|
//发送查询
|
_seriesPort.Write(byteMessage.ToArray(), 0, byteMessage.Count);
|
}
|
catch (Exception err)
|
{
|
MessageBox.Show("Error in write event: " + err.Message);
|
return;
|
}
|
}
|
|
bool isDisp0X = true;
|
//接收消息(事件)
|
private void OnReceiveMessage(object sender, SerialDataReceivedEventArgs e)
|
{
|
if (_seriesPort == null || !_seriesPort.IsOpen)
|
return;
|
|
//SerialPort sp = sender as SerialPort;
|
Thread.Sleep(100);//暂停100毫秒等一条完整的命令进入缓冲区
|
|
//读取串口中的数据
|
int count = _seriesPort.BytesToRead;
|
if (count < 1)
|
{
|
string strError = string.Format("收到信号的数据量过少,字节长度只有{0}位\n ", count);
|
this.textBoxReceive.Invoke(new EventHandler(delegate { textBoxReceive.AppendText(strError); }));
|
//this.rtfTerminal1.AppendText(strError);
|
return;
|
}
|
|
try
|
{
|
//读取信号
|
byte[] byteMessage = new byte[count];
|
_seriesPort.Read(byteMessage, 0, byteMessage.Length);
|
|
string strMessage;
|
if (isDisp0X)
|
strMessage = BitConverter.ToString(byteMessage, 0, byteMessage.Length);// byte[] -> 16进制字符串
|
else
|
strMessage = Encoding.ASCII.GetString(byteMessage.ToArray());
|
|
this.textBoxReceive.Invoke(new MethodInvoker(
|
delegate
|
{
|
this.textBoxReceive.AppendText(strMessage + "\n");
|
}));
|
}
|
catch (Exception err)
|
{
|
string strError = "Error in read event: " + err.Message + "\n";
|
this.textBoxReceive.Invoke(new EventHandler(delegate { textBoxReceive.AppendText(strError); }));
|
return;
|
}
|
}
|
|
|
}
|
}
|