自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

  • 博客(0)
  • 资源 (17)
  • 收藏
  • 关注

空空如也

C#串口通信源代码 学习使用

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; /// <summary> /// 设置发送缓冲区大小 /// </summary> public int outBufferSize { set { sport.WriteBufferSize = value; } } /// <summary> /// 设置接收缓冲区大小 /// </summary> public int inBufferSize { set { sport.ReadBufferSize = value; } } public Com() { sport = new SerialPort(); } /// <summary> /// 初始化 /// </summary> /// <param name="portName">端口名称</param> /// <param name="rate">波特率</param> /// <param name="parity">校验方式</param> 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(); } /// <summary> /// 串口设置并打开 /// </summary> /// <param name="portName"></param> /// <param name="rate"></param> /// <param name="parity"></param> /// <returns></returns> 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; } } /// <summary> /// 发送字节 /// </summary> /// <param name="writeBytes">要发送的字节</param> /// <param name="count">发送字节的数量</param> /// <returns></returns> public bool write(byte[] writeBytes,int count) { if (InitCom()) { try { sport.Write(writeBytes, 0, count); return true; } catch (Exception e) { return false; } } return false; } /// <summary> /// 发送字符串 /// </summary> /// <param name="writestrs"></param> /// <returns></returns> public bool write(string writeStrs) { if (InitCom()) { try { sport.Write(writeStrs); System.Threading.Thread.Sleep(100); return true; } catch { return false; } } return false; } /// <summary> /// 读取数据 /// </summary> /// <param name="NumBytes">读取的字节数</param> /// <returns></returns> 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); } } } } }

2012-05-23

C#串口助手源码

C#写的串口通信助手,有助于初学者 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; /// <summary> /// 设置发送缓冲区大小 /// </summary> public int outBufferSize { set { sport.WriteBufferSize = value; } } /// <summary> /// 设置接收缓冲区大小 /// </summary> public int inBufferSize { set { sport.ReadBufferSize = value; } } public Com() { sport = new SerialPort(); } /// <summary> /// 初始化 /// </summary> /// <param name="portName">端口名称</param> /// <param name="rate">波特率</param> /// <param name="parity">校验方式</param> 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(); } /// <summary> /// 串口设置并打开 /// </summary> /// <param name="portName"></param> /// <param name="rate"></param> /// <param name="parity"></param> /// <returns></returns> 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; } } /// <summary> /// 发送字节 /// </summary> /// <param name="writeBytes">要发送的字节</param> /// <param name="count">发送字节的数量</param> /// <returns></returns> public bool write(byte[] writeBytes,int count) { if (InitCom()) { try { sport.Write(writeBytes, 0, count); return true; } catch (Exception e) { return false; } } return false; } /// <summary> /// 发送字符串 /// </summary> /// <param name="writestrs"></param> /// <returns></returns> public bool write(string writeStrs) { if (InitCom()) { try { sport.Write(writeStrs); System.Threading.Thread.Sleep(100); return true; } catch { return false; } } return false; } /// <summary> /// 读取数据 /// </summary> /// <param name="NumBytes">读取的字节数</param> /// <returns></returns> 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); } } } } }

2012-05-23

ModelSim教程大全

ModelSim教程大全,超详细讲解modelsim的使用方法!!!

2011-10-14

Verilog数字系统设计教程

Verilog数字系统设计教程Verilog数字系统设计教程

2011-10-10

verilog_hdl教程经典135例

verilog_hdl教程经典135例,太好的学习资料,不容错过!!!

2011-10-10

FPGA设计的四种常用思想与技巧

FPGA设计的四种常用思想与技巧,绝对经典,不容错过!!!

2011-10-10

ARMv7架构参考手册.pdf

ARMv7架构参考手册,学习必备资料!!!!!!!!!!!!!!

2011-03-18

proteus教程\Proteus自学教程(上册

proteus教程\Proteus自学教程(上册

2011-03-15

proteus教程\Proteus与单片机实时动态仿真

proteus教程\Proteus与单片机实时动态仿真

2011-03-15

\proteus教程\Proteus 6 Professional 入门教程

\proteus教程\Proteus 6 Professional 入门教程

2011-03-15

嵌入式学习\嵌入式2绝对好资料

嵌入式学习\嵌入式2绝对好资料!!!!!!!!!!!!!!!!!

2011-03-15

嵌入式学习\嵌入式绝好资料

嵌入式学习\嵌入式绝好资料!!!!!!!!

2011-03-15

LM3S1138(中

EasyARM1138开发指南\LM3S1138(中

2011-03-15

Proteus教程——电子线路设计·制版与仿真

protues学习的好资料,Proteus教程——电子线路设计·制版与仿真.part01.rar

2010-11-28

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除