日期:2014-05-20  浏览次数:21112 次

C# serialport+mschart读取上位机数据绘制实时曲线之进制转换,与mschart数据源绑定
我现在的问题主要在于从串口读取数据后,进制转换,以及如何实现按照3秒的时间间隔来控制串口的读取和图像绘制、、、  

小弟刚刚接触串口,还望各位大哥大姐多多指教。。。、

        DBhelper sql = new DBhelper();
        #region  变量声明
        //定义 SerialPort对象
        SerialPort serialPort1 = new SerialPort();
        Byte[] vbReceive = new byte[6764];//声明字节型数组,用以存储串口中读取的数据
        Byte[] receiveBuffer = new byte[6764];//声明字节型数组,用以存储串口中读取的数据
        double[] gDiameter = new double[] { -1, -1, -1, 0.523, 0.542, 0.582, 0.626, 0.673, 0.723, 0.777, 0.835, 0.897, 0.964, 1.036, 1.113, 1.196, 1.286, 1.381, 1.485, 1.595, 1.714, 1.842, 1.98, 2.128, 2.286, 2.457, 2.64, 2.837, 3.052, 3.277, 3.521, 3.784, 4.066, 4.37, 4.696, 5.046, 5.423, 5.828, 6.263, 6.73, 7.232, 7.772, 8.352, 8.975, 9.645, 10.365, 11.138, 11.97, 12.863, 13.823, 14.854, 15.963, 17.154, 18.434, 19.81 };      //用于标识横轴坐标的[55]粒径数组
        long[] gDataInDiamete = new long[55];// 每次从串口读取的粒径通道的数据(52个粒径通道)

        delegate void ReceivData(byte[] bytes);//声明一个委托用于接收串口数据
        
        serialPort1.ReadBufferSize = 6764;                  //接收缓冲区大小
        #endregion

        


        /// <summary>
        /// 串口激发事件
        /// </summary>
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            #region  判断接收数据包大小达到6764,并进行相关数据计算
            if (serialPort1.ReceivedBytesThreshold >= 6764)
            {
                serialPort1.Read(vbReceive, 0, 6764);
                if (vbReceive[0] == 255 && vbReceive[1] == 1&&vbReceive[6762]=255&&vbReceive[6763]=2)
                {

                    gDataInDiamete[1] = -1;
                    gDataInDiamete[2] = -1;
                    gDataInDiamete[3] = -1;