Пример #1
0
 /// <summary>
 /// 协调器发送数据
 /// </summary>
 /// <returns></returns>
 public bool SendData(byte[] buffer)
 {
     try
     {
         serialPort.Write(buffer, 0, buffer.Length);
         sendData = sendData + Converts.BytesToString(buffer);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Пример #2
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="type">类型</param>
 /// <param name="addr">地址</param>
 /// <param name="data">数据</param>
 /// <returns></returns>
 public bool SendData(byte type, byte addr, byte[] data)
 {
     try
     {
         byte[] buffer = new byte[] { 0xA5, type, addr, data[0], data[1], data[2], data[3], data[4], 0x5A };
         serialPort.Write(buffer, 0, buffer.Length);
         sendData = sendData + Converts.BytesToString(buffer);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Пример #3
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="type">类型</param>
 /// <param name="addr">地址</param>
 /// <param name="data">数据</param>
 /// <returns>状态</returns>
 public bool SendData(byte type, byte addr, byte[] data)
 {
     try
     {
         byte[] buffer = new byte[] { 0xA5, type, addr, data[0], data[1], data[2], data[3], data[4], 0x5A };
         socket.Send(buffer);
         sendData = sendData + Converts.BytesToString(buffer);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Пример #4
0
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                int    count  = this.serialPort.BytesToRead;    //数据的长度
                Byte[] buffer = new Byte[count];
                this.serialPort.Read(buffer, 0, buffer.Length); //串口接收数据

                if (buffer[0] == 0xB5 && count % 9 == 0)        //判断数据下标为0的数据是否为0xB5及长度为9的倍数
                {
                    Byte[] data = new Byte[count];
                    Array.Copy(buffer, 0, data, 0, data.Length);               //复制数据
                    receiveData  = receiveData + Converts.BytesToString(data); //将数据转化为String类型放置接收缓存区;
                    this.rawData = this.rawData + receiveData;
                    Thread.Sleep(10);
                    parseData.StartParseData();
                }
            }
            catch (Exception)
            {
            }
        }
Пример #5
0
        /// <summary>
        /// 接收数据
        /// </summary>
        private void ReceiveData()
        {
            while (true)
            {
                Thread.Sleep(1);  //线程停止1sms

                try
                {
                    Byte[] buffer = new Byte[1024];
                    Int32  count  = this.socket.Receive(buffer); //Socket接收数据

                    if (buffer[0] == 0xB5 && count % 9 == 0)     //判断数据下标为0的数据是否为0xB5及长度为9的倍数
                    {
                        Byte[] data = new Byte[count];
                        Array.Copy(buffer, 0, data, 0, data.Length);               //复制数据
                        receiveData  = receiveData + Converts.BytesToString(data); //将数据转化为String类型放置接收缓存区
                        this.rawData = this.rawData + receiveData;
                    }
                }
                catch (Exception)
                {
                }
            }
        }