• 大小: 0.48M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-05-01
  • 语言: C#
  • 标签: 上位机  实现  串口  C#  

资源简介

C# 上位机 串口通讯 

资源截图

代码片段和文件信息

/*
 * Project:     Serial Port Interface with C#
 * Company:     StudentCompanion
 * Created:     April 2013
 * Updated:    February 2020
 * Visual Studio: 2019
 * .NET framework: 4.6.2
 * Notes:       This is a simple demontration on how to use the SerialPort control for
 *              communicating with your PC‘s  COM Port. 
 *              This is for educational purposes only and not for any commercial use. 
 *              For more on this please visit: https://www.studentcompanion.co.za/creating-a-serial-port-interface-with-c/
 * */

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;  //This is a namespace that contains the SerialPort class


namespace Serial_Communication
    {
    public partial class Form1 : Form
        {
        public Form1()
            {
            InitializeComponent();
            }

        private void Form1_Load(object sender EventArgs e) 
            {
            updatePorts();           //Call this function everytime the page load 
                                     //to update port names
            CheckForIllegalCrossThreadCalls = false;
            }
        private void updatePorts()
            {
            // Retrieve the list of all COM ports on your Computer
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
                {
                cmbPortName.Items.Add(port);
                }
            }
        private SerialPort ComPort = new SerialPort();  //Initialise ComPort Variable as SerialPort
        private void connect()
            {
            bool error = false;

            // Check if all settings have been selected

            if (cmbPortName.SelectedIndex != -1 & cmbBaudRate.SelectedIndex != -1 & cmbParity.SelectedIndex != -1 & cmbDataBits.SelectedIndex != -1 & cmbStopBits.SelectedIndex != -1)
                {
                    //if yes than Set The Port‘s settings
                    ComPort.PortName = cmbPortName.Text;
                    ComPort.BaudRate = int.Parse(cmbBaudRate.Text);      //convert Text to Integer
                    ComPort.Parity = (Parity)Enum.Parse(typeof(Parity) cmbParity.Text); //convert Text to Parity
                    ComPort.DataBits = int.Parse(cmbDataBits.Text);        //convert Text to Integer
                    ComPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits) cmbStopBits.Text);  //convert Text to stop bits
                    
                try  //always try to use this try and catch method to open your port. 
                     //if there is an error your program will not display a message instead of freezing.
                    {
                    //Open Port
                    ComPort.Open();
                    ComPort.DataReceived += SerialPortD

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-02-02 22:56  Serial_Communication\
     目录           0  2020-02-01 07:41  Serial_Communication\.vs\
     目录           0  2020-02-01 07:41  Serial_Communication\.vs\Serial_Communication\
     目录           0  2020-02-01 07:57  Serial_Communication\.vs\Serial_Communication\v16\
     文件       29696  2020-02-02 22:57  Serial_Communication\.vs\Serial_Communication\v16\.suo
     目录           0  2020-02-01 07:46  Serial_Communication\.vs\Serial_Communication\v16\Server\
     目录           0  2020-02-02 22:57  Serial_Communication\.vs\Serial_Communication\v16\Server\sqlite3\
     文件           0  2020-02-01 07:46  Serial_Communication\.vs\Serial_Communication\v16\Server\sqlite3\db.lock
     文件      569344  2020-02-02 22:57  Serial_Communication\.vs\Serial_Communication\v16\Server\sqlite3\storage.ide
     文件         163  2020-02-02 22:12  Serial_Communication\app.config
     目录           0  2014-02-18 21:52  Serial_Communication\bin\
     目录           0  2020-02-02 22:22  Serial_Communication\bin\Debug\
     文件       30208  2020-02-02 22:22  Serial_Communication\bin\Debug\Serial_Communication.exe
     文件         163  2020-02-02 22:12  Serial_Communication\bin\Debug\Serial_Communication.exe.config
     文件       30208  2020-02-02 22:22  Serial_Communication\bin\Debug\Serial_Communication.pdb
     文件       11600  2013-04-26 21:09  Serial_Communication\bin\Debug\Serial_Communication.vshost.exe
     文件         490  2010-03-18 04:39  Serial_Communication\bin\Debug\Serial_Communication.vshost.exe.manifest
     目录           0  2015-11-30 02:06  Serial_Communication\bin\Release\
     目录           0  2014-02-18 21:52  Serial_Communication\bin\Release\app.publish\
     目录           0  2014-02-18 21:52  Serial_Communication\bin\Release\app.publish\Application Files\
     目录           0  2014-02-18 21:52  Serial_Communication\bin\Release\app.publish\Application Files\Serial_Communication_1_0_0_0\
     文件       15872  2013-04-26 21:19  Serial_Communication\bin\Release\app.publish\Application Files\Serial_Communication_1_0_0_0\Serial_Communication.exe.deploy
     文件        6747  2013-04-26 21:20  Serial_Communication\bin\Release\app.publish\Application Files\Serial_Communication_1_0_0_0\Serial_Communication.exe.manifest
     文件        5658  2013-04-26 21:20  Serial_Communication\bin\Release\app.publish\Serial_Communication.application
     文件      428960  2013-04-26 21:20  Serial_Communication\bin\Release\app.publish\setup.exe
     文件        1938  2015-11-30 02:10  Serial_Communication\bin\Release\Serial_Communication.application
     文件       15872  2015-11-30 02:10  Serial_Communication\bin\Release\Serial_Communication.exe
     文件        3101  2015-11-30 02:10  Serial_Communication\bin\Release\Serial_Communication.exe.manifest
     文件       34304  2015-11-30 02:10  Serial_Communication\bin\Release\Serial_Communication.pdb
     文件        1938  2015-11-30 02:10  Serial_Communication\bin\Release\Serial_Communication.vshost.application
     文件       24224  2015-11-30 02:10  Serial_Communication\bin\Release\Serial_Communication.vshost.exe
............此处省略47个文件信息

评论

共有 条评论