C#四路继电器usb转com口编程

该博客介绍了如何使用C#的System.IO.Ports.SerialPort类来控制四路继电器,通过USB转COM口进行通信。在代码中,创建了一个EleRelay类,包含了设置继电器状态的指令,以及打开、关闭串口的方法。通过定时器实现继电器的闪烁功能,并处理可能的通信错误。
摘要由CSDN通过智能技术生成
用到C#在名字空间System.IO.Ports下的SerialPort类来实现功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.IO.Ports;

namespace ComLibrary
{
    public class EleRelay
    {
        #region 四路继电器属性区域
        private SerialPort mycom = new SerialPort();    //com接口
        private string portName;    //com号
        private bool flag = false;
        private int numFlashing;
        //设置几号灯闪烁
        public int NumFlashing
        {
            set { this.numFlashing = value; }
            get { return this.numFlashing; }
        }
        public Timer tickTimer;
        public string PortName
        {
            set { this.portName = value; }
            get { return this.portName; }
        }
        //控制开关量的指令
        string[] str = {
                           "5501010200000059",
                           "5501010100000058",
                           "
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值