C#winform串口使用及串口调试工具

需要控件:

串口传输serialport
定时器:timer


使用方法:

--C:\\WeightConfig.xml文件写法, 串口配置文件
<?xml version="1.0" encoding="utf-8" ?>
<Config>
    <PortName_S>COM2</PortName_S>//串口名称
    <StartString_S>U/W</StartString_S>//串口读取到的数据格式,既电子称返回的数据格式例如G + 10.02
    <WeightStrFirstChatPos_S>1</WeightStrFirstChatPos_S>//从第几位开始读,默认
    <WeightStrLen_S>9</WeightStrLen_S>//数据长度
    --<PortName_B>COM4</PortName_B>
    --<StartString_B>+</StartString_B>//如果需要多个串口加上以下一段
    --<WeightStrFirstChatPos_B>1</WeightStrFirstChatPos_B>
    --<WeightStrLen_B>8</WeightStrLen_B>
    --<StartString_S_YB>G  +</StartString_S_YB>//一个称同时返回两种数据例如G+和+同时返回
    --<WeightStrFirstChatPos_S_YB>1</WeightStrFirstChatPos_S_YB>
    --<WeightStrLen_S_YB>9</WeightStrLen_S_YB>
    --<DanWei_S>g</DanWei_S>
</Config>
if (File.Exists(@"C:\WeightConfig.xml"))
{
    this.timer1.Enabled = true;//启动计时器
    CloseCom();
    ReadCOMConfig();//读取配置文件
    InitCom();
    OpenCom();
}
private void CloseCom()//关闭串口
{
      Exception exception;
      try
      {
          if (this.serialPort1.IsOpen)
          {
              this.serialPort1.Close();
          }
      }
      catch (Exception exception5)
      {
          exception = exception5;
          throw exception;
      }
}
#region 读取COM口配置参数
private void ReadCOMConfig()
 {
     string vsUrl = "C:\\WeightConfig.xml";

     if (System.IO.File.Exists(vsUrl))
     {
         XmlDocument doc = new XmlDocument();
         doc.Load(vsUrl);

         foreach (XmlNode node in doc.DocumentElement.ChildNodes)
         {
             switch (node.Name)
             {
                 case "PortName_S":
                     PortName_S = node.InnerText;
                     break;
                 case "StartString_S":
                     StartString_S = node.InnerText;
                     break;
                 case "WeightStrFirstChatPos_S":
                     WeightStrFirstChatPos_S = Convert.ToInt32(node.InnerText);
                     break;
                 case "WeightStrLen_S":
                     WeightStrLen_S = Convert.ToInt32(node.InnerText);
                     break;
                 case "PortName_B":
                     PortName_B = node.InnerText;
                     break;
                 case "StartString_B":
                     StartString_B = node.InnerText;
                     break;
                 case "WeightStrFirstChatPos_B":
                     WeightStrFirstChatPos_B = Convert.ToInt32(node.InnerText);
                     break;
                 case "WeightStrLen_B":
                     WeightStrLen_B = Convert.ToInt32(node.InnerText);
                     break;
                 case "StartString_S_YB":
                     StartString_S_YB = node.InnerText;
                     break;
                 case "WeightStrFirstChatPos_S_YB":
                     WeightStrFirstChatPos_S_YB = Convert.ToInt32(node.InnerText);
                     break;
                 case "WeightStrLen_S_YB":
                     WeightStrLen_S_YB = Convert.ToInt32(node.InnerText);
                     break;
                 case "DanWei_S":
                     DanWei_S = node.InnerText;
                     break;
             }
         }
     }
 }
 #endregion
 private void InitCom()//初始化串口
 {
     Exception exception;
     try
     {
         this.serialPort1.PortName = PortName_B;
         this.serialPort1.BaudRate = BaudRate;
         this.serialPort1.DataBits = DataBits;
     }
     catch (Exception exception5)
     {
         exception = exception5;
         throw exception;
     }
 }

 private void OpenCom()//打开串口
 {
     Exception exception;
     try
     {
         if (!this.serialPort1.IsOpen)
         {
             this.serialPort1.Open();//打开串口
         }
     }
     catch (Exception exception5)
     {
         exception = exception5;
         throw exception;
     }
 }
private void timer1_Tick(object sender, EventArgs e)//计时器方法每隔2秒更新重量
{
    GetMac();
}

private void GetMac()//读取重量
{
    if (this.serialPort1.IsOpen)
    {
        string yuanShiStr = "";
        yuanShiStr = this.serialPort1.ReadExisting();//读取串口传输的数据(未处理)
        ReturnWeight_ZZ(yuanShiStr);//数据处理
    }
}

 #region 数据处理
private string ReturnWeight_ZZ(string YuanShiStr)
 {
     string str = "";
     string s = "";
     string str4 = "";
     string str5 = "";
     string str6 = "";
     try
     {
         str4 = YuanShiStr;//例:G + 10.2515541
         if (YuanShiStr.IndexOf(this.StartString_B) < 0)//判断是否是以G +  (xml中设置)开头
         {
             return this.LastStr_ZZ;
         }
         int index = YuanShiStr.IndexOf(this.StartString_B);
         str = YuanShiStr.Remove(0, index + this.StartString_B.Length);//移出G +等开头只取后面的数字
         if (str.Length < this.WeightStrLen_B)
         {
             return this.LastStr_ZZ;
         }
         s = str.Substring(this.WeightStrFirstChatPos_B - 1, this.WeightStrLen_B);//取有效长度
         if (!IsNumeric(s))//判断是否是数字类型
         {
             return this.LastStr_ZZ;
         }
         str5 = s;
         this.LastStr_ZZ = str5;
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return str5;
 }
 #endregion
#region 判断是否数字类型
public static bool IsNumeric(string value)
{
    try
    {
        if (Convert.ToDouble(value) <= 0)
        {
            return false;
        }
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

串口模拟工具:

sscom串口数据发送 vspd 添加虚拟串口

网盘链接:https://pan.baidu.com/s/1qtxCQmgFSASxrCkjp9CmiA 提取码:wlpm
在这里插入图片描述
添加一组虚拟串口,端口一为发送是串口//相当于称,端口二用于接收,当你实际工作需要多个串口但是电脑上只有一个com1时也可以使用这个添加虚拟串口
在这里插入图片描述
串口工具:com2发送,com3接收,需要检查串口是否已经被别的使用了,界面上的定时发送可以连续进行发送


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值