基本信息
源码名称:串口通讯:化验数据采集[测硫仪]
源码大小:6.96M
文件格式:.zip
开发语言:C#
更新时间:2021-12-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Text.RegularExpressions;
using DataParse;
//using System.Windows.Forms;
namespace SendData
{
/// <summary>
///
/// </summary>
public partial class uSerPort : Label
{
private SerialPort[] serPorts;
public delegate void GetComData(string sValue);
public GetComData getData;
private bool[] isOpens;
public static string _weightZL = "";
[DescriptionAttribute("通讯端口号")]
/// <summary>
/// 串口号
/// </summary>
public string[] Ports;
[DescriptionAttribute("通讯波特率")]
public int[] iBits;
[DescriptionAttribute("数据长度")]
public int[] iLengths;
[DescriptionAttribute("数据较验:0,1,2")]
//0为None,1为寄校验,2为偶较验
public int[] iParitys;
[DescriptionAttribute("数据停止位:0,1,2")]
public string[] iStops;
[DescriptionAttribute("过滤的字符")]
public string[] filters;
[DescriptionAttribute("是否连续发送")]
public string CanSpace;
[DescriptionAttribute("台称个数")]
public int iNum=1;
[DescriptionAttribute("数据解析方法")]
public string dataParse = "";
[DescriptionAttribute("设备ID")]
public string deviceId = "";
public uSerPort()
{
InitializeComponent();
//this.TextAlign = ContentAlignment.MiddleCenter;
//this.serPort.ReadBufferSize = 1024;
}
/// <summary>
/// 串口打开
/// </summary>
/// <returns></returns>
public bool[] OpenPort()
{
isOpens = new bool[iNum];
try
{
serPorts = new SerialPort[iNum];
for (int i = 0; i < iNum; i )
{
isOpens[i] = false;
serPorts[i] = new SerialPort();
serPorts[i].ReceivedBytesThreshold = 1;
serPorts[i].BaudRate = iBits[i];
serPorts[i].DataBits = iLengths[i];
serPorts[i].DtrEnable = true;
serPorts[i].PortName = Ports[i];
int iStop = 0;
if (iStops[i] == "1.5")
iStop = 15;
else
{
try
{
iStop = Convert.ToInt16(iStops[i]);
}
catch { }
}
switch (iParitys[i])
{
default:
case 0:
serPorts[i].Parity = Parity.None;
break;
case 1:
serPorts[i].Parity = Parity.Odd;
break;
case 2:
serPorts[i].Parity = Parity.Even;
break;
}
switch (iStop)
{
default:
serPorts[i].StopBits = StopBits.One;
break;
case 2:
serPorts[i].StopBits = StopBits.Two;
break;
case 15:
serPorts[i].StopBits = StopBits.OnePointFive;
break;
}
if (serPorts[i].IsOpen)
serPorts[i].Close();
serPorts[i].Open();
isOpens[i] = true;
if (i == 0)
{
serPorts[i].DataReceived = new SerialDataReceivedEventHandler(serPort_DataReceived);
}
else
{
serPorts[i].DataReceived = new SerialDataReceivedEventHandler(serPort_DataReceived2);
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return isOpens;
}
/// <summary>
/// 串口关闭
/// </summary>
/// <returns></returns>
public bool ClosePort()
{
bool flag = false;
isOpens = new bool[iNum];
try
{
for (int i=0;i<iNum;i )
{
if (serPorts[i].IsOpen)
{
if(i==0)
serPorts[i].DataReceived -= new SerialDataReceivedEventHandler(serPort_DataReceived);
else
serPorts[i].DataReceived -= new SerialDataReceivedEventHandler(serPort_DataReceived2);
serPorts[i].Close();
//serPort.Dispose();
}
flag = true;
isOpens[i] = false;
}
}
catch (Exception ex)
{
flag = false;
throw new Exception(ex.Message);
}
return flag;
}
/// <summary>
/// 获取端口打开状态
/// </summary>
/// <returns></returns>
public bool[] GetPortState()
{
bool[] isOp = new bool[iNum];
for (int i = 0; i < iNum;i )
{
isOp[i] = serPorts[i].IsOpen;
}
return isOp;
}
private void serPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort serPort = sender as SerialPort;
getWeight(serPort, filters[0]);
}
private void serPort_DataReceived2(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort serPort = sender as SerialPort;
getWeight(serPort, filters[1]);
}
private void getWeight(SerialPort serPort, string Filter)
{
string str = TransWeight.getWeight(serPort, Filter);
getData(str);
//this.Dispatcher.Invoke(getData, new string[] { str });
//this.Invoke(getData, new string[] { str });
_weightZL = str;
//SerialPort serPort = sender as SerialPort;
//try
//{
// if (serPort.IsOpen)
// {
// string extStr = "";
// string str = "";
// if (Filter != "0")
// {
// System.Threading.Thread.Sleep(200);
// extStr = serPort.ReadExisting();
// loger.Info(string.Format("ReadExisting:{0}", extStr));
// if (extStr.IndexOf("\r") > 0)//带换行
// {
// str = extStr.Replace("\r", "");
// if (str.IndexOf("Net") >= 0)
// {
// str = str.Substring(str.IndexOf("Net") 3);
// }
// else if (str.IndexOf("Weight") >= 0)
// {
// str = str.Substring(str.IndexOf("Weight") 6);
// }
// else if (str.IndexOf("WT:") >= 0)
// {
// str = str.Substring(str.IndexOf("WT:") 3);
// }
// }
// else//不带换行
// {
// #region 不带回车符的连续发送
// if (extStr.Contains("ENTER."))
// {
// serPort.NewLine = "ENTER."; //空格代表换行
// str = serPort.ReadLine();
// }
// else
// {
// str = extStr;
// }
// #endregion
// }
// }
// else//数据不做任何处理,按键发送
// {
// str = extStr = serPort.ReadLine();
// loger.Info(string.Format("ReadExisting:{0}", str));
// }
// if (str.Length > 0)
// {
// if (Filter.Length > 0 && str.IndexOf(Filter) >= 0 && Filter != "0")
// {
// str = str.Substring(str.IndexOf(Filter) Filter.Length);
// }
// Regex reg = new Regex(@"-?[\d] .?[\d] ");
// str = reg.Match(str).Value;
// loger.Info(string.Format("MatchString:{0}", str));
// //if (this.IsHandleCreated && str.Length > 1) { this.Invoke(getData, new string[] { str }); }
// //try { }
// //catch { getData(str); }
// //getData(str);
// this.Dispatcher.Invoke(getData, new string[] { str });
// _weightZL = toDouble(str);
// }
// }
//}
//catch (Exception ex)
//{
// //MessageBox.Show(ex.Message);
//}
}
/// <summary>
/// 设置串口属性
/// </summary>
/// <param name="strPort">端口号</param>
/// <param name="Bit">波特率</param>
/// <param name="Length">数据位</param>
/// <param name="Parity">较验码</param>
/// <param name="Stop">停止位</param>
public void IniSerPort(string[] strPorts,int[] Bits,int[] Lengths,int[] Paritys,string[] Stops)
{
Ports = strPorts;
iBits = Bits;
iLengths = Lengths;
iParitys = Paritys;
iStops = Stops;
}
private double toDouble(object str)
{
double result = 0.0;
try { result=Convert.ToDouble(str); }
catch { }
return result;
}
/// <summary>
/// 返回计量仪表上的重量数据
/// </summary>
/// <returns></returns>
public static string GetjlybData()
{
return _weightZL;
}
}
}
串口通讯:化验数据采集[测硫仪]
using System;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Text.RegularExpressions;
using DataParse;
//using System.Windows.Forms;
namespace SendData
{
/// <summary>
///
/// </summary>
public partial class uSerPort : Label
{
private SerialPort[] serPorts;
public delegate void GetComData(string sValue);
public GetComData getData;
private bool[] isOpens;
public static string _weightZL = "";
[DescriptionAttribute("通讯端口号")]
/// <summary>
/// 串口号
/// </summary>
public string[] Ports;
[DescriptionAttribute("通讯波特率")]
public int[] iBits;
[DescriptionAttribute("数据长度")]
public int[] iLengths;
[DescriptionAttribute("数据较验:0,1,2")]
//0为None,1为寄校验,2为偶较验
public int[] iParitys;
[DescriptionAttribute("数据停止位:0,1,2")]
public string[] iStops;
[DescriptionAttribute("过滤的字符")]
public string[] filters;
[DescriptionAttribute("是否连续发送")]
public string CanSpace;
[DescriptionAttribute("台称个数")]
public int iNum=1;
[DescriptionAttribute("数据解析方法")]
public string dataParse = "";
[DescriptionAttribute("设备ID")]
public string deviceId = "";
public uSerPort()
{
InitializeComponent();
//this.TextAlign = ContentAlignment.MiddleCenter;
//this.serPort.ReadBufferSize = 1024;
}
/// <summary>
/// 串口打开
/// </summary>
/// <returns></returns>
public bool[] OpenPort()
{
isOpens = new bool[iNum];
try
{
serPorts = new SerialPort[iNum];
for (int i = 0; i < iNum; i )
{
isOpens[i] = false;
serPorts[i] = new SerialPort();
serPorts[i].ReceivedBytesThreshold = 1;
serPorts[i].BaudRate = iBits[i];
serPorts[i].DataBits = iLengths[i];
serPorts[i].DtrEnable = true;
serPorts[i].PortName = Ports[i];
int iStop = 0;
if (iStops[i] == "1.5")
iStop = 15;
else
{
try
{
iStop = Convert.ToInt16(iStops[i]);
}
catch { }
}
switch (iParitys[i])
{
default:
case 0:
serPorts[i].Parity = Parity.None;
break;
case 1:
serPorts[i].Parity = Parity.Odd;
break;
case 2:
serPorts[i].Parity = Parity.Even;
break;
}
switch (iStop)
{
default:
serPorts[i].StopBits = StopBits.One;
break;
case 2:
serPorts[i].StopBits = StopBits.Two;
break;
case 15:
serPorts[i].StopBits = StopBits.OnePointFive;
break;
}
if (serPorts[i].IsOpen)
serPorts[i].Close();
serPorts[i].Open();
isOpens[i] = true;
if (i == 0)
{
serPorts[i].DataReceived = new SerialDataReceivedEventHandler(serPort_DataReceived);
}
else
{
serPorts[i].DataReceived = new SerialDataReceivedEventHandler(serPort_DataReceived2);
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return isOpens;
}
/// <summary>
/// 串口关闭
/// </summary>
/// <returns></returns>
public bool ClosePort()
{
bool flag = false;
isOpens = new bool[iNum];
try
{
for (int i=0;i<iNum;i )
{
if (serPorts[i].IsOpen)
{
if(i==0)
serPorts[i].DataReceived -= new SerialDataReceivedEventHandler(serPort_DataReceived);
else
serPorts[i].DataReceived -= new SerialDataReceivedEventHandler(serPort_DataReceived2);
serPorts[i].Close();
//serPort.Dispose();
}
flag = true;
isOpens[i] = false;
}
}
catch (Exception ex)
{
flag = false;
throw new Exception(ex.Message);
}
return flag;
}
/// <summary>
/// 获取端口打开状态
/// </summary>
/// <returns></returns>
public bool[] GetPortState()
{
bool[] isOp = new bool[iNum];
for (int i = 0; i < iNum;i )
{
isOp[i] = serPorts[i].IsOpen;
}
return isOp;
}
private void serPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort serPort = sender as SerialPort;
getWeight(serPort, filters[0]);
}
private void serPort_DataReceived2(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort serPort = sender as SerialPort;
getWeight(serPort, filters[1]);
}
private void getWeight(SerialPort serPort, string Filter)
{
string str = TransWeight.getWeight(serPort, Filter);
getData(str);
//this.Dispatcher.Invoke(getData, new string[] { str });
//this.Invoke(getData, new string[] { str });
_weightZL = str;
//SerialPort serPort = sender as SerialPort;
//try
//{
// if (serPort.IsOpen)
// {
// string extStr = "";
// string str = "";
// if (Filter != "0")
// {
// System.Threading.Thread.Sleep(200);
// extStr = serPort.ReadExisting();
// loger.Info(string.Format("ReadExisting:{0}", extStr));
// if (extStr.IndexOf("\r") > 0)//带换行
// {
// str = extStr.Replace("\r", "");
// if (str.IndexOf("Net") >= 0)
// {
// str = str.Substring(str.IndexOf("Net") 3);
// }
// else if (str.IndexOf("Weight") >= 0)
// {
// str = str.Substring(str.IndexOf("Weight") 6);
// }
// else if (str.IndexOf("WT:") >= 0)
// {
// str = str.Substring(str.IndexOf("WT:") 3);
// }
// }
// else//不带换行
// {
// #region 不带回车符的连续发送
// if (extStr.Contains("ENTER."))
// {
// serPort.NewLine = "ENTER."; //空格代表换行
// str = serPort.ReadLine();
// }
// else
// {
// str = extStr;
// }
// #endregion
// }
// }
// else//数据不做任何处理,按键发送
// {
// str = extStr = serPort.ReadLine();
// loger.Info(string.Format("ReadExisting:{0}", str));
// }
// if (str.Length > 0)
// {
// if (Filter.Length > 0 && str.IndexOf(Filter) >= 0 && Filter != "0")
// {
// str = str.Substring(str.IndexOf(Filter) Filter.Length);
// }
// Regex reg = new Regex(@"-?[\d] .?[\d] ");
// str = reg.Match(str).Value;
// loger.Info(string.Format("MatchString:{0}", str));
// //if (this.IsHandleCreated && str.Length > 1) { this.Invoke(getData, new string[] { str }); }
// //try { }
// //catch { getData(str); }
// //getData(str);
// this.Dispatcher.Invoke(getData, new string[] { str });
// _weightZL = toDouble(str);
// }
// }
//}
//catch (Exception ex)
//{
// //MessageBox.Show(ex.Message);
//}
}
/// <summary>
/// 设置串口属性
/// </summary>
/// <param name="strPort">端口号</param>
/// <param name="Bit">波特率</param>
/// <param name="Length">数据位</param>
/// <param name="Parity">较验码</param>
/// <param name="Stop">停止位</param>
public void IniSerPort(string[] strPorts,int[] Bits,int[] Lengths,int[] Paritys,string[] Stops)
{
Ports = strPorts;
iBits = Bits;
iLengths = Lengths;
iParitys = Paritys;
iStops = Stops;
}
private double toDouble(object str)
{
double result = 0.0;
try { result=Convert.ToDouble(str); }
catch { }
return result;
}
/// <summary>
/// 返回计量仪表上的重量数据
/// </summary>
/// <returns></returns>
public static string GetjlybData()
{
return _weightZL;
}
}
}