zoukankan      html  css  js  c++  java
  • winform 端口serialport简用

    最近的一个小项目中需要从串口读取摄像机的应答指令,因此在程序中用到了SerialPort控件(使用SerialPort类也可以)。
    在SerialPort控件的属性列表中主要注意3个地方:
    (1)PortName:表示要打开的通信端口名称;
    (2)BaudRate:表示端口的波特率;
    (3)ReceivedBytesThreshold:表示触发SerialPort控件的DataReceived事件前输入缓冲区里的字节数;
    在这个小项目中,PortName的值为摄像机与电脑连接的端口(可通过设备管理器查看);
            BaudRate的值与摄像机串口设置的值一致;
            ReceivedBytesThreshold默认为1,表示当输入缓冲区中有1个字节就触发SerialPort控件的DataReceived事件(
    //初始化窗体
    private void Form1_Load(object sender, EventArgs e)
            {
                serialPort1.ReceivedBytesThreshold = 1;
                serialPort1.PortName = "COM3";
                serialPort1.BaudRate = 9600;
                serialPort1.Open();//打开串口
                serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
            }
    
    //serialPort1控件的DataReceived事件
    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                serialPort1.Read(Data,0,4);//data数组用于存储读取的数据
            }
    

     使用一个Button控件来向摄像机发送查询指令

    private void btnSend_Click(object sender, EventArgs e)
            {
                if (serialPort1.IsOpen == true)
                {
                    serialPort1.Write(Power, 0, 5);//Power数组用于存储发送给摄像机的查询指令
                }
            }
    
    在该事件中使用SerialPort.Read读取输入缓冲区中的数据)
  • 相关阅读:
    ColorMask
    InfoPanel
    什么是三消游戏
    Display file information in the document window
    Layer Comps
    Add words to your picture
    为什么质数是无穷的?
    嘉年华的来历
    MonoBehaviour.OnValidate
    Loadrunner中百分比模式和Vuser模式
  • 原文地址:https://www.cnblogs.com/gaara-zhang/p/9078698.html
Copyright © 2011-2022 走看看