p/invoke,串口通信,事件

先bs一下某网站blog,连附件都不能传送, 要不是在某地几年念旧,还真的搬移到他处去啦....

1.1的时候还框架类库还没有serialport这类,还必须使用开源项目,后来2.0的serialport想必估计也参考了这里,因为看了一下其接口基本类似.由于serialport提供了事件响应而且.net也支持event,所以完全可以使用事件驱动的方式,一层一层的向上进行响应,而不必采用 while(true) 之类方式,在复杂情况下极其不易调试.当初封装此模块时也恰逢和项目组其他人争吵,偶执意要不使用死循环方式进行进行串口数据的监听,后来还加入了多线程来处理事件重复触发的情况等.

要在unix linux平台上可没办法这样搞了,貌似没有事件机制,还真不知道怎么搞,难道只能用死循环+sleep这种.....

信号量也可以自定义,还真不知道能否和event达到同样的效果...

.......

  private InteropCardOperations()
  {
   //获取串口通讯对象
   interopCommOprations = InteropCommOprations.GetIneropCommOprInstance();
   //绑定事件
   interopCommOprations.RecvCardSN += new NCharge.InteropServices.InteropCommOprations.RecvCardSNHandler(interopCommOprations_RecvCardSN);
   interopCommOprations.RecvReaderProp +=new NCharge.InteropServices.InteropCommOprations.RecvReaderPropHandler(interopCommOprations_RecvReaderProp);
   interopCommOprations.RecvWriteResult +=new NCharge.InteropServices.InteropCommOprations.RecvWriteResultHandler(interopCommOprations_RecvWriteResult);
   interopCommOprations.RecvBeepResult += new NCharge.InteropServices.InteropCommOprations.RecvBeepResultHandler(interopCommOprations_RecvBeepResult);
   interopCommOprations.RecvReadSector += new NCharge.InteropServices.InteropCommOprations.RecvReadSectorHandler(interopCommOprations_RecvReadSector);
   //初始化同步对象
   myManualEvent = new ManualResetEvent(false);
   //初始化转化对象
   cardAppConvert = new CardAppConvertService();
   //初始化log4net
   log4net.Config.DOMConfigurator.Configure();
   log = log4net.LogManager.GetLogger("MyLogger");
  }

 

...........

 

  /// <summary>
  /// 根据操作抛出对应的事件
  /// </summary>
  /// <param name="sendCmd"></param>
  private void InvokeExactEvent(int sendCmd)
  {
   ReceiveCommEventArgs recv = null;
   byte[] returnBytes = new byte[receivedBytes.Length];
   Array.Copy(receivedBytes,0,returnBytes,0,receivedBytes.Length);
   receivedBytes = null;
   switch(sendCmd)
   {
    case InteropSendCmd.HD_SEND_CARDID:
     recv = new ReceiveCommEventArgs(returnBytes,objUpDataStruct,InteropSendCmd.HD_SEND_CARDID);
     log.Debug("invoke recv cardSn event!");
     this.OnRecvCardSN(recv);
     break;
    case InteropSendCmd.HD_RECV_READERPROP:
     recv = new ReceiveCommEventArgs(returnBytes,objUpDataStruct,InteropSendCmd.HD_RECV_READERPROP);
     log.Debug("invoke recv readerProp event!");
                    this.OnRecvReaderProp(recv);
     break;
    case InteropSendCmd.HD_BEEP:
     recv = new ReceiveCommEventArgs(returnBytes,objUpDataStruct,InteropSendCmd.HD_RECV_READERPROP);
     log.Debug("invoke recv beepResult event!");
     this.OnRecvBeepResult(recv);
     break;
    case InteropSendCmd.HD_WRITE_SECTOR:
     log.Debug("invoke recv writeResult event!");
     recv = new ReceiveCommEventArgs(returnBytes,objUpDataStruct,InteropSendCmd.HD_WRITE_SECTOR);
     this.OnRecvWriteResult(recv);
     break;
    case InteropSendCmd.HD_WRITE_DISCRETESECTOR:
     recv = new ReceiveCommEventArgs(returnBytes,objUpDataStruct,InteropSendCmd.HD_WRITE_DISCRETESECTOR);
     this.OnRecvWriteResult(recv);
     break;
    case InteropSendCmd.HD_WRITE_DISCRETEBLOCK:
     log.Debug("invoke recv write_Discreteblock event!");
     recv = new ReceiveCommEventArgs(returnBytes,objUpDataStruct,InteropSendCmd.HD_WRITE_DISCRETEBLOCK);
     this.OnRecvWriteResult(recv);
     break;
    case InteropSendCmd.HD_READ_SECTOR:
     recv = new ReceiveCommEventArgs(returnBytes,objUpDataStruct,InteropSendCmd.HD_READ_SECTOR);
     this.OnRecvReadSector(recv);
     break;
   }
  }

.........


  [DllImport(conConversion, SetLastError=true), SuppressUnmanagedCodeSecurity]
  internal static extern bool Initialization(int MyAddr, int TagAddr);

  [DllImport(conConversion, SetLastError=true), SuppressUnmanagedCodeSecurity]
  internal static extern int Conversion(ref int Command, [MarshalAs(UnmanagedType.AsAny)] Object objDataStruct, int nSizeOfParameter);

  [DllImport(conConversion, SetLastError=true), SuppressUnmanagedCodeSecurity]
  internal static extern int Conversion(ref int Command, IntPtr buffer, int nSizeOfParameter);

  [DllImport(conConversion, SetLastError=true), SuppressUnmanagedCodeSecurity]
  internal static extern int GetSizeOfData();

  [DllImport(conConversion, SetLastError=true), SuppressUnmanagedCodeSecurity]

.........

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值