您的位置:控制工程论坛网论坛 » 传感器 » 厦门宇电智能调节器的通讯控制程序

semiscone

semiscone   |   当前状态:在线

总积分:4800  2024年可用积分:0

注册时间: 2008-04-29

最后登录时间: 2013-07-11

空间 发短消息加为好友

厦门宇电智能调节器的通讯控制程序

semiscone  发表于 2008/12/19 14:50:47      785 查看 0 回复  [上一主题]  [下一主题]

手机阅读

今天完善了下温控仪的控制程序。温度控制仪采用的是厦门宇电生产的AI-708P型温度控制器。在官方论坛上,看见很多人在问如何写PC和温控仪的通讯控制程序。

把我写的发上来,希望能和大家一起讨论。

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;

using System.IO.Ports;
namespace InternalFrictionApparatus
{
    public partial class TempController : Form
    {
        long nATime;
        public byte[] cmdCode = new byte[8];
       
        public TempController()
        {
            InitializeComponent();
           
            mySerialPort.PortName = "COM1";
            mySerialPort.BaudRate = 9600;
            mySerialPort.Open();
            mySerialPort.ReadTimeout = 1000;
            mySerialPort.WriteTimeout = 1000;

        }
               
        private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] data = Encoding.Unicode.GetBytes(txtSend.Text);
            string str = Convert.ToBase64String(data);
            try
            {
             
                mySerialPort.WriteLine(str);
                MessageBox.Show("数据发送成功!", "系统提示");      
            }
            catch (System.Exception Err)
            {
                MessageBox.Show(Err.Message);
            }              
        }

        private void btnReceive_Click(object sender, EventArgs e)
        {
            try
            {
                //byte[] data = Convert.FromBase64String(mySerialPort.ReadLine());
                byte[] data = Convert.FromBase64String(mySerialPort.ReadLine());
                txtReceive.Text = Encoding.Unicode.GetString(data);
                MessageBox.Show("数据接收成功!", "系统提示");
            }
            catch (System.Exception Err)
            {
                MessageBox.Show(Err.Message);
            } 
          
        }       
       
        private void TempController_FormClosing(object sender, FormClosingEventArgs e)
        {           
            mySerialPort.Dispose();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {           
            uint opCode = 21;
            uint opValue = 200;
           
            CalculateReadCode(opCode,opValue);

            nATime = Environment.TickCount;
                       
            mySerialPort.Write(cmdCode,0,8);
           
        }

        private void btnRev_Click(object sender, EventArgs e)
        {
            byte[] instring = new byte[10];
            float pv;
            int sv, mv, alm, cs, crc;
            try
            {
                mySerialPort.Read(instring, 0, 10);
                pv = JoinToByte(instring[0], instring[1]);  // 测量值
                sv = JoinToByte(instring[2], instring[3]);  // 给定值
                mv = instring[4];
                alm = instring[5];
                cs = JoinToByte(instring[6], instring[7]);  // 所读/写参数值
                crc = JoinToByte(instring[8], instring[9]); // 校验码
                MessageBox.Show("测量值:" + (pv / 10).ToString() + "\r\n给定值:" + sv.ToString() + "\r\nmv" + mv.ToString()
                    + "\r\nalm" + alm.ToString() + "\r\n所读/写参数值:" + cs.ToString() + "\r\n校验码:" + crc.ToString());
            }
            catch (System.Exception Err)
            {
                MessageBox.Show(Err.Message);             
            }
        }

        public int JoinToByte(byte lowbyte,byte highbyte)
        {
            int c;
            c = highbyte;
            if (c > 127)
            { c = (c - 256) * 256; }
            else
            { c = c * 256; }
            return c + lowbyte;
           
        }

        private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string extingString = mySerialPort.ReadExisting();
            MessageBox.Show(extingString.ToString());
            /*
             * byte[] instring = new byte[10];
            //float pv;
            int pv, sv, mv, alm, cs, crc;
            mySerialPort.Read(instring, 0, 10);
            pv = JoinToByte(instring[0], instring[1]);  // 测量值
            sv = JoinToByte(instring[2], instring[3]);  // 给定值
            mv = instring[4];
            alm = instring[5];
            cs = JoinToByte(instring[6], instring[7]);  // 所读/写参数值
            crc = JoinToByte(instring[8], instring[9]); // 校验码
            MessageBox.Show("测量值:" + (pv / 10).ToString() + "\r\n给定值:" + sv.ToString() + "\r\nmv" + mv.ToString()
                + "\r\nalm" + alm.ToString() + "\r\n所读/写参数值:" + cs.ToString() + "\r\n校验码:" + crc.ToString()
                +"\r\nTime: "+(Environment.TickCount-nATime).ToString()+"\r\nBufferSize: "+bufferSize.ToString());
             */
        }

        public void 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
            // 写校验码 = 指令参数*256 + 67 + 参数值 + 1
            uint crc = opCode * 256 + 82 + opValue + 1;
            cmdCode[6] = Convert.ToByte(crc % 256); // 低字节
            cmdCode[7] = Convert.ToByte(crc / 256);  // 高字节
        }
    }   
}

1楼 0 0 回复