首页 > 作文

C#基于SerialPort类实现串口通讯详解

更新时间:2023-04-04 21:14:03 阅读: 评论:0

本文实例为大家分享了c#基于rialport类实现串口通讯的具体代码,供大家参考,具体内容如下

最终效果

窗体设置:

using system;using system.collections.generic;using system.componentmodel;using system.data;using system.drawing;using system.linq;using system.text;using system.threading.tasks;using system.io.ports;using system.threading;using system.windows.forms;namespace rialcommunication{  public partial class form1 : form  {    system.threading.synchronizationcontext m_synccontext = null;    rialport rialport = null;    tohexadecimalstring tohexadecimalstring = new tohexadecimalstring();    public form1()    {      m_synccontext = synchronizationcontext.current;      initializecomponent();    }    //data initialization    private void form1_load(object nder, eventargs e)    {      string[] ports = rialport.getportnames();      rialportnumber.items.addrange(ports);      rialportnumber.lectedindex = rialportnumber.items.count > 0 ? 0 : -1;      baudratecom.text = "9600"; //t parameters      checkbitcom.text = "none";      databitcom.text = "8";      stopbitcom.text = "one";    }      //nd data    private void nd_click(object nder, eventargs e)    {      try      {        //nd da诚实的反义词是什么ta and handle exceptions        byte[] bytes = new byte[ndmessage.text.length];        bytes = encoding.default.getbytes(ndmessage.text);        rialport.write(bytes, 0, bytes.length);      }      catch (exce朱瑞将军生平简介ption error)      {        messagebox.show(error.message);      }    }        // byte to hexadecimal string    //clear data    private void clear_click(object nder, eventargs e)  代码查询  {      receivemessage.text = "";    }    private void open_click(object nder, eventargs e)    {      string portname = rialportnumber.text;      int buadrate = int.par(baudratecom.text);      parity parity = 0;      switch (checkbitcom.text)      {        ca "none":          parity = parity.none; break;        ca "odd":          parity = parity.odd; 为母亲喝彩break;        ca "even":          parity = parity.even; break;        ca "mark":          parity = parity.mark; break;        ca "space":          parity = parity.space; break;      }      int databit = int.par(databitcom.text);      stopbits stopbits = 0;      switch (stopbitcom.text)      {        ca "none":          stopbits = stopbits.none; break;        ca "one":          stopbits = stopbits.one; break;        ca "tow":          stopbits = stopbits.two; break;        ca "onepointfive":          stopbits = stopbits.onepointfive; break;      }      try      {        if疾首蹙额 (open.text == "open")        {          rialport = new rialport(portname, buadrate, parity, databit, stopbits);          rialport.open();          open.text = "clo";        }        el if (open.text == "clo")        {          rialport.clo();          open.text = "open";        }      }      catch (exception errror)      {        messagebox.show(errror.message);      }      rialport.datareceived += ondatareceived;    }    private void ondatareceived(object nder, rialdatareceivedeventargs e)    {      try      {        byte[] bytes = new byte[rialport.bytestoread];        rialport.read(bytes, 0, rialport.bytestoread);        m_synccontext.post(new ndorpostcallback((obj) =>        {          receivemessage.appendtext(encoding.default.getstring(bytes));        }), bytes);      }      catch (exception error)      {        messagebox.show(error.message);      }    }  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-04 21:14:01,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/ce6714fae895fd582f127e6041b0771b.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:C#基于SerialPort类实现串口通讯详解.doc

本文 PDF 下载地址:C#基于SerialPort类实现串口通讯详解.pdf

标签:疾首蹙额   本文   代码   反义词
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图