CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

用C#做串口通讯!如何入门?大哥们指点一下!119分感谢!

楼主50277(柳影随风)2004-12-01 16:59:54 在 .NET技术 / C# 提问

从没做过串口通讯,不知道如何入门?请大家推荐一些好书,资料   什么的做入门教程! 问题点数:119、回复次数:18Top

1 楼muse2008(沉思)回复于 2004-12-01 17:18:18 得分 9

你上网找一下MSComm控件吧,也会找到一些相关的文章的。Top

2 楼hujiiori(Coder×Coder——sytu)回复于 2004-12-01 17:23:19 得分 0

upTop

3 楼fun1984(funy)回复于 2004-12-01 17:26:19 得分 50

写串口通讯的C#程序建议用下面   的一个通用类  
  http://blog.csdn.net/fun1984/archive/2004/08/04/60663.aspx  
  他是把kernel32.dll中用于串口通讯的api转到了C#  
  可以直接调用  
  我这里有实例,如果需要给出你   的email吧Top

4 楼diaoerlangdang()回复于 2004-12-01 17:34:13 得分 0

俺要mengli1198@sina.com.cnTop

5 楼nga96(因为我笨,所以努力。陈勇华)回复于 2004-12-01 17:43:21 得分 0

俺也要nga96@163.comTop

6 楼waximi(阿猫)回复于 2004-12-01 18:00:36 得分 50

using   System;  
  using   System.Runtime.InteropServices;  
   
  namespace   Gccs.Messages   {  
  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   int   fRtsControl;           //   RTS   flow   control    
  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  
  );  
  [DllImport("kernel32.dll")]  
  private   static   extern   bool   GetCommTimeouts(  
      int   hFile,                                     //   handle   to   comm   device  
      ref   COMMTIMEOUTS   lpCommTimeouts     //   time-out   values  
  );  
  [DllImport("kernel32.dll")]  
  private   static   extern   bool   SetCommTimeouts(  
      int   hFile,                                     //   handle   to   comm   device  
      ref   COMMTIMEOUTS   lpCommTimeouts     //   time-out   values  
  );  
  [DllImport("kernel32.dll")]  
  private   static   extern   bool   ReadFile(  
      int   hFile,                                 //   handle   to   file  
      byte[]   lpBuffer,                           //   data   buffer  
      int   nNumberOfBytesToRead,     //   number   of   bytes   to   read  
      ref   int   lpNumberOfBytesRead,   //   number   of   bytes   read  
      ref   OVERLAPPED   lpOverlapped         //   overlapped   buffer  
  );  
  [DllImport("kernel32.dll")]  
  private   static   extern   bool   WriteFile(  
      int   hFile,                                         //   handle   to   file  
      byte[]   lpBuffer,                                 //   data   buffer  
      int   nNumberOfBytesToWrite,           //   number   of   bytes   to   write  
      ref   int   lpNumberOfBytesWritten,     //   number   of   bytes   written  
      ref   OVERLAPPED   lpOverlapped                 //   overlapped   buffer  
  );  
  [DllImport("kernel32.dll")]  
  private   static   extern   bool   CloseHandle(  
      int   hObject       //   handle   to   object  
  );  
   
  [DllImport("kernel32.dll")]  
  private   static   extern   int   MultiByteToWideChar(  
  int           CodePage,  
  int         dwFlags,  
  string       lpMultiByteStr,  
  int             cchMultiByte,  
  ref   byte[]       lpWideCharStr,  
  int             cchWideChar);  
  [DllImport("kernel32.dll")]  
  private   static   extern   int   WideCharToMultiByte(  
  int           CodePage,  
  int         dwFlags,  
  byte[]     lpWideCharStr,  
  int             cchWideChar,  
  ref   byte[]         lpMultiByteStr,  
  int             cchMultiByte,  
  string       lpDefaultChar,  
  string       lpUsedDefaultChar);  
   
   
  [DllImport("kernel32.dll")]  
  private   static   extern   uint   GetLastError();  
   
  public   int     MBTW(int   CodePage,int   dwFlags,string   lpMultiByteStr,int   cchMultiByte,ref   byte[]   lpWideCharStr,int   cchWideChar){  
  return   MultiByteToWideChar(CodePage,dwFlags,lpMultiByteStr,cchMultiByte,   ref   lpWideCharStr,cchWideChar);    
  }  
   
  public   int   WTMB(int     CodePage,int   dwFlags,byte[]     lpWideCharStr,int   cchWideChar,ref   byte[]     lpMultiByteStr,int   cchMultiByte,string   lpDefaultChar,string   lpUsedDefaultChar)  
  {  
  return   WideCharToMultiByte(   CodePage,dwFlags,lpWideCharStr,cchWideChar,   ref   lpMultiByteStr,cchMultiByte,lpDefaultChar,lpUsedDefaultChar);  
   
  }  
   
  Top

7 楼waximi(阿猫)回复于 2004-12-01 18:00:43 得分 0

public   void   Open()   {  
     
    DCB   dcbCommPort   =   new   DCB();  
    COMMTIMEOUTS   ctoCommPort   =   new   COMMTIMEOUTS();  
     
     
      //   OPEN   THE   COMM   PORT.  
   
       
      hComm   =   CreateFile("COM"   +   PortNum   ,GENERIC_READ   |   GENERIC_WRITE,0,   0,OPEN_EXISTING,0,0);  
   
      //   IF   THE   PORT   CANNOT   BE   OPENED,   BAIL   OUT.  
      if(hComm   ==   INVALID_HANDLE_VALUE)   {  
      throw(new   ApplicationException("Comm   Port   Can   Not   Be   Opened"));  
      }  
   
      //   SET   THE   COMM   TIMEOUTS.  
   
      GetCommTimeouts(hComm,ref   ctoCommPort);  
      ctoCommPort.ReadTotalTimeoutConstant   =   ReadTimeout;  
      ctoCommPort.ReadTotalTimeoutMultiplier   =   0;  
      ctoCommPort.WriteTotalTimeoutMultiplier   =   0;  
      ctoCommPort.WriteTotalTimeoutConstant   =   0;      
      SetCommTimeouts(hComm,ref   ctoCommPort);  
   
      //   SET   BAUD   RATE,   PARITY,   WORD   SIZE,   AND   STOP   BITS.  
      GetCommState(hComm,   ref   dcbCommPort);  
      dcbCommPort.BaudRate=BaudRate;  
      dcbCommPort.flags=0;  
                    //   dcbCommPort.fRtsControl=1;  
      //dcb.fBinary=1;  
      dcbCommPort.flags|=1;  
      if   (Parity>0)  
      {  
        //dcb.fParity=1  
              dcbCommPort.flags|=2;  
      }  
      dcbCommPort.Parity=Parity;  
      dcbCommPort.ByteSize=ByteSize;  
      dcbCommPort.StopBits=StopBits;  
      if   (!SetCommState(hComm,   ref   dcbCommPort))  
      {  
    //uint   ErrorNum=GetLastError();  
            throw(new   ApplicationException("Comm   Port   Can   Not   Be   Opened"));  
      }  
      //unremark   to   see   if   setting   took   correctly  
      //DCB   dcbCommPort2   =   new   DCB();  
      //GetCommState(hComm,   ref   dcbCommPort2);  
      Opened   =   true;  
       
  }  
   
  public   void   Close()   {  
  if   (hComm!=INVALID_HANDLE_VALUE)    
  {  
  CloseHandle(hComm);  
  }  
   
  }  
   
  public   byte[]   Read(int   NumBytes)   {  
  byte[]   BufBytes;  
  byte[]   OutBytes;  
  BufBytes   =   new   byte[NumBytes];  
  if   (hComm!=INVALID_HANDLE_VALUE)   {  
  OVERLAPPED   ovlCommPort   =   new   OVERLAPPED();  
  int   BytesRead=0;  
  ReadFile(hComm,BufBytes,NumBytes,ref   BytesRead,ref   ovlCommPort);  
  OutBytes   =   new   byte[BytesRead];  
  Array.Copy(BufBytes,OutBytes,BytesRead);  
  }    
  else   {  
  throw(new   ApplicationException("Comm   Port   Not   Open"));  
  }  
  return   OutBytes;  
  }  
   
  public   void   Write(byte[]   WriteBytes)   {  
  if   (hComm!=INVALID_HANDLE_VALUE)   {  
  OVERLAPPED   ovlCommPort   =   new   OVERLAPPED();  
  int   BytesWritten   =   0;  
  WriteFile(hComm,WriteBytes,WriteBytes.Length,ref   BytesWritten,ref   ovlCommPort);  
   
  }  
  else   {  
  throw(new   ApplicationException("Comm   Port   Not   Open"));  
  }  
  }  
  }  
   
  class   HexCon   {  
  //converter   hex   string   to   byte   and   byte   to   hex   string  
  public   static   string   ByteToString(byte[]   InBytes)   {  
  string   StringOut="";  
  foreach   (byte   InByte   in   InBytes)   {  
  StringOut=StringOut   +   String.Format("{0:X2}   ",InByte);  
  }  
  return   StringOut;    
  }  
  public   static   byte[]   StringToByte(string   InString)   {  
  string[]   ByteStrings;  
  ByteStrings   =   InString.Split("   ".ToCharArray());  
  byte[]   ByteOut;  
  ByteOut   =   new   byte[ByteStrings.Length-1];  
  for   (int   i   =   0;i==ByteStrings.Length-1;i++)   {  
  ByteOut[i]   =   Convert.ToByte(("0x"   +   ByteStrings[i]));  
  }    
  return   ByteOut;  
  }  
  }  
  }Top

8 楼waximi(阿猫)回复于 2004-12-01 18:01:57 得分 0

该程序能单独编译通过,调用该程序的函数就可以实现串口操作了。我在此基础上做了短消息的发送与接收功能  
  Top

9 楼hel(抵制日货,从我做起)回复于 2004-12-01 18:09:38 得分 0

C#2.0framework   包含串口通讯控件。Top

10 楼an_andy()回复于 2004-12-02 10:28:33 得分 10

http://blog.csdn.net/fun1984/archive/2004/08/04/60663.aspx  
  Top

11 楼abctop321(开始编程序)回复于 2004-12-02 10:34:21 得分 0

偶也要  
  rz6315326@126.comTop

12 楼50277(柳影随风)回复于 2004-12-02 20:23:08 得分 0

谢谢!代码还没来得及看!先谢谢了!结帐!Top

13 楼50277(柳影随风)回复于 2004-12-02 20:23:32 得分 0

50277@163.comTop

14 楼lookool()回复于 2004-12-03 10:34:33 得分 0

你好:我需要这个串口通讯的C#通用类的实例,请寄给我!  
  lookool@tom.comTop

15 楼XhmDIY(学习..)回复于 2004-12-13 16:38:44 得分 0

xhm200312@163.com  
  谢谢Top

16 楼Yans(跟贴是一种友谊)回复于 2005-05-29 14:55:37 得分 0

toyan@tom.com  
   
  谢谢Top

17 楼lidan9980(如果)回复于 2005-09-08 08:52:27 得分 0

lidan9980@163.com  
  谢谢Top

18 楼Nemocat(凉风有信)回复于 2005-09-08 16:20:33 得分 0

fengcao1982@126.com  
   
  万分感谢Top

相关问题

  • c# 串口通讯
  • 求教C#串口通讯
  • C#如何实现串口通讯
  • 一段串口通讯的代码,求高手指点!
  • 用纯C语言编写串口通讯的程序
  • 急需串口通讯开发包,C++、Delphi均可以
  • 如何用C++对串口通讯编程!
  • turbo c 下的网络串口通讯哪位做过?
  • 关于用C#做PDA的串口通讯
  • 串口通讯协议的分析问题,希望高手指点迷津)

关键词

  • .net
  • c#
  • 串口通讯
  • private const
  • byte

得分解答快速导航

  • 帖主:50277
  • muse2008
  • fun1984
  • waximi
  • an_andy

相关链接

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

广告也精彩

反馈

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