In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to realize serial communication based on SerialPort class in C #". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "C # based on SerialPort class how to achieve serial communication" it!
Final effect
Form Settings:
Using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO.Ports;using System.Threading;using System.Windows.Forms;namespace SerialCommunication {public partial class Form1: Form {System.Threading.SynchronizationContext m_SyncContext = null; SerialPort serialPort = null; ToHexadecimalString toHexadecimalString = new ToHexadecimalString () Public Form1 () {m_SyncContext = SynchronizationContext.Current; InitializeComponent ();} / / Data initialization private void Form1_Load (object sender, EventArgs e) {string [] ports = SerialPort.GetPortNames (); SerialPortNumber.Items.AddRange (ports); SerialPortNumber.SelectedIndex = SerialPortNumber.Items.Count > 0:-1 BaudRateCom.Text = "9600"; / / Set parameters CheckBitCom.Text = "None"; DataBitCom.Text = "8"; StopBitCom.Text = "One" } / / Send data private void Send_Click (object sender, EventArgs e) {try {/ / Send data and handle exceptions byte [] bytes = new byte [SendMessage.Text.Length]; bytes = Encoding.Default.GetBytes (SendMessage.Text); serialPort.Write (bytes, 0, bytes.Length) } catch (Exception error) {MessageBox.Show (error.Message);}} / Byte to hexadecimal string / / Clear data private void Clear_Click (object sender, EventArgs e) {ReceiveMessage.Text = "" } private void Open_Click (object sender, EventArgs e) {string portName = SerialPortNumber.Text; int buadRate = int.Parse (BaudRateCom.Text); Parity parity = 0; switch (CheckBitCom.Text) {case "None": parity = Parity.None; break Case "Odd": parity = Parity.Odd; break; case "Even": parity = Parity.Even; break; case "Mark": parity = Parity.Mark; break; case "Space": parity = Parity.Space; break } int dataBit = int.Parse (DataBitCom.Text); StopBits stopBits = 0; switch (StopBitCom.Text) {case "None": stopBits = StopBits.None; break; case "One": stopBits = StopBits.One; break Case "Tow": stopBits = StopBits.Two; break; case "OnePointFive": stopBits = StopBits.OnePointFive; break } try {if (Open.Text = = "Open") {serialPort = new SerialPort (portName, buadRate, parity, dataBit, stopBits); serialPort.Open (); Open.Text = "Close" } else if (Open.Text = = "Close") {serialPort.Close (); Open.Text = "Open";}} catch (Exception errror) {MessageBox.Show (errror.Message) } serialPort.DataReceived + = onDataReceived;} private void onDataReceived (object sender, SerialDataReceivedEventArgs e) {try {Byte [] bytes = new byte [serialPort.BytesToRead]; serialPort.Read (bytes, 0, serialPort.BytesToRead) M_SyncContext.Post (new SendOrPostCallback ((obj) = > {ReceiveMessage.AppendText (Encoding.Default.GetString (bytes));}), bytes);} catch (Exception error) {MessageBox.Show (error.Message) At this point, I believe you have a deeper understanding of "how to realize serial communication based on SerialPort class in C #". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.