⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 temperaturecontroller.cs

📁 厦门宇控
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;

namespace InternalFrictionApparatus
{
    class TemperatureController
    {       
        private uint myOpCode;
        //private uint myOpValue;
        
        public byte[] cmdCode = new byte[8];
        public uint sv, mv, alm, cs, crc;
        public float pv = 0;
        
        SerialPort mySerialPort = new SerialPort();
               
        public TemperatureController()
        {
            mySerialPort.Open();
            mySerialPort.ReadTimeout = 1000;
            mySerialPort.WriteTimeout = 1000;
        }
        public void InitializeParameters()
        {
            cmdCode = CalculateWriteCode(0, 1);
            mySerialPort.Write(cmdCode, 0, 8);
        }
        
        // 写入设定温度值
        public void WriteCommand()
        {
            uint opCode = 26;   // C01
            float opValue;
            
            try
            {
                for (int i = 0; i < 30; i++)
                {
                    for (int j = 0; j < 2; j++)
                    {
                        opValue = Parameters.CT[i, j];
                        cmdCode = CalculateWriteCode(opCode, opValue);
                        mySerialPort.Write(cmdCode, 0, 8);

                        System.Threading.Thread.Sleep(50);
                        
                        opCode++;                        
                    }
                    if (Parameters.CT[i,1]==0)
                    {
                        break;
                    }
                   
                }                
            }
            catch (System.Exception Err)
            {
                MessageBox.Show(Err.Message);
            }        
        }

        public void Run()
        {
            cmdCode = CalculateWriteCode(21, 0);
            mySerialPort.Write(cmdCode, 0, 8);
        }

        public void Stop()
        {
            cmdCode = CalculateWriteCode(21, 12);
            mySerialPort.Write(cmdCode, 0, 8);
        }

        private byte[] CalculateWriteCode(uint opCode,float opValue)
        {
            myOpCode = opCode;
            //myOpValue = opValue;
            
            // 地址:H81(80H+1)
            cmdCode[0] = 129;
            cmdCode[1] = 129;

            // 写:43H,67;读:52H,82
            cmdCode[2] = 67;

            // 指令参数代码
            cmdCode[3] = Convert.ToByte(opCode);

            // 参数值
            cmdCode[4] = Convert.ToByte(opValue % 256); // Low
            cmdCode[5] = Convert.ToByte(opValue / 256); // High
            
            // 校验码             
            // 写校验码 = 指令参数*256 + 67 + 参数值 + 1
            uint crc = Convert.ToUInt32(opCode * 256 + 67 + opValue + 1);
            cmdCode[6] = Convert.ToByte(crc % 256); // 低字节
            cmdCode[7] = Convert.ToByte(crc / 256);  // 高字节

            return cmdCode;
        }

        private byte[] CalculateReadCode(uint opCode, uint opValue)
        {
            // 地址:H81(80H+1)
            cmdCode[0] = 129;
            cmdCode[1] = 129;

            // 读:52H,82
            cmdCode[2] = 82;

            // 指令参数代码
            cmdCode[3] = Convert.ToByte(opCode);

            // 参数值
            cmdCode[4] = Convert.ToByte(opValue % 256); // Low
            cmdCode[5] = Convert.ToByte(opValue / 256); // High

            // 校验码 
            // 读校验码 = 指令参数*256 + 82 + 1            
            uint crc = opCode * 256 + 82 + opValue + 1;
            cmdCode[6] = Convert.ToByte(crc % 256); // 低字节
            cmdCode[7] = Convert.ToByte(crc / 256);  // 高字节

            return cmdCode;
        }
        
        public float GetTemperatureStatus()
        {           
            uint opCode = 21;
            uint opValue = 200;
            cmdCode = CalculateReadCode(opCode, opValue);
            
            byte[] instring = new byte[10];

            try
            {
                mySerialPort.ReadExisting();

                mySerialPort.Write(cmdCode, 0, 8);
                System.Threading.Thread.Sleep(50);
                mySerialPort.Read(instring, 0, 10);
               
                //WriteLog(instring);
                pv = JoinTwoByte(instring[0], instring[1]);  // 测量值
                sv = JoinTwoByte(instring[2], instring[3]);  // 给定值
                mv = instring[4];
                alm = instring[5];
                cs = JoinTwoByte(instring[6], instring[7]);  // 所读/写参数值
                crc = JoinTwoByte(instring[8], instring[9]); // 校验码                
            }
            catch (System.Exception Err)
            {
                MessageBox.Show(Err.Message);
            }
            return pv / 10;
        }

        private uint JoinTwoByte(byte lowbyte, byte highbyte)
        {
            uint c;
            c = highbyte;
            if (c > 127)
            { c = (c - 256) * 256; }
            else
            { c = c * 256; }
           
            return c + lowbyte;
        }

       

        private void WriteLog(byte[] bytes)
        {
            string strLog = null;
            for (int i=0;i<10;i++)
            {
                strLog += bytes[i];
                strLog += "\t";
            }
            strLog += "\r\n";
            string strPath = Application.StartupPath + "\\Log.txt";
            //File.WriteAllText(strPath, strLog);

            
            // This text is added only once to the file.
            if (!File.Exists(strPath))
            {
                // Create a file to write to.
                using (StreamWriter sw = File.CreateText(strPath))
                {
                    sw.Write(strLog);
                    //sw.WriteLine("And");                   
                }
            }

            // This text is always added, making the file longer over time
            // if it is not deleted.
            using (StreamWriter sw = File.AppendText(strPath))
            {
                sw.Write(strLog);
                //sw.WriteLine("This");
                
            }

            // Open the file to read from.
            using (StreamReader sr = File.OpenText(strPath))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -