C # combined with Serial communication class to achieve serial communication source code

Source: Internet
Author: User
Tags bool

The

Mycom is a serial communication class. The following is the serial communication test program source code, will mycom class in this project, you can achieve a simple serial communication, I hope readers through this program to the serial communication process has a preliminary understanding:

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Threading;
 Summary description of namespace Busapp {///<summary>///Form1.
  </summary> public class Form1:System.Windows.Forms.Form {private System.Windows.Forms.Label Label1;
  Private System.Windows.Forms.Label Label2;
  Private System.Windows.Forms.Button button1;
  Private System.Windows.Forms.GroupBox GroupBox1;
  Private System.Windows.Forms.Label label3;
  Private System.Windows.Forms.Label label4;
  Private System.Windows.Forms.Label Label5;
  Private System.Windows.Forms.Label label6;
  Private System.Windows.Forms.Button button2;
  Private System.Windows.Forms.Button Button3;
  Private System.Windows.Forms.Button Button4;
  Private System.Windows.Forms.TextBox textBox8;
  Private System.Windows.Forms.Label Label7; public int iport=1; 1,2,3,4 public int irate=9600; 1200,2400,4800,9600 public byte bsize=8; //8 bits public byte bparity=0; 0-4=no,odd,even,mark,space public byte Bstopbits=1;
  0,1,2 = 1, 1.5, 2 public int itimeout=1000;
  Public mycom mycom1=new mycom ();
  Public byte[] RECB;
  Private System.Windows.Forms.TextBox msg;
  Private System.Windows.Forms.TextBox T_port;
  Private System.Windows.Forms.TextBox t_rate;
  Private System.Windows.Forms.TextBox t_bytesize;
  Private System.Windows.Forms.TextBox T_stopbyte;
  Private System.Windows.Forms.TextBox t_parity;
  Private System.Windows.Forms.TextBox t_send; Private System.Windows.Forms.Button button5;
  ReadTimeout///<summary>///required designer variables.
  </summary> private System.ComponentModel.Container components = null;
  Public Form1 () {InitializeComponent ();
  ///<summary>///Clean up all the resources that are in use.  </summary> protected override void Dispose (bool disposing) {if (disposing) {if (Components!= NULL) {components.
    Dispose (); } base. Dispose (disposing);
  #region the code generated by the Windows Forms Designer///<summary>///Designer supports the desired method-do not use the Code Editor to modify the contents of///this method.
   </summary> private void InitializeComponent () {this.msg = new System.Windows.Forms.TextBox ();
   This.label1 = new System.Windows.Forms.Label ();
   This.label2 = new System.Windows.Forms.Label ();
   This.t_send = new System.Windows.Forms.TextBox ();
   This.button1 = new System.Windows.Forms.Button ();
   This.groupbox1 = new System.Windows.Forms.GroupBox ();
   This.button2 = new System.Windows.Forms.Button ();
   This.t_port = new System.Windows.Forms.TextBox ();
   This.label3 = new System.Windows.Forms.Label ();
   This.t_rate = new System.Windows.Forms.TextBox ();
   This.label4 = new System.Windows.Forms.Label ();
   This.t_bytesize = new System.Windows.Forms.TextBox ();
   This.label5 = new System.Windows.Forms.Label ();
   This.t_stopbyte = new System.Windows.Forms.TextBox ();
   This.label6 = new System.Windows.Forms.Label (); this.t_parity = new System.wiNdows.
   Forms.textbox ();
   This.button3 = new System.Windows.Forms.Button ();
   This.button4 = new System.Windows.Forms.Button ();
   this.textbox8 = new System.Windows.Forms.TextBox ();
   THIS.LABEL7 = new System.Windows.Forms.Label ();
   This.button5 = new System.Windows.Forms.Button ();
   This.groupBox1.SuspendLayout (); This.
   SuspendLayout ();
   msg//This.msg.ForeColor = System.Drawing.Color.Green;
   This.msg.Location = new System.Drawing.Point (0, 0);
   This.msg.Multiline = true;
   This.msg.Name = "MSG";
   This.msg.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   This.msg.Size = new System.Drawing.Size (512, 264);
   This.msg.TabIndex = 0;
   This.msg.Text = "";
   Label1//this.label1.Location = new System.Drawing.Point (16, 24);
   This.label1.Name = "Label1";
   This.label1.Size = new System.Drawing.Size (56, 16);
   This.label1.TabIndex = 1;
   This.label1.Text = "string slogan:"; Label2//This.label2.ForeColor = System.Drawing.
   Color.FromArgb ((System.Byte) (in)), ((System.Byte) ()), ((System.Byte) (0));
   This.label2.Location = new System.Drawing.Point (8, 280);
   This.label2.Name = "Label2";
   This.label2.Size = new System.Drawing.Size (80, 16);
   This.label2.TabIndex = 1;
   This.label2.Text = "Set packet:"; T_send//This.t_send.
   BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; This.t_send.
   ForeColor = System.Drawing.Color.FromArgb ((system.byte) (255)), ((System.Byte) (128)), ((System.Byte) (0)); This.t_send.
   Location = new System.Drawing.Point (80, 272); This.t_send.
   Name = "T_send"; This.t_send.
   Size = new System.Drawing.Size (344, 21); This.t_send.
   TabIndex = 2; This.t_send.
   Text = "";
   Button1//this.button1.Location = new System.Drawing.Point (432, 272);
   This.button1.Name = "Button1";
   This.button1.Size = new System.Drawing.Size (40, 23);
   This.button1.TabIndex = 3;
   This.button1.Text = "Send"; This.button1.Click + = new System.EventHandler(This.button1_click);
   GroupBox1//THIS.GROUPBOX1.CONTROLS.ADD (This.button2);
   THIS.GROUPBOX1.CONTROLS.ADD (This.t_port);
   THIS.GROUPBOX1.CONTROLS.ADD (THIS.LABEL1);
   THIS.GROUPBOX1.CONTROLS.ADD (THIS.LABEL3);
   THIS.GROUPBOX1.CONTROLS.ADD (this.t_rate);
   THIS.GROUPBOX1.CONTROLS.ADD (THIS.LABEL4);
   THIS.GROUPBOX1.CONTROLS.ADD (this.t_bytesize);
   THIS.GROUPBOX1.CONTROLS.ADD (THIS.LABEL5);
   THIS.GROUPBOX1.CONTROLS.ADD (This.t_stopbyte);
   THIS.GROUPBOX1.CONTROLS.ADD (THIS.LABEL6);
   THIS.GROUPBOX1.CONTROLS.ADD (this.t_parity); This.groupBox1.ForeColor = System.Drawing.Color.FromArgb ((System.Byte) (()), ((System.Byte) ()), ((System.Byte)
   (0)));
   This.groupBox1.Location = new System.Drawing.Point (8, 304);
   This.groupBox1.Name = "GroupBox1";
   This.groupBox1.Size = new System.Drawing.Size (176, 216);
   This.groupBox1.TabIndex = 4;
   This.groupBox1.TabStop = false;
   This.groupBox1.Text = "parameter setting"; Button2//This.button2.FlatStyle = SysteM.windows.forms.flatstyle.popup;
   This.button2.Location = new System.Drawing.Point (80, 184);
   This.button2.Name = "Button2";
   This.button2.TabIndex = 3;
   This.button2.Text = "Apply settings";
   This.button2.Click + = new System.EventHandler (This.button2_click); T_port//This.t_port.
   BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; This.t_port.
   Location = new System.Drawing.Point (80, 16); This.t_port.
   Name = "T_port"; This.t_port.
   Size = new System.Drawing.Size (80, 21); This.t_port.
   TabIndex = 2; This.t_port.
   Text = "1";
   LABEL3//this.label3.Location = new System.Drawing.Point (16, 58);
   This.label3.Name = "Label3";
   This.label3.Size = new System.Drawing.Size (56, 16);
   This.label3.TabIndex = 1;
   This.label3.Text = "baud rate:"; T_rate//This.t_rate.
   BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; This.t_rate.
   Location = new System.Drawing.Point (80, 50); This.t_rate.
   Name = "T_rate"; This.t_rate.
   Size = new System.Drawing.Size (80, 21); This.t_rate.
   TabIndex = 2; This.t_rate.
   Text = "9600";
   Label4//this.label4.Location = new System.Drawing.Point (16, 92);
   This.label4.Name = "Label4";
   This.label4.Size = new System.Drawing.Size (56, 16);
   This.label4.TabIndex = 1;
   This.label4.Text = "Data bits:"; T_bytesize//This.t_bytesize.
   BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; This.t_bytesize.
   Location = new System.Drawing.Point (80, 84); This.t_bytesize.
   Name = "T_bytesize"; This.t_bytesize.
   Size = new System.Drawing.Size (80, 21); This.t_bytesize.
   TabIndex = 2; This.t_bytesize.
   Text = "8";
   Label5//this.label5.Location = new System.Drawing.Point (16, 126);
   This.label5.Name = "Label5";
   This.label5.Size = new System.Drawing.Size (56, 16);
   This.label5.TabIndex = 1;
   This.label5.Text = "Stop bit:"; T_stopbyte//This.t_stopbyte. BorderStyle = System.Windows.Forms.BorderStyle.FixedSiNgle; This.t_stopbyte.
   Location = new System.Drawing.Point (80, 118); This.t_stopbyte.
   Name = "T_stopbyte"; This.t_stopbyte.
   Size = new System.Drawing.Size (80, 21); This.t_stopbyte.
   TabIndex = 2; This.t_stopbyte.
   Text = "1";
   LABEL6//this.label6.Location = new System.Drawing.Point (16, 160);
   This.label6.Name = "Label6";
   This.label6.Size = new System.Drawing.Size (56, 16);
   This.label6.TabIndex = 1;
   This.label6.Text = "Check digit:"; T_parity//This.t_parity.
   BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; This.t_parity.
   Enabled = false; This.t_parity.
   Location = new System.Drawing.Point (80, 152); This.t_parity.
   Name = "T_parity"; This.t_parity.
   Size = new System.Drawing.Size (80, 21); This.t_parity.
   TabIndex = 2; This.t_parity.
   Text = "0";
   Button3//this.button3.Location = new System.Drawing.Point (472, 272);
   This.button3.Name = "Button3";
 This.button3.Size = new System.Drawing.Size (40, 23);  This.button3.TabIndex = 3;
   This.button3.Text = "Empty";
   This.button3.Click + = new System.EventHandler (This.button3_click);
   Button4//This.button4.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
   This.button4.Location = new System.Drawing.Point (432, 312);
   This.button4.Name = "Button4";
   This.button4.Size = new System.Drawing.Size (72, 23);
   This.button4.TabIndex = 6;
   This.button4.Text = "initialization";
   TEXTBOX8//This.textBox8.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; This.textBox8.ForeColor = System.Drawing.Color.FromArgb ((system.byte) (255)), ((System.Byte) (128)), ((System.Byte)
   (0)));
   This.textBox8.Location = new System.Drawing.Point (288, 312);
   This.textBox8.Name = "TextBox8";
   This.textBox8.Size = new System.Drawing.Size (136, 21);
   This.textBox8.TabIndex = 7;
   This.textBox8.Text = "";
   LABEL7//this.label7.Location = new System.Drawing.Point (200, 320);
   This.label7.Name = "Label7";This.label7.Size = new System.Drawing.Size (100, 16);
   This.label7.TabIndex = 8;
   This.label7.Text = "Set the local address:";
   BUTTON5//This.button5.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
   This.button5.Location = new System.Drawing.Point (440, 504);
   This.button5.Name = "Button5";
   This.button5.Size = new System.Drawing.Size (64, 23);
   This.button5.TabIndex = 9;
   This.button5.Text = "Close serial port";
   This.button5.Click + = new System.EventHandler (This.button5_click); Form1//this.
   AutoScaleBaseSize = new System.Drawing.Size (6, 14); This.
   ClientSize = new System.Drawing.Size (512, 533); This.
   Controls.Add (THIS.BUTTON5); This.
   Controls.Add (THIS.TEXTBOX8); This.
   Controls.Add (THIS.LABEL7); This.
   Controls.Add (THIS.BUTTON4); This.
   Controls.Add (This.groupbox1); This.
   Controls.Add (This.button1); This.
   Controls.Add (This.t_send); This.
   Controls.Add (THIS.MSG); This.
   Controls.Add (THIS.LABEL2); This.
   Controls.Add (This.button3); This. Name= "Form1"; This.
   Text = "Serial communication (small y design)"; This. Closing + = new System.ComponentModel.CancelEventHandler (this.
   Form1_Closing); This. Load + = new System.EventHandler (this.
   Form1_Load);
   This.groupBox1.ResumeLayout (FALSE); This.
  ResumeLayout (FALSE);
  #endregion The main entry point for the///<summary>///application.
  </summary> [STAThread] static void Main () {Application.Run (New Form1 ()); }//program open, serial initialization private void Form1_Load (object sender, System.EventArgs e) {mycom1.
   Portnum=iport; Mycom1.
   Baudrate=irate; Mycom1.
   Bytesize=bsize; Mycom1.
   parity=bparity; Mycom1.
   Stopbits=bstopbits; Mycom1.
   Readtimeout=itimeout; if (this. Opencom ()) Msg.
   AppendText ("Serial port initialization successful ... \ r \ n"); Else Msg. AppendText ("Serial port initialization failed!")
  \ r \ n ");
   ///Display package information public string Dis_package (byte[] reb) {string temp= "";
   foreach (Byte b in Reb) temp+=b.tostring ("X2") + "";
  return temp; //Open serial Port public bool Opencom () {try {if (mycom1). Opened) {mycom1. Close(); Mycom1. Open (); Open the serial port} else {mycom1.
   Open ();//Turn on serial port true;
     catch (Exception e) {MessageBox.Show ("error:" + e.message);
   return false; }//Send button private void Button1_Click (object sender, System.EventArgs e) {if (t_send). text== "") {MessageBox.Show ("Send data is empty!)"
   "); return;}
   Byte[] Temp1=mysendb ();
   int sendnumb=0; try {sendnumb=mycom1.
     Write (TEMP1); Msg.
    AppendText ("\ r \ n) send data (" +sendnumb+ "):" +dis_package (TEMP1)); Recb=mycom1.
    Read (50); if (RECB. length!=0) Msg.
   AppendText ("\ r \ n received packet:" +dis_package (RECB)); Catch {msg. AppendText ("\ r \ n Send failed!")

   "); return;}
  Opencom ();
   //Remove the space in the Send array public string Delspace (String Putin) {string putout= ""; for (int i=0;i<putin.
   length;i++) {if (putin[i]!= ') putout+=putin[i];
  return putout; }//Extract packet public byte[] Mysendb () {string Temps=delspace (t_send.
   Text);
   Byte[] Tempb=new byte[50];
  int j=0; for (int i=0;i<temps. length;i=i+2,j++) Tempb[j]=convert.tobyte (temps).
   Substring (i,2), 16);
   Byte[] Send=new byte[j];
   Array.copy (TEMPB,SEND,J);
  return send; }//Empty button private void Button3_Click (object sender, System.EventArgs e) {t_send. Text=string.
   Empty; Msg. Text=string.
  Empty; }//Parameter setting private void Button2_Click (object sender, System.EventArgs e) {mycom1. Portnum=convert.toint16 (T_port. Text); 1,2,3,4 mycom1. Baudrate=convert.toint16 (t_rate. Text); 1200,2400,4800,9600 mycom1. Bytesize=convert.tobyte (t_bytesize. text,10); 8 bits mycom1. Parity=convert.tobyte (t_parity. text,10); 0-4=no,odd,even,mark,space mycom1. Stopbits=convert.tobyte (T_stopbyte. text,10);
   0,1,2 = 1, 1.5, 2//itimeout=3; if (this. Opencom ()) Msg.
   AppendText ("Serial port initialization successful ... \ r \ n"); Else Msg. AppendText ("Serial port initialization failed!")
  \ r \ n "); }//program shutdown, end serial port private void Form1_Closing (object sender, System.ComponentModel.CancelEventArgs e) {mycom1.
  Close (); } private void ButtOn5_click (object sender, System.EventArgs e) {if (mycom1). Opened) {mycom1.
    Close (); Button5.
    text= "Open serial port"; Msg.
   AppendText ("\ r \ n Serial port is closed ..."); else {mycom1.
    Open (); Button5.
    text= "Close serial port"; Msg.
   AppendText ("\ r \ n Serial port successfully opened ..."); }
  }
 }
}

Note: The format of the packet sent is 16 data such as: The FA can have or without spaces in the middle, but to ensure that there are even digits.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.