www.pudn.com > 485MODBUS.rar > Form1.cs, change:2010-12-10,size:11092b


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO.Ports; 
using System.Timers; 
using System.Collections; 
using System.Diagnostics; 
 
namespace chuankou 
{ 
 
    public partial class Form1 : Form 
    { 
        
        int m = 0; 
        int p; 
        byte[] TxData1 = new byte[] { }; 
        byte[] TxData2 = new byte[] { }; 
        public Form1() 
        { 
            InitializeComponent(); 
 
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            sp.Close(); 
            btn_close.Enabled = false; 
            sp.Close(); 
            for (int i = 0; i < 8; i++) 
            { 
                cbxCOMPort.Items.Add("COM" + (i + 1).ToString()); 
 
            } 
            cbxCOMPort.SelectedIndex = 0; 
            //列出常用的波特率 
            cbxBaudRate.Items.Add("1200"); 
            cbxBaudRate.Items.Add("2400"); 
            cbxBaudRate.Items.Add("4800"); 
            cbxBaudRate.Items.Add("9600"); 
            cbxBaudRate.Items.Add("19200"); 
            cbxBaudRate.Items.Add("28800"); 
            cbxBaudRate.Items.Add("38400"); 
            cbxBaudRate.Items.Add("57600"); 
            cbxBaudRate.Items.Add("115200"); 
            cbxBaudRate.SelectedIndex = 3; 
            //列出数据位 
            cbxDataBits.Items.Add("8"); 
            cbxDataBits.Items.Add("7"); 
            cbxDataBits.Items.Add("6"); 
            cbxDataBits.Items.Add("5"); 
            cbxDataBits.SelectedIndex = 0; 
            //列出停止位 
            cbxStopBits.Items.Add("0"); 
            cbxStopBits.Items.Add("1"); 
            cbxStopBits.Items.Add("1.5"); 
            cbxStopBits.Items.Add("2"); 
            cbxStopBits.SelectedIndex = 1; 
            //列出奇偶校验位 
            cbxParity.Items.Add("None"); 
            cbxParity.Items.Add("Odd"); 
            cbxParity.Items.Add("Even"); 
            cbxParity.Items.Add("Mark"); 
            cbxParity.Items.Add("Space"); 
            cbxParity.SelectedIndex = 2; 
            
        } 
 
 
 
        private void btn_close_Click(object sender, EventArgs e) 
        { 
            btn_close.Enabled = false; 
            sp.Close(); 
            btn_open.Enabled = true; 
        } 
 
        private void btn_open_Click(object sender, EventArgs e) 
        { 
            int nSelect; 
            btn_open.Enabled = false; 
            Object selectedItem = cbxCOMPort.SelectedItem; 
            sp.PortName = selectedItem.ToString();//串口 
            selectedItem = cbxBaudRate.SelectedItem; 
            sp.BaudRate = int.Parse(selectedItem.ToString());//波特率 
            nSelect = cbxDataBits.SelectedIndex;//数据位 
            switch (nSelect) 
            { 
                case 0: 
                    sp.DataBits = 8; 
                    break; 
                case 1: 
                    sp.DataBits = 7; 
                    break; 
                case 2: 
                    sp.DataBits = 6; 
                    break; 
                case 3: 
                    sp.DataBits = 5; 
                    break; 
            } 
            nSelect = cbxParity.SelectedIndex; 
            switch (nSelect) 
            { 
                case 0: 
                    sp.Parity = Parity.None; 
                    break; 
                case 1: 
                    sp.Parity = Parity.Odd; 
                    break; 
                case 2: 
                    sp.Parity = Parity.Even; 
                    break; 
                case 3: 
                    sp.Parity = Parity.Mark; 
                    break; 
                case 4: 
                    sp.Parity = Parity.Space; 
                    break; 
            } 
            nSelect = cbxStopBits.SelectedIndex; 
            switch (nSelect) 
            { 
                case 0: 
                    sp.StopBits = StopBits.None; 
                    break; 
                case 1: 
                    sp.StopBits = StopBits.One; 
                    break; 
                case 2: 
                    sp.StopBits = StopBits.OnePointFive; 
                    break; 
                case 3: 
                    sp.StopBits = StopBits.Two; 
                    break; 
            } 
            sp.Open(); 
            btn_close.Enabled = true; 
        } 
 
        private void btn_send_Click(object sender, EventArgs e) 
        { 
            textBox1.Text = ""; 
            int n = 6;           
            byte[] buf = new byte[n]; 
            string ee = textBox4.Text; 
            buf[0] = Convert.ToByte(ee); 
            if (comboBox1.Text == "3X") 
            { 
                buf[1] = Convert.ToByte(3); 
            } 
            else 
            { 
                buf[1] = Convert.ToByte(4); 
            } 
            if (int.Parse(textBox5.Text) <= 255) 
            { 
                buf[2] = Convert.ToByte(0); 
                buf[3] = Convert.ToByte(int.Parse(textBox5.Text)); 
            } 
            else 
            { 
                if (Convert.ToString(int.Parse(textBox5.Text), 16).Length < 4) 
                { 
                    int b = Convert.ToInt32(Convert.ToString(int.Parse(textBox5.Text), 16).Substring(0, 1), 16); 
                    buf[2] = Convert.ToByte(b); 
                    int c = Convert.ToInt32(Convert.ToString(int.Parse(textBox5.Text), 16).Substring(1, 2), 16); 
                    buf[3] = Convert.ToByte(c); 
                } 
                else 
                { 
                    int b = Convert.ToInt32(Convert.ToString(int.Parse(textBox5.Text), 16).Substring(0, 2), 16); 
                    buf[2] = Convert.ToByte(b); 
                    int c = Convert.ToInt32(Convert.ToString(int.Parse(textBox5.Text), 16).Substring(2, 2), 16); 
                    buf[3] = Convert.ToByte(c); 
                } 
            } 
 
 
            if (int.Parse(textBox6.Text) <= 255) 
            { 
                buf[4] = Convert.ToByte(0); 
                buf[5] = Convert.ToByte(int.Parse(textBox6.Text)); 
            } 
            else 
            { 
                if (Convert.ToString(int.Parse(textBox6.Text), 16).Length < 4) 
                { 
                    int d = Convert.ToInt32(Convert.ToString(int.Parse(textBox6.Text), 16).Substring(0, 1), 16); 
                    buf[4] = Convert.ToByte(d); 
                    int f = Convert.ToInt32(Convert.ToString(int.Parse(textBox6.Text), 16).Substring(1, 2), 16); 
                    buf[5] = Convert.ToByte(f); 
                } 
                else 
                { 
                    int d = Convert.ToInt32(Convert.ToString(int.Parse(textBox6.Text), 16).Substring(0, 2), 16); 
                    buf[4] = Convert.ToByte(d); 
                    int f = Convert.ToInt32(Convert.ToString(int.Parse(textBox6.Text), 16).Substring(2, 2), 16); 
                    buf[5] = Convert.ToByte(f); 
                } 
            } 
            p = int.Parse(textBox6.Text)*2+5; 
            byte[] buf1 = new byte[n + 2]; 
            byte[] ReturnData = new byte[2]; 
            ReturnData = CRC16_C(buf); 
            for (int i = 0; i < n; i++) 
            { 
                buf1[i] = buf[i]; 
            } 
            buf1[n] = ReturnData[1]; 
            buf1[n + 1] = ReturnData[0]; 
            sp.Write(buf1.ToArray(), 0, n + 2); 
           
        } 
        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) 
        { 
 
            int n = sp.BytesToRead; 
            byte[] TxData = new byte[n];//声明一个临时数组存储当前来的串口数据 
            sp.Read(TxData, 0, n); //读取缓冲数据  
            m = m + n; 
            this.Invoke((EventHandler) 
               (delegate 
                    {      
                        
                        Array.Resize(ref  TxData2, m);//改写数组大小 
                        TxData.CopyTo(TxData2, m - TxData.Length); 
                        if (m == p) 
                        { 
                            byte[] data = new byte[p-2]; 
                            for (int i = 0; i < TxData2.Length -2;i++ ) 
 
                            {  
                                data[i]=TxData2[i]; 
                            } 
                            byte[] data2 = new byte[2]; 
                            data2[0] = TxData2[TxData2.Length - 2]; 
                            data2[1] = TxData2[TxData2.Length - 1]; 
                            byte[] data1 = new byte[2]; 
                            data1 = CRC16_C(data); 
                            if (data1[0] == data2[1] && data1[1] == data2[0]) 
                            { 
                                for (int j = 3; j <= m - 4; j = j + 2) 
                                { 
                                    string s1 = TxData2[j].ToString(); 
                                    string s2 = TxData2[j + 1].ToString(); 
                                    string s = s1 + s2; 
                                    int g = Convert.ToInt32(s, 10); 
                                    string h = g.ToString(); 
                                    textBox1.Text += h + "  "; 
                                } 
                                textBox7.Text = "读取成功"; 
                            } 
                            else 
                            { 
                                textBox7.Text ="读取失败"; 
                            } 
                            m = 0; 
                        } 
 
                    } 
               ) 
            ); 
        } 
        public byte[] CRC16_C(byte[] data) 
        { 
            byte CRC16Lo; 
            byte CRC16Hi; 
            byte CL; byte CH; 
            byte SaveHi; byte SaveLo; 
            byte[] tmpData; 
            int Flag; 
            CRC16Lo = 0xFF; 
            CRC16Hi = 0xFF; 
            CL = 0x01; 
            CH = 0xA0; 
            tmpData = data; 
            for (int i = 0; i < tmpData.Length; i++) 
            { 
                CRC16Lo = (byte)(CRC16Lo ^ tmpData[i]); 
                for (Flag = 0; Flag <= 7; Flag++) 
                { 
                    SaveHi = CRC16Hi; 
                    SaveLo = CRC16Lo; 
                    CRC16Hi = (byte)(CRC16Hi >> 1); 
                    CRC16Lo = (byte)(CRC16Lo >> 1); 
                    if ((SaveHi & 0x01) == 0x01) 
                    { 
                        CRC16Lo = (byte)(CRC16Lo | 0x80); 
                    } 
 
                    if ((SaveLo & 0x01) == 0x01) 
                    { 
                        CRC16Hi = (byte)(CRC16Hi ^ CH); 
                        CRC16Lo = (byte)(CRC16Lo ^ CL); 
                    } 
                } 
            } 
            byte[] ReturnData = new byte[2]; 
            ReturnData[0] = CRC16Hi; 
            ReturnData[1] = CRC16Lo; 
            return ReturnData; 
        } 
 
    } 
}