您好,欢迎光临本网站![请登录][注册会员]  
文件名称: C#串口通信源代码
  所属分类: C#
  开发工具:
  文件大小: 51kb
  下载次数: 0
  上传时间: 2012-05-23
  提 供 者: tiank******
 详细说明: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Ports; using System.Windows.Forms; namespace SComm { public class Com { private SerialPort sport; /// /// 设置发送缓冲区大小 /// public int outBufferSize { set { sport.WriteBufferSize = value; } } /// /// 设置接收缓冲区大小 /// public i nt inBufferSize { set { sport.ReadBufferSize = value; } } public Com() { sport = new SerialPort(); } /// /// 初始化 /// /// 端口名称 /// 波特率 /// 校验方式 public Com(string portName,int rate,Parity parity) { sport = new SerialPort(portName,rate,parity); } public bool InitCom() { if (sport.IsOpen) return true; else { try { sport.Open(); return true; } catch (Exception e) { return false; } } } public void Close() { if (sport.IsOpen) sport.Close(); } /// /// 串口设置并打开 /// /// /// /// /// public bool InitCom(string portName, int rate, Parity parity) { if (sport.IsOpen) sport.Close(); sport.BaudRate = rate; sport.PortName = portName; sport.Parity = parity; try { sport.Open(); return true; } catch (Exception e) { return false; } } /// /// 发送字节 /// /// 要发送的字节 /// 发送字节的数量 /// public bool write(byte[] writeBytes,int count) { if (InitCom()) { try { sport.Write(writeBytes, 0, count); return true; } catch (Exception e) { return false; } } return false; } /// /// 发送字符串 /// /// /// public bool write(string writeStrs) { if (InitCom()) { try { sport.Write(writeStrs); System.Threading.Thread.Sleep(100); return true; } catch { return false; } } return false; } /// /// 读取数据 /// /// 读取的字节数 /// public byte[] Read(int NumBytes) { byte[] inbuffer=null; if (sport.IsOpen && sport.BytesToRead > 0) { if (NumBytes > sport.BytesToRead) NumBytes = sport.BytesToRead; try { int b = sport.ReadByte(); string s = sport.ReadExisting(); string s1 = sport.NewLine; // string ss = sport.ReadLine(); inbuffer = new byte[NumBytes]; int count = sport.Read(inbuffer, 0, NumBytes); } catch (TimeoutException) { throw; } } // sport.Close(); return inbuffer; } public byte[] Read() { return Read(sport.BytesToRead); } public string ReadLine() { try { if (sport.IsOpen && sport.BytesToRead > 0) { string s = sport.ReadExisting(); return sport.ReadLine(); } return null; } catch (TimeoutException e) { return e.Message; } } public string ReadExisting() { return sport.ReadExisting(); } public void SendSignal(byte cmd) { byte[] hexdata = new byte[5]; hexdata[0] = 0x01; hexdata[1] = 0x03; hexdata[2] = cmd; ushort crc = CommWithARM. CRC_16(hexdata, 3); hexdata[3] = (byte)(crc / 256); hexdata[4] = (byte)(crc % 256); write(hexdata, 5); } public void OnOnCommMscomm1() { byte[] rxdata= Read(); ////string srxdata = ReadLine(); int i = 0; try ////{ //// foreach (char s in srxdata) //// { //// rxdata[i++] = (byte)s; //// } ////} ////catch { } if (rxdata != null) { int len = rxdata.Length; if (len == 23) { CommWithARM.CheckHandSignal(rxdata); } else if (len >= 53) { CommWithARM.HandData(rxdata, len); } } } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: C# 代码
 输入关键字,在本站1000多万海量源码库中尽情搜索: