我们知道对于

标准DLL,可以采用DllImport进行调用。例如:

[DllImport("KMY350X.dll")]

private static extern int OpenPort(int PortNum, int BaudRate);

如果一些厂家比较懒的话,没有提供相应的dll,我们只能对它进行串口通信编程了。以前从没接触过串口编程,最近在一个项目中有几个地方都需要采用串口通信,跟公司一个老手请教后,感觉学到了很多东西,特在此做个总结:

一:首先我们来认识下什么是串口:

右键 我的电脑-管理-设备管理器-端口,选择一个端口,点击属性。

我们可以看到该串口的属性,在C#中我们使用SerialPort类来表示串口

ConfigClass config = new ConfigClass();

comm.serialPort.PortName = config.ReadConfig("SendHealCard");

//波特率

comm.serialPort.BaudRate = 9600;

//数据位

comm.serialPort.DataBits = 8;

//两个停止位

comm.serialPort.StopBits = System.IO.Ports.StopBits.One;

//无奇偶校验位

comm.serialPort.Parity = System.IO.Ports.Parity.None;

comm.serialPort.ReadTimeout = 100;

comm.serialPort.WriteTimeout = -1;

二:串口调试工具:

在对串口进行编程时候,我们要向串口发送指令,然后我们解析串口返回的指令。在这里向大家推荐一款工具。

将要发送的指令用空格隔开,选择HEX显示为放回的字符串。

三:正式编程:

编写Comm类:

public class Comm

{

public delegate void EventHandle(byte[] readBuffer);

public event EventHandle DataReceived;

public SerialPort serialPort;

Thread thread;

volatile bool _keepReading;

public Comm()

{

serialPort = new SerialPort();

thread = null;

_keepReading = false;

}

public bool IsOpen

{

get

{

return serialPort.IsOpen;

}

}

private void StartReading()

{

if (!_keepReading)

{

_keepReading = true;

thread = new Thread(new ThreadStart(ReadPort));

thread.Start();

}

}

private void StopReading()

{

if (_keepReading)

{

_keepReading = false;

thread.Join();

thread = null;

}

}

private void ReadPort()

{

while (_keepReading)

{

if (serialPort.IsOpen)

{

int count = serialPort.BytesToRead;

if (count > 0)

{

byte[] readBuffer = new byte[count];

try

{

Application.DoEvents();

serialPort.Read(readBuffer, 0, count);

if(DataReceived != null)

DataReceived(readBuffer);

Thread.Sleep(100);

}

catch (TimeoutException)

{

}

}

}

}

}

public void Open()

{

Close();

serialPort.Open();

if (serialPort.IsOpen)

{

StartReading();

}

else

{

MessageBox.Show("串口打开失败!");

}

}

public void Close()

{

StopReading();

serialPort.Close();

}

public void WritePort(byte[] send, int offSet, int count)

{

if (IsOpen)

{

serialPort.Write(send, offSet, count);

}

}

}

注册串口:

Comm

comm = new Comm();

ConfigClass config = new ConfigClass();

comm.serialPort.PortName = config.ReadConfig("SendHealCard");

//波特率

comm.serialPort.BaudRate = 9600;

//数据位

comm.serialPort.DataBits = 8;

//两个停止位

comm.serialPort.StopBits = System.IO.Ports.StopBits.One;

//无奇偶校验位

comm.serialPort.Parity = System.IO.Ports.Parity.None;

comm.serialPort.ReadTimeout = 100;

comm.serialPort.WriteTimeout = -1;

comm.Open();

if (comm.IsOpen)

{

comm.DataReceived += new Comm.EventHandle(comm_DataReceived);

}

发送指令:

///

/// 发卡到机口

///

private void SendCardToOut()

{

is_read_card = false;

sendCardToOut = true;

byte[] send = { 0x02, 0x46, 0x43, 0x34, 0x03, 0x30 };

if (comm.IsOpen)

{

comm.WritePort(send, 0, send.Length);

}

}

收到指令,并解析:

void comm_DataReceived(byte[] readBuffer1)

{

//log.Info(HexCon.ByteToString(readBuffer));

if (readBuffer1.Length == 1)

{

receive = HealCardClass.ByteToString(readBuffer1);

string str = "06";

if (string.Equals(receive.Trim(), str, StringComparison.CurrentCultureIgnoreCase))

{

try

{

if (is_read_card)

{

byte[] send = new byte[1];

send[0] = 0x05;

comm.WritePort(send, 0, send.Length);

Thread.Sleep(500);

comm.DataReceived -= new Comm.EventHandle(comm_DataReceived);

InitReadComm();

}

if (sendCardToOut)

{

byte[] send = new byte[1];

send[0] = 0x05;

comm.WritePort(send, 0, send.Length);

readComm.DataReceived -= new Comm.EventHandle(readComm_DataReceived);

readComm.Close();

log.Info("发卡完成!");

lblMsg.Text = "发卡成功!";

lblSendCardMsg.Text = "发卡完成,请收好卡!";

timer1.Tick -= new EventHandler(timer1_Tick);

PlaySound();

this.btnOK.Enabled = true;

}

}

catch (Exception ex)

{

log.Info(ex.ToString());

}

}

}

}

至此,串口通信编程告一段落

c# wifi串口通信_C# 串口通信总结相关推荐

  1. serialport接收串口数据_C#串口操作类,包括串口读写操作

    串口进行操作的类,其中包括写和读操作,类可设置串口参数.设置接收函数.打开串口资源.关闭串口资源,操作完成后,一定要关闭串口.接收串口数据事件.接收数据出错事件.获取当前全部串口.把字节型转换成十六进 ...

  2. php和javascript的get和post方式 有人串口转wifi模块httpdclient网页交互通信成功源码2 wifi继电器小黄人软件ypnr

    全部源码下载:链接: http://pan.baidu.com/s/1qXxr0i4 密码: 有人串口转wifi模块 httpd client通信示例-用户使用网页通过服务器收发串口数据get 功能: ...

  3. 串口通信及串口转蓝牙相关知识

    之前没有接触过硬件相关的工作, 因此对硬件的知识一知半解. 最近由于项目需要, 用到了串口通信以及串口跟蓝牙之间通信相关的东西.记录下来, 希望对新手有所帮助. 如有疏漏之处, 欢迎指正. 1 串口通 ...

  4. c语言串口通信_stm32 串口通信收发说明

    很多网友在stm32 串口通信收发中,发现接收发送函数中,数据都是指针指向首字符的字符串中,下一步进行后续操作就非常吃力了.其实这是对C语言指针的不能熟练应用有关.指针是C语言的灵魂,也是最难的部分, ...

  5. android spi串口调试,PIC入门3,SPI通信和串口调试实验

    原标题:PIC入门3,SPI通信和串口调试实验 MSSP模块工作于SPI主控方式,这个可以直接在实验板上执行. 程序: //适合3EPIC实验板,配置PIC的MSSP模块工作于SPI主控方式下, // ...

  6. s7300plc串口通信_s7-300串口通讯.ppt

    s7-300串口通讯 串口通信特点 1:点到点连接 2:串形通信 信息字节的每个位按照固定的次序一个连接一个传输 3:通信双方约定统一的传输速率.数据位.奇偶校验.停止位 4:半双工.全双工操作 串口 ...

  7. cubemx stm32 陶晶驰 串口屏 基于YXY通信原理的串口屏驱动代码

    陶晶驰串口屏 资料 陶晶驰串口屏是本质是一个MCU,屏幕是MCU的模块,一般是一块TFT屏幕.在串口屏厂商提供的软件上面编写界面,然后通过串口直接烧到串口屏的MCU上,然后在屏幕上展示出来. 串口屏资 ...

  8. STM32开发,串口和PC机通信(串口中断、FIFO机制),安富莱+正点原子程序合并

    STM32开发,串口和PC机通信(串口中断.FIFO机制),安富莱+正点原子程序合并 1 概述 1.1 资源概述 1.2 实现功能 2 软件实现 2.1实现步骤 2.2 main()函数代码 2.3 ...

  9. 【genius_platform软件平台开发】第九十三讲:串口通信(485通信)

    485通信 1. 485通信 1.1 termios结构 1.2 头文件 1.3 函数讲解 1.3.1 tcgetattr 1.3.2 tcsetattr 1.4 示例工程 1.5 参考文献 1.5. ...

最新文章

  1. html5的在线播放页面,整理5款html5网页播放器,总有一款适合你吧
  2. linux如何设置awesome字体,Awesome简单配置
  3. python 通过 实例方法 名字的字符串调用方法
  4. C语言指针实现计算平均分等功能
  5. android 用dx.bat 转换class 为dex
  6. 垃圾收集算法与垃圾收集器
  7. Linux课程笔记 Crond介绍
  8. Java ArrayList与LinkedList数据结构和特点
  9. python字符串前缀
  10. 笔记本 续航测试软件,续航测试:较高强度运行状态_笔记本评测-中关村在线
  11. 日本日野汽车因尾气数据造假问题受到调查
  12. KVM虚拟化平台的基础知识及搭建部署!
  13. 2021-11-05 springMVC 错误:org.springframework.http.converter.HttpMessageNotWritableException No conve
  14. 再来学习一下“八荣八耻”
  15. IPv6进阶:IPv6 过渡技术之 NAT64(IPv6 节点主动访问 IPv4 节点-地址池方式)
  16. API接口名称(包括在请求地址中)[item_search,item_get,item_search_shop等] item_get - 根据ID取商品详情
  17. mysql 只显示第一条记录_MySQL:此种查询结果,怎么仅保留第一条记录?
  18. 4K Video Downloader (4K超高清视频下载器)V6.1.46版本发布
  19. TestStand中,ProcessSetup中的fileGlobal没有传递到MainSequence中
  20. 前端中关于下载文件问题

热门文章

  1. 95-130-348-源码-source-kafka相关-Handover
  2. 【Flink】Flink时间之internalTimerService初始化
  3. Mac安装telnet
  4. Maven多模块打包遇到的问题详解
  5. eclipse中查怎样看某个方法调用了谁
  6. Gradle 比 Maven 好为什么用的人少
  7. 你写的代码扩展性高吗?快试试用Spring注入方式来解耦代码!
  8. 干掉MySQL:他们的MySQL分库分表架构,搞得太棒了!
  9. Java多线程学习八:线程池常用的阻塞队列
  10. SpringMVC实现文件上传