CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
英特尔®游戏设计大赛100美元现金周周送 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

请问怎么在C#中给COM口发信息?

楼主bocnb(mjs)2003-06-03 20:31:38 在 .NET技术 / C# 提问

如题、不想用VB6.0的MSCOMM32.OCX控件,老是有问题。 问题点数:50、回复次数:3Top

1 楼CMIC(大象)回复于 2003-06-03 21:51:52 得分 50

权威代码  
  http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/default.aspx  
  Top

2 楼CMIC(大象)回复于 2003-06-03 21:53:47 得分 0

已经有.Net的新控件了,而且还带有源码.(微软网站)  
   
  Use   P/Invoke   to   Develop   a   .NET   Base   Class   Library   for   Serial   Device   Communications  
   
  http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/default.aspx  
   
   
  也可以直接下载这个类:  
   
  http://www.gotdotnet.com/userfiles/LoMaN/SerialStream.zip  
  Top

3 楼CMIC(大象)回复于 2003-06-03 21:55:00 得分 0

用API,参考这个例子,分两段贴出.  
   
  //----------1---------------------  
   
  using   System;    
   
  using   System.Runtime.InteropServices;    
   
   
   
  namespace   JustinIO   {    
   
          class   CommPort   {    
   
   
   
                  public   int   PortNum;    
   
                  public   int   BaudRate;    
   
                  public   byte   ByteSize;    
   
                  public   byte   Parity;   //   0-4=no,odd,even,mark,space    
   
                  public   byte   StopBits;   //   0,1,2   =   1,   1.5,   2    
   
                  public   int   ReadTimeout;    
   
   
   
                  //comm   port   win32   file   handle    
   
                  private   int   hComm   =   -1;    
   
   
   
                  public   bool   Opened   =   false;    
   
                       
   
                  //win32   api   constants    
   
                      private   const   uint   GENERIC_READ   =   0x80000000;    
   
                      private   const   uint   GENERIC_WRITE   =   0x40000000;    
   
                      private   const   int   OPEN_EXISTING   =   3;    
   
                      private   const   int   INVALID_HANDLE_VALUE   =   -1;    
   
   
   
                  [StructLayout(LayoutKind.Sequential)]    
   
                  public   struct   DCB   {    
   
                          //taken   from   c   struct   in   platform   sdk    
   
                          public   int   DCBlength;                       //   sizeof(DCB)    
   
                          public   int   BaudRate;                         //   current   baud   rate    
   
                          /*   these   are   the   c   struct   bit   fields,   bit   twiddle   flag   to   set    
   
                          public   int   fBinary;                     //   binary   mode,   no   EOF   check    
   
                          public   int   fParity;                     //   enable   parity   checking    
   
                          public   int   fOutxCtsFlow;             //   CTS   output   flow   control    
   
                          public   int   fOutxDsrFlow;             //   DSR   output   flow   control    
   
                          public   int   fDtrControl;               //   DTR   flow   control   type    
   
                          public   int   fDsrSensitivity;       //   DSR   sensitivity    
   
                          public   int   fTXContinueOnXoff;   //   XOFF   continues   Tx    
   
                          public   int   fOutX;                     //   XON/XOFF   out   flow   control    
   
                          public   int   fInX;                       //   XON/XOFF   in   flow   control    
   
                          public   int   fErrorChar;           //   enable   error   replacement    
   
                          public   int   fNull;                     //   enable   null   stripping    
   
                          public   int   fRtsControl;           //   RTS   flow   control    
   
                          public   int   fAbortOnError;       //   abort   on   error    
   
                          public   int   fDummy2;                 //   reserved    
   
                          */    
   
                          public   uint   flags;    
   
                          public   ushort   wReserved;                     //   not   currently   used    
   
                          public   ushort   XonLim;                           //   transmit   XON   threshold    
   
                          public   ushort   XoffLim;                         //   transmit   XOFF   threshold    
   
                          public   byte   ByteSize;                       //   number   of   bits/byte,   4-8    
   
                          public   byte   Parity;                           //   0-4=no,odd,even,mark,space    
   
                          public   byte   StopBits;                       //   0,1,2   =   1,   1.5,   2    
   
                          public   char   XonChar;                         //   Tx   and   Rx   XON   character    
   
                          public   char   XoffChar;                       //   Tx   and   Rx   XOFF   character    
   
                          public   char   ErrorChar;                     //   error   replacement   character    
   
                          public   char   EofChar;                         //   end   of   input   character    
   
                          public   char   EvtChar;                         //   received   event   character    
   
                          public   ushort   wReserved1;                   //   reserved;   do   not   use    
   
                  }    
   
   
   
                  [StructLayout(LayoutKind.Sequential)]    
   
                  private   struct   COMMTIMEOUTS   {    
   
                      public   int   ReadIntervalTimeout;    
   
                      public   int   ReadTotalTimeoutMultiplier;    
   
                      public   int   ReadTotalTimeoutConstant;    
   
                      public   int   WriteTotalTimeoutMultiplier;    
   
                      public   int   WriteTotalTimeoutConstant;    
   
                  }              
   
   
   
                  [StructLayout(LayoutKind.Sequential)]    
   
                  private   struct   OVERLAPPED   {    
   
                          public   int     Internal;    
   
                          public   int     InternalHigh;    
   
                          public   int     Offset;    
   
                          public   int     OffsetHigh;    
   
                          public   int   hEvent;    
   
                  }    
   
   
   
                  [DllImport("kernel32.dll")]    
   
                  private   static   extern   int   CreateFile(    
   
                      string   lpFileName,                                                   //   file   name    
   
                      uint   dwDesiredAccess,                                             //   access   mode    
   
                      int   dwShareMode,                                                     //   share   mode    
   
                      int   lpSecurityAttributes,   //   SD    
   
                      int   dwCreationDisposition,                                 //   how   to   create    
   
                      int   dwFlagsAndAttributes,                                   //   file   attributes    
   
                      int   hTemplateFile                                                 //   handle   to   template   file    
   
                  );    
   
                  [DllImport("kernel32.dll")]    
   
                  private   static   extern   bool   GetCommState(    
   
                      int   hFile,     //   handle   to   communications   device    
   
                      ref   DCB   lpDCB         //   device-control   block    
   
                  );    
   
                  [DllImport("kernel32.dll")]    
   
                  private   static   extern   bool   BuildCommDCB(    
   
                      string   lpDef,     //   device-control   string    
   
                      ref   DCB   lpDCB           //   device-control   block    
   
                  );    
   
                  [DllImport("kernel32.dll")]    
   
                  private   static   extern   bool   SetCommState(    
   
                      int   hFile,     //   handle   to   communications   device    
   
                      ref   DCB   lpDCB         //   device-control   block    
   
                  );    
   
  //-----------(1   end)------------------------  
  Top

相关问题

  • c#怎么读?
  • c怎么学?
  • C#怎么念?
  • 怎么c++
  • 怎么用Linux 下的C/C++开发一个窗口,人人有分!
  • 怎么用c语言,编写串口的通讯程序(关于com2的)这次给80分
  • JAVA怎么象C++那样隐藏自己的窗口?
  • C++Buider的MDI的主窗口怎么加上背景图?
  • 怎么做出像C++Builder界面的窗口?
  • dev c++ 的结果窗口一闪就没了,怎么看?

关键词

  • .net
  • byte
  • private

得分解答快速导航

  • 帖主:bocnb
  • CMIC

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo