Ejemplo n.º 1
0
 protected virtual void OnDataReceived(report e)
 {
     if (DataReceived != null)
     {
         DataReceived(this, e);
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="buffer"></param>
 /// <returns></returns>
 public HID_RETURN Write(report r)
 {
     if (deviceOpened)
     {
         try
         {
             byte[] buffer = new byte[outputReportLength];
             buffer[0] = r.reportID;
             int maxBufferLength = 0;
             if (r.reportBuff.Length < outputReportLength - 1)
             {
                 maxBufferLength = r.reportBuff.Length;
             }
             else
             {
                 maxBufferLength = outputReportLength - 1;
             }
             for (int i = 1; i <= maxBufferLength; i++)
             {
                 buffer[i] = r.reportBuff[i - 1];
             }
             hidDevice.Write(buffer, 0, OutputReportLength);
         }
         catch
         {
             EventArgs ex = new EventArgs();
             OnDeviceRemoved(ex);//发出设备移除消息
         }
     }
     return(HID_RETURN.WRITE_FAILD);
 }
 /// <summary>
 /// 异步读取结束,发出有数据到达事件
 /// </summary>
 /// <param name="iResult">这里是输入报告的数组</param>
 private void ReadCompleted(IAsyncResult iResult)
 {
     byte[] readBuff = (byte[])(iResult.AsyncState);
     try
     {
         if (deviceOpened == true)
         {
             hidDevice.EndRead(iResult);//读取结束,如果读取错误就会产生一个异常
             byte[] reportData = new byte[readBuff.Length - 1];
             for (int i = 1; i < readBuff.Length; i++)
             {
                 reportData[i - 1] = readBuff[i];
             }
             report e = new report(readBuff[0], reportData);
             OnDataReceived(e);   //发出数据到达消息
             BeginAsyncRead();    //启动下一次读操作
             EventArgs ex = new EventArgs();
             OnDataProcessed(ex); //发出数据已处理完成消息
         }
     }
     catch //读写错误,设备可能已经被移除
     {
         EventArgs ex = new EventArgs();
         OnDeviceRemoved(ex);//发出设备移除消息
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="buffer"></param>
 /// <returns></returns>
 public HID_RETURN Write(report r)
 {
     if (deviceOpened)
     {
         try
         {
             //增加超出长度时的处理规则
             int nByteCount = r.reportBuff.Length;
             int nPos       = 0;
             while (nPos < nByteCount)
             {
                 byte[] chunk = new byte[outputReportLength];
                 chunk[0] = r.reportID;
                 for (int i = 1; i < outputReportLength; i++)
                 {
                     if (nPos < nByteCount)
                     {
                         chunk[i] = r.reportBuff[nPos++];
                     }
                     else
                     {
                         chunk[i] = 0;
                     }
                 }
                 hidDevice.Write(chunk, 0, OutputReportLength);
                 hidDevice.Flush();
                 Thread.Sleep(5);
             }
             return(HID_RETURN.SUCCESS);
         }
         catch
         {
             if (OnSpecifiedDeviceRemoved != null)
             {
                 OnSpecifiedDeviceRemoved(this, new EventArgs());
             }
             CloseDevice();
         }
     }
     else
     {
         if (OnSpecifiedDeviceRemoved != null)
         {
             OnSpecifiedDeviceRemoved(this, new EventArgs());
         }
         CloseDevice();
     }
     return(HID_RETURN.WRITE_FAILD);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 异步读取结束,发出有数据到达事件
 /// </summary>
 /// <param name="iResult">这里是输入报告的数组</param>
 private void ReadCompleted(IAsyncResult iResult)
 {
     byte[] readBuff = (byte[])(iResult.AsyncState);
     try
     {
         hidDevice.EndRead(iResult);//读取结束,如果读取错误就会产生一个异常
         byte[] reportData = new byte[readBuff.Length - 1];
         Buffer.BlockCopy(readBuff, 1, reportData, 0, reportData.Length);
         report e = new report(readBuff[0], reportData);
         OnDataReceived(e); //发出数据到达消息
         BeginAsyncRead();  //启动下一次读操作
     }
     catch (Exception e)    //读写错误,设备已经被移除
     {
         EventArgs ex = new EventArgs();
         OnDeviceRemoved(ex);//发出设备移除消息
         CloseDevice();
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 异步读取结束,发出有数据到达事件
 /// </summary>
 /// <param name="iResult">这里是输入报告的数组</param>
 private void ReadCompleted(IAsyncResult iResult)
 {
     byte[] readBuff = (byte[])(iResult.AsyncState);
     try
     {
         hidDevice.EndRead(iResult);//读取结束,如果读取错误就会产生一个异常
         byte[] reportData = new byte[readBuff.Length - 1];
         for (int i = 1; i < readBuff.Length; i++)
         {
             reportData[i - 1] = readBuff[i];
         }
         report e = new report(readBuff[0], reportData);
         OnDataReceived(e); //发出数据到达消息
         BeginAsyncRead();  //启动下一次读操作
     }
     catch (IOException e)  //读写错误,设备已经被移除
     {
         EventArgs ex = new EventArgs();
         OnDeviceRemoved(ex);//发出设备移除消息
         CloseDevice();
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// 重载写入方法.
        /// </summary>
        /// <param name="context"></param>
        public void Write(string context)
        {
            report data = new report(0x00, Encoding.Default.GetBytes(context));

            Write(data);
        }