zoukankan      html  css  js  c++  java
  • serialPort控件(串口通信)

    记得打开奇偶校验
    
    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;
    using System.Threading;
    
    namespace 串口通信_控件_
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
    //string[] ports = SerialPort.GetPortNames();
    
    }
    
    
    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    a = send();
    this.BeginInvoke(new System.Threading.ThreadStart(delegate()
    {
    textBox1.Text = a.ToString();
    }));
    
    }
    StringBuilder a = new StringBuilder();
    private StringBuilder send()
    {
    byte[] buf = new byte[serialPort1.BytesToRead];
    serialPort1.Read(buf, 0, buf.Length);
    a.Append(Encoding.ASCII.GetString(buf));
    return a;
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
    if (serialPort1.IsOpen == false)
    this.serialPort1.Open();
    string temp = textBox2.Text;
    byte[] buf = new byte[this.serialPort1.BytesToRead];
    buf = Encoding.ASCII.GetBytes(temp);
    if (serialPort1.IsOpen)
    {
    serialPort1.Write(buf, 0, buf.Length);
    }
    }
    }
    }
  • 相关阅读:
    POJ 3468 A Simple Problem with Integers
    BZOJ 4430 Guessing Camels
    POJ 2309 BST
    POJ 1990 MooFest
    cf 822B Crossword solving
    cf B. Black Square
    cf 828 A. Restaurant Tables
    Codefroces 822C Hacker, pack your bags!
    [HDU 2255] 奔小康赚大钱
    [BZOJ 1735] Muddy Fields
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/2736141.html
Copyright © 2011-2022 走看看