上位机-串口通信-C#

上位机-串口通信-C#

一.   新建项目

二.   在组件中 拖拽serialPort空间


这样就默认产生了serialPort1对象。在串口的属性栏里面设置串口属性


设置串口属性的时候最好是在程序里面设置,用代码实现。直接在置属性可能会使串口的DateReceivd事件无法触发,(具体为什么我也不知道,高手们可以帮忙看看)这是亲身体验啊~~找了好久,都不知道问题在哪里。得出结论,最保险的方法还是用代码实现串口的属性设置。

具体代码如下:

(不要忘记导入命名空间usingSystem.IO.Ports;  否则会有错)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO.Ports;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        publicForm1()

        {

            InitializeComponent();

 

            serialPort1.ReceivedBytesThreshold= 1;

            serialPort1.BaudRate = 9600;

            serialPort1.PortName = "COM1";

            serialPort1.Parity = Parity.Even;

            serialPort1.DataBits = 8;

            serialPort1.StopBits = StopBits.One;

        }

    }

}

 

三 、打开,关闭串口

serialPort类封装了 close();和open();方法 调用close方法时关闭端口连接,将isopen属性设置为false,并且释放内部的stream对象。调用open时打开

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值