当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现把指定数据写入串口

C#实现把指定数据写入串口

2019年07月18日  | 移动技术网IT编程  | 我要评论
    public static bool writetoserialport(byte[]bytearr)
    {
      serialport com = newserialport();
      try
      {
        com.readtimeout = 5000;
        com.writetimeout = 5000;
        com.portname = "com1";
        com.baudrate = 9600;
        com.stopbits = stopbits.one;
        com.parity = parity.none;
        com.open();
        com.write(bytearr, 0,bytearr.length);
        return true;
      }
      catch(exception ex)
      {
        return false;
      }
      finally
      {
        com.close();
      }      
    }

本例使用的是方法write(byte[]buffer, int32 offset, int32 count)。该方法使用缓冲区的数据将指定数量的字节写入串行端口。buffer为缓冲区,offset表示从此处开始将字节复制到端口,count表示要写入的字节数。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网