资源简介

winform读取电子秤重量RS232串口数据

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using Microsoft.Win32;

namespace TEXTCOM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private SerialPort Sp = new SerialPort();
        public delegate void HandleInterfaceUpdataDelegate(string text); //委托,此为重点 
        private HandleInterfaceUpdataDelegate interfaceUpdataHandle;

        public void Sp_DataReceived(object sender System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string strTemp = ““;
            double iSecond = 0.5;

            DateTime dtOld = System.DateTime.Now;
            DateTime dtNow = System.DateTime.Now;
            TimeSpan dtInter;
            dtInter = dtNow - dtOld;

            try
            {
                int i = Sp.BytesToRead;
                if (i > 0)
                {
                    try
                    {
                        strTemp = Sp.ReadExisting();
                        if (strTemp.IndexOf(“=“) != -1)
                        {
                            string newTemp = string.Empty;
                            for (int j = strTemp.Length - 1; j >= 0; j--)
                            {
                                newTemp += strTemp[j];
                            }
                            strTemp = newTemp.Replace(“=“ “kg\r\n+“);
                            strTemp = strTemp.Replace(“000.“ “0.“);
                        }
                    }
                    catch
                    { }
                    if (strTemp.ToLower().IndexOf(“\r“) < 0)
                    {
                        i = 0;
                    }
                    else
                    {
                        this.Invoke(interfaceUpdataHandle strTemp);
                    }
                }
                while (dtInter.TotalSeconds < iSecond && i <= 0)
                {
                    dtNow = System.DateTime.Now;
                    dtInter = dtNow - dtOld;
                    i = Sp.BytesToRead;
                    if (i > 0)
                    {
                        try
                        {
                            strTemp += Sp.ReadExisting();
                            if (strTemp.IndexOf(“=“) != -1)
                            {
                                string newTemp = string.Empty;
                                for (int j = strTemp.Length - 1; j >= 0; j--)
                                {
                                    newTemp += strTemp[j];
                                }
                                strTemp = newTemp.Replace(“=“ “kg\r\n+“);
                                strTemp = strTemp.Replace(“000.“ “0.“);
                            }

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-09-19 15:28  TEXTCOM\
     文件        5923  2009-05-31 10:50  TEXTCOM\Form1.Designer.cs
     文件       10532  2014-11-11 17:17  TEXTCOM\Form1.cs
     文件        5814  2009-05-31 10:50  TEXTCOM\Form1.resx
     文件         466  2009-05-26 13:49  TEXTCOM\Program.cs
     目录           0  2016-09-19 15:28  TEXTCOM\Properties\
     文件        1162  2009-05-26 13:49  TEXTCOM\Properties\AssemblyInfo.cs
     文件        2853  2014-10-21 16:34  TEXTCOM\Properties\Resources.Designer.cs
     文件        5612  2009-05-26 13:49  TEXTCOM\Properties\Resources.resx
     文件        1106  2014-10-21 16:34  TEXTCOM\Properties\Settings.Designer.cs
     文件         249  2009-05-26 13:49  TEXTCOM\Properties\Settings.settings
     目录           0  2017-08-30 11:38  TEXTCOM\TEXTCOM\
     文件         191  2017-08-28 17:22  TEXTCOM\TEXTCOM\App.config
     文件       15084  2017-08-30 09:45  TEXTCOM\TEXTCOM\Form1.Designer.cs
     文件        9813  2017-08-30 09:45  TEXTCOM\TEXTCOM\Form1.cs
     文件        6408  2017-08-30 09:45  TEXTCOM\TEXTCOM\Form1.resx
     文件        4711  2016-09-05 17:15  TEXTCOM\TEXTCOM\Form2.Designer.cs
     文件        8060  2017-08-28 17:23  TEXTCOM\TEXTCOM\Form2.cs
     文件        6013  2016-09-05 17:15  TEXTCOM\TEXTCOM\Form2.resx
     文件       15728  2017-08-30 11:05  TEXTCOM\TEXTCOM\Form3.Designer.cs
     文件        3422  2017-08-30 11:38  TEXTCOM\TEXTCOM\Form3.cs
     文件        6012  2017-08-30 11:05  TEXTCOM\TEXTCOM\Form3.resx
     文件         466  2017-08-30 09:35  TEXTCOM\TEXTCOM\Program.cs
     目录           0  2016-09-19 15:28  TEXTCOM\TEXTCOM\Properties\
     文件        1162  2009-05-26 13:49  TEXTCOM\TEXTCOM\Properties\AssemblyInfo.cs
     文件        2868  2009-05-26 13:49  TEXTCOM\TEXTCOM\Properties\Resources.Designer.cs
     文件        5612  2009-05-26 13:49  TEXTCOM\TEXTCOM\Properties\Resources.resx
     文件        1090  2009-05-26 13:49  TEXTCOM\TEXTCOM\Properties\Settings.Designer.cs
     文件         249  2009-05-26 13:49  TEXTCOM\TEXTCOM\Properties\Settings.settings
     文件        3984  2017-08-30 09:35  TEXTCOM\TEXTCOM\TEXTCOM.csproj
     目录           0  2016-09-19 15:28  TEXTCOM\TEXTCOM\bin\
............此处省略40个文件信息

评论

共有 条评论