using System;
|
using System.IO.Ports;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Windows.Forms;
|
|
namespace TProduct.Link.Debugger
|
{
|
public partial class DebugMsg_KEDI_SPEED_Ctrl : UserControl, ILinkDebugger
|
{
|
public SerialPort _serialPort = null;
|
public bool isAutoDis = false;//自动显示返回数据
|
|
private bool _isDisp0X = true;//是否是显示16进制
|
|
//
|
public DebugMsg_KEDI_SPEED_Ctrl()
|
{
|
InitializeComponent();
|
}
|
|
private void TestRS485Form_Load(object sender, EventArgs e)
|
{
|
//设置当前电脑有哪个串口可用的Combox控件
|
InitialDispContrl(comboBoxCOM);
|
|
groupBox1.Enabled = false;
|
groupBox3.Enabled = false;
|
|
comboBoxbtl.SelectedIndex = 2;
|
}
|
|
|
|
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;
|
}
|
|
public void DisposePort()
|
{
|
if (_serialPort != null)
|
{
|
if (_serialPort.IsOpen)
|
{
|
//清理事件接收
|
this._serialPort.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(this.OnReceiveMessage);
|
//关闭
|
_serialPort.Close();
|
}
|
}
|
}
|
byte[] commandSend = null;//rs485的发送命令
|
//连接设备(下发命令)
|
private void btnSendQueryMsg_Click(object sender, EventArgs e)
|
{
|
if (_serialPort == null)
|
return;
|
if (!_serialPort.IsOpen)
|
{
|
MessageBox.Show("端口没有打开!");
|
return;
|
}
|
|
//设备地址
|
byte addr10 = 0;
|
try
|
{
|
if (radioButton10.Checked)
|
{
|
addr10 = byte.Parse(textEquipAddress.Text);
|
}
|
else
|
{
|
addr10 = Convert.ToByte(textEquipAddress.Text, 16);
|
}
|
}
|
catch (Exception)
|
{
|
MessageBox.Show("请输入合理的地址");
|
return;
|
}
|
if (addr10 < 0)
|
{
|
MessageBox.Show("地址请输入0-256");
|
return;
|
}
|
|
//
|
TProduct.RS485.Properties.Settings.Default.PortName = comboBoxCOM.Text;
|
TProduct.RS485.Properties.Settings.Default.TestRs485addr = textEquipAddress.Text;
|
TProduct.RS485.Properties.Settings.Default.TestRs485addr10 = radioButton10.Checked;
|
TProduct.RS485.Properties.Settings.Default.Save();
|
|
try
|
{
|
string error_info = null;
|
if (TProduct.CorpConfig.Instance.CorpFlag == Model.eCorpFlag.WH701)
|
{
|
commandSend = TProduct.Link.Kedi.MsgItem_武汉701_转速.BuildQueryMessage(addr10);
|
}
|
else if (TProduct.CorpConfig.Instance.CorpFlag == Model.eCorpFlag.BENLIU )
|
{
|
commandSend = TProduct.Link.Kedi.MsgItem_犇流_转速.BuildQueryMessage(addr10);
|
}
|
else if (TProduct.CorpConfig.Instance.CorpFlag == Model.eCorpFlag.FFCW )
|
{
|
commandSend = TProduct.Link.Kedi.MsgItem_犇流_转速.BuildQueryMessage(addr10);
|
}
|
else
|
{
|
error_info = "unkonw corp error:122";
|
}
|
if (commandSend == null)
|
{
|
MessageBox.Show(error_info);
|
return;
|
}
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show(ex.ToString());
|
return;
|
}
|
|
|
//手动发送
|
OnSendMessageManu();
|
}
|
|
|
|
//手动发消息
|
public void OnSendMessageManu()
|
{
|
if (null == commandSend)
|
return;
|
|
try
|
{
|
//清除
|
_serialPort.DiscardOutBuffer();
|
_serialPort.DiscardInBuffer();
|
|
//发送查询
|
_serialPort.Write(commandSend, 0, commandSend.Length);
|
}
|
catch (Exception ex)
|
{
|
this.rtfTerminal1.AppendText("Error : 发送失败" + ex.Message);
|
return;
|
}
|
|
//输出到主窗口文本控件,显示查询命令
|
if (!isAutoDis)
|
{
|
//查询命令(字符)
|
string strMessage;
|
if (_isDisp0X)
|
strMessage = BitConverter.ToString(commandSend, 0, commandSend.Length);
|
else
|
strMessage = Encoding.ASCII.GetString(commandSend.ToArray());
|
|
this.rtfTerminal1.AppendText(" \n");
|
this.rtfTerminal1.AppendText("----------------------------START------------------------------\n");
|
this.rtfTerminal1.AppendText(string.Format("------查询的消息: {0} ;\n", strMessage));
|
}
|
}
|
|
//接收消息(事件)
|
private void OnReceiveMessage(object sender, SerialDataReceivedEventArgs e)
|
{
|
//等一条完整的命令进入缓冲区
|
Thread.Sleep(200);
|
|
//读取串口中的数据
|
int count = _serialPort.BytesToRead;
|
if (count < 5)
|
{
|
DisplayMessage(string.Format("Error196 : 可能设备没有连接上,得到的数据长度为: {0}; ", count));
|
return;
|
}
|
byte[] byteMessage = new byte[count];
|
try
|
{
|
_serialPort.Read(byteMessage, 0, byteMessage.Length);
|
}
|
catch (Exception ex)
|
{
|
DisplayMessage("Error206 : 数据读取失败 " + ex.Message);
|
return;
|
}
|
|
//显示收到的消息
|
string strMessage;
|
if (_isDisp0X)
|
strMessage = BitConverter.ToString(byteMessage, 0, byteMessage.Length);
|
else
|
strMessage = Encoding.ASCII.GetString(byteMessage.ToArray());
|
|
DisplayMessage(string.Format("######收到的消息: {0};", strMessage));
|
|
if (TProduct.CorpConfig.Instance.CorpFlag == Model.eCorpFlag.WH701 )
|
{
|
var anaRsultInfo = TProduct.Link.Kedi.MsgItem_武汉701_转速.GetReceiveMessageValues(byteMessage);
|
DisplayMessage(string.Format("######消息内容: {0};", anaRsultInfo));
|
}
|
else if (TProduct.CorpConfig.Instance.CorpFlag == Model.eCorpFlag.BENLIU )
|
{
|
var anaRsultInfo = TProduct.Link.Kedi.MsgItem_犇流_转速.GetReceiveMessageValues(byteMessage);
|
DisplayMessage(string.Format("######消息内容: {0};", anaRsultInfo));
|
}
|
else if (TProduct.CorpConfig.Instance.CorpFlag == Model.eCorpFlag.FFCW )
|
{
|
var anaRsultInfo = TProduct.Link.Kedi.MsgItem_犇流_转速.GetReceiveMessageValues(byteMessage);
|
DisplayMessage(string.Format("######消息内容: {0};", anaRsultInfo));
|
}
|
else
|
{
|
//if (!productRS485.CheckPollReceiveMessage(byteMessage))
|
//{
|
// DisplayMessage("Error219 : 检校码不正确");
|
// return;
|
//}
|
|
//具体的值
|
//List<MonitorPointValue> dataArray;
|
//string error_info = null;
|
//int address = productRS485.AnaPollReceiveValue(byteMessage, out dataArray, out error_info);
|
//if (address < 0 || dataArray == null)
|
//{
|
// DisplayMessage("Error228 : 数据读取失败 "); return;
|
//}
|
|
//for (int i = 0; i < dataArray.Count; i++)
|
//{
|
// DisplayMessage(string.Format("######{1}为 ---> {0} ", dataArray[i].Name, dataArray[i].Value));
|
//}
|
}
|
|
|
|
}
|
|
//输出到主窗口文本控件
|
void DisplayMessage(string strMessage)
|
{
|
this.rtfTerminal1.Invoke
|
(
|
/*表示一个委托,该委托可执行托管代码中声明为 void 且不接受任何参数的任何方法。
|
* 在对控件的 Invoke 方法进行调用时或需要一个简单委托又不想自己定义时可以使用该委托。*/
|
new MethodInvoker
|
(
|
delegate
|
{
|
/*匿名方法,这是一种允许程序员将一段完整代码区块当成参数传递的程序代码编写技术,
|
* 通过此种方法可 以直接使用委托来设计事件响应程序以下就是你要在主线程上实现的功能但是有一点要注意,
|
* 这里不适宜处理过多的方法,因为C#消息机制是消息流水线响应机制,
|
* 如果这里在主线程上处理语句的时间过长会导致主UI线程阻塞,
|
* 停止响应或响应不顺畅,这时你的主form界面会延迟或卡死 */
|
|
//具体的值
|
this.rtfTerminal1.AppendText(strMessage);
|
this.rtfTerminal1.AppendText("\n");
|
}
|
)
|
);
|
}
|
|
|
|
|
//打开
|
private void btnOpenCom_Click(object sender, EventArgs e)
|
{
|
try
|
{
|
if (_serialPort == null)
|
{
|
_serialPort = new SerialPort();
|
//使用事件接收
|
//this._serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.OnReceiveMessage);
|
|
}
|
|
if (_serialPort.IsOpen)
|
{
|
MessageBox.Show("串口已打开,请关闭后再打开");
|
return;
|
}
|
//
|
_serialPort.BaudRate = Convert.ToInt32(comboBoxbtl.Text);
|
_serialPort.PortName = comboBoxCOM.Text;
|
_serialPort.DataBits = 8;
|
_serialPort.Parity = Parity.None;
|
_serialPort.StopBits = StopBits.One;
|
_serialPort.ReceivedBytesThreshold = 1;
|
|
//打开COM接口
|
_serialPort.Open();
|
if (!_serialPort.IsOpen)
|
return;
|
|
|
//!设置按钮
|
groupBox1.Enabled = true;
|
groupBox3.Enabled = true;
|
|
_isDisp0X = true;//是否是显示16进制
|
|
|
//使用事件接收
|
this._serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.OnReceiveMessage);
|
|
// watingTime = productRS485.GetMiniReceiveWaitTime() > 0 ? productRS485.GetMiniReceiveWaitTime() : 200;
|
// textWaitingTime.Text = watingTime.ToString();
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show("串口信息提示:" + ex.Message, "信息提示",
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
}
|
}
|
|
//关闭
|
private void btnCloseCom_Click(object sender, EventArgs e)
|
{
|
if (_serialPort.IsOpen)
|
{
|
_serialPort.Close();
|
|
this._serialPort.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(this.OnReceiveMessage);
|
}
|
}
|
|
private void btnClearText_Click(object sender, EventArgs e)
|
{
|
rtfTerminal1.Text = string.Empty;
|
}
|
}
|
}
|