• c#学习心得(4)


    近期准备做一个上位机采集传感器的数据 使用串口进行通信

    翻看vs的说明文档 看到有关serialport的部分 特此记录

    后续仍有不懂的地方 再行记录

    此处放上关于串口中关于数据接受事件的例子 https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.datareceived?view=dotnet-plat-ext-3.1

    有关事件的使用,还是不太明白 翻看了一个帖子

    https://www.cnblogs.com/gq0324/p/8177799.html

    前段时间做了一个处理数据的小程序 处理坐标的偏差 记录一下

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Text.RegularExpressions;
    
    
    namespace 读取文本并处理数据
    {
        class Program
        {
            static void Main(string[] args)
            {
            
                //声明全局变量
                string path = @"C:UsersAdministratorDesktop1.txt";
                string[] lines = ReadTxt(path);
                int lenth = lines.Length;
                double[] doubleArr = new double[99];
                double[] doubleArr1 = new double[99];
                double[] doubleArr2 = new double[99];
    
    
                Console.WriteLine(lenth);
                Console.WriteLine(lines[2]);
                //按空格分割字符串
                string[] arr = Regex.Split(lines[0], "\s+", RegexOptions.IgnoreCase);
                Console.WriteLine(arr[1]);
    
                //for循环分割所有行
                for (int i = 0; i < 34; i++)
                {
    
                    string[] splitarr = StringSplit(lines[i]);
    
                    //Console.WriteLine (splitarr [0]+"    "+ splitarr [1]+"    "+splitarr [2]);
                    //添加字符串进数组
                    string[] arr3 = new string[3] { splitarr[0], splitarr[1], splitarr[2] };
                    double value1 = TransStrToDb(splitarr[0]);
                    double value2 = TransStrToDb(splitarr[1]);
                    double value3 = TransStrToDb(splitarr[2]);
    
                    //将元素添加进数组
                    doubleArr[i] = value1;
                    doubleArr1[i] = value2;
                    doubleArr2[i] = value3;
    
                    /*
                   //遍历数组
                    foreach (var item in doubleArr)
                        Console.WriteLine(item);
                   */
                    //打印数组
                    Console.Write("第{0}行   ", i);
                    Console.WriteLine(doubleArr[i] + "     " + doubleArr1[i] + "    " + doubleArr2[i]);
    
    
    
    
                }
    
                double avr1 = Average(doubleArr);
                double avr11 = Average1(doubleArr);
                double avr12 = Average2(doubleArr);
    
                double avr2 = Average(doubleArr1);
                double avr3 = Average(doubleArr2);
    
                Console.WriteLine("平均数" + avr1);
                Console.WriteLine("平均数" + avr2);
                Console.WriteLine("平均数" + avr3);
    
                //求偏差
                double p1 = Calculate(avr1, avr2, avr3, doubleArr[0], doubleArr1[0], doubleArr2[0]);
                Console.WriteLine("偏差 " + p1);
              //后续太懒就只测试一个就行了
            }
        
    
                
                
    
               
     
         
            //方法 读取文本 返回字符串数组
            private static string[] ReadTxt(string path)
            {
                string[] lines = File.ReadAllLines(path);
    
                return lines;
    
            }
             
            //方法 将字符串转换成double
            private static double TransStrToDb(string s)
            { 
                double value = double .Parse (s );
                return value;
            }
            
            //方法 将行字符串分割
            private static string[] StringSplit(string s)
            {
                string[] splitarr = Regex.Split(s, "\s+", RegexOptions.IgnoreCase);
                return splitarr;
            }
            //计算公式
            private static double Calculate(double avgX,
                double avgY,
                double avgZ,
                double xi,
                double yi,
                double zi)
            {
                //p= √((x-x1)²+(z-z1)²+(y-y1)²)
                double p =  Math .Sqrt (Math.Pow ((avgX -xi ),2)+Math .Pow ((avgY -yi),2)+Math .Pow ((avgZ -zi ),2));
                return p;
    
            }
            private static double Average(double [] d)
            {
                double avr = 0;
                double sum = d[3];
                int i = 3;
                while (i < 28 )
                {
    
                    sum = sum + d[i + 3];
                    i = i + 3;
    
    
                }
                avr = sum / 10;
                    return avr;
            }
            private static double Average1(double[] d)
            {
                double avr = 0;
                double sum = d[4];
                int i = 4;
                while (i < 29)
                {
    
                    sum = sum + d[i + 3];
                    i = i + 3;
    
    
                }
                avr = sum / 10;
                return avr;
    
            }
            private static double Average2(double[] d)
            {
                double avr = 0;
                double sum = d[5];
                int i = 5;
                while (i < 30)
                {
    
                    sum = sum + d[i + 3];
                    i = i + 3;
    
    
                }
                avr = sum / 10;
                return avr;
    
            }
        }
    }
    

      

  • 相关阅读:
    python-Mitmproxy抓包
    pytest-html、cov、xdist
    python-unittest添加用例的几种方式
    python-*args、**kargs用法
    One,Two,Three,Ak模板
    栈和队列小练习
    区块链入门介绍笔记
    Research on Facebook and Social Graph
    线段树板子的小修改
    htaccess远古时期技术了解一下
  • 原文地址:https://www.cnblogs.com/ttplearning/p/12961128.html
Copyright © 2020-2023  润新知