serialport两个c#winforms应用程序 [英] serialport two c# winforms applications

查看:78
本文介绍了serialport两个c#winforms应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试串行端口是否正在发送/接收数据,我有两个c#winforms应用程序.
一种应用是:

i need to test whether serial port sending /receiving data , i have two c# winforms applications.
one application is:

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 Excel=Microsoft.Office.Interop.Excel;
using System.IO;
using System.Threading;
using System.IO.Ports;



namespace Hand_Held_Data_Transporter
{
    public partial class Form1 : Form
    {
        int[] stream_buffer;
        String final = null;
        String upload1 = null, upload2 = null, upload3 = null, upload4 = null, upload5 = null, upload6 = null, upload7 = null;
        
        

        /*private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
        private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
        private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
        private static Microsoft.Office.Interop.Excel.Application oXL;*/

        public Form1()
        {
            InitializeComponent();
            progressBar1.Enabled = false;
            progressBar2.Enabled = false;

            serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
           
        }



        private void Browse_Click(object sender, EventArgs e)
        {

            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Filter = "All Files(*.*)|*.*";
            if (fdlg.ShowDialog() == DialogResult.OK)
            {


                textBox1.Text = fdlg.FileName;
                File.ReadAllText(textBox1.Text);

            }
        }

       



        private void Upload_Click(object sender, EventArgs e)
        {

           // StringBuilder sb = new StringBuilder();
  

            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;
            progressBar1.Visible = true;
            

            int k = 100 / rowCount;

            for (int i = 1; i <= rowCount; i++)
            {

               
                for (int j = 1; j <= colCount; j++)
                {

                    if (j == 1)
                      upload1 = xlRange.Cells[i, j].Value.ToString();
                 

                        //upload1 = xlRange.Cells[i, j].Value.ToString();
                    if (j == 2)
                        upload2 = xlRange.Cells[i, j].Value.ToString();
                   
                  
                    if (j == 3)
                        upload3 = xlRange.Cells[i, j].Value.ToString();
                
                    if (j == 4)
                        upload4= xlRange.Cells[i, j].Value.ToString();
                    
                     if (j == 5)
                         upload5 = xlRange.Cells[i, j].Value.ToString();
                     
                    if (j == 6)
                        upload6= xlRange.Cells[i, j].Value.ToString();
                    
                    if (j == 7)
                        upload7 = xlRange.Cells[i, j].Value.ToString();
                    

                    final = "$ POST," + upload1 + "," + upload2 + "," + upload3 + "," + upload4 + "," + upload5 + "," + upload6 + "," + upload7 + " # ";


                }

                if (serialPort1.IsOpen)

                   serialPort1.Write(final);
                Thread.Sleep(1000);

              serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);

                MessageBox.Show(final);
                if (progressBar1.Value < 100)
                {
                    Thread.Sleep(1000);
                    progressBar1.Value += k;
                }


            }
            progressBar1.Value = 100;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            progressBar1.Visible = false;
            progressBar2.Visible = false;
           serialPort1.Open();

           string[] ArrayComPortsNames = null;
           int index = -1;
           string ComPortName = null;

           ArrayComPortsNames = SerialPort.GetPortNames();
           do
           {
               index += 1;
               comboBox1.Items.Add(ArrayComPortsNames[index]);
           }

           while (!((ArrayComPortsNames[index] == ComPortName)
                         || (index == ArrayComPortsNames.GetUpperBound(0))));
           Array.Sort(ArrayComPortsNames);

           //want to get first out
           if (index == ArrayComPortsNames.GetUpperBound(0))
           {
               ComPortName = ArrayComPortsNames[0];
           }
           comboBox1.Text = ArrayComPortsNames[0];



         
        }


另一个应用是:


and another application is:

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 Hand_Held_Data_Transporter;
using System.IO.Ports;

namespace handheld_Responce
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            serialPort1.Open();
            serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
        }

        private void send_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {




        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                String RecievedData;
                RecievedData = serialPort1.ReadExisting();
                if (!(RecievedData == ""))
                {
                    textBox2.Text += RecievedData;
                }
            }
        }
    }
}





请告诉我如何交流两个应用程序

[edit]已添加代码块-OriginalGriff [/edit]





plz tell me how can i communicate both applications

[edit]Code block added - OriginalGriff[/edit]

推荐答案

POST," + upload1 + ," + upload2 + ," + upload3 + " + upload4 + " + upload5 + " + upload6 + " + upload7 + #"; } 如果(serialPort1.IsOpen) serialPort1.Write(final); Thread.Sleep( 1000 ); serialPort1.DataReceived + = SerialDataReceivedEventHandler(serialPort1_DataReceived); MessageBox.Show(final); 如果(progressBar1.Value < 100 ) { Thread.Sleep( 1000 ); progressBar1.Value + = k; } } progressBar1.Value = 100 ; } 私有 无效 Form1_Load(对象发​​件人,EventArgs e) { progressBar1.Visible = false ; progressBar2.Visible = false ; serialPort1.Open(); 字符串 [] ArrayComPortsNames = ; int index = -1; 字符串 ComPortName = ; ArrayComPortsNames = SerialPort.GetPortNames(); { 索引+ = 1 ; comboBox1.Items.Add(ArrayComPortsNames [index]); } while (!(((ArrayComPortsNames [index] == ComPortName) || (index == ArrayComPortsNames.GetUpperBound( 0 ))))); Array.Sort(ArrayComPortsNames); // 想抢先 如果(索引== ArrayComPortsNames.GetUpperBound( 0 )) { ComPortName = ArrayComPortsNames [ 0 ]; } comboBox1.Text = ArrayComPortsNames [ 0 ]; }
POST," + upload1 + "," + upload2 + "," + upload3 + "," + upload4 + "," + upload5 + "," + upload6 + "," + upload7 + " # "; } if (serialPort1.IsOpen) serialPort1.Write(final); Thread.Sleep(1000); serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived); MessageBox.Show(final); if (progressBar1.Value < 100) { Thread.Sleep(1000); progressBar1.Value += k; } } progressBar1.Value = 100; } private void Form1_Load(object sender, EventArgs e) { progressBar1.Visible = false; progressBar2.Visible = false; serialPort1.Open(); string[] ArrayComPortsNames = null; int index = -1; string ComPortName = null; ArrayComPortsNames = SerialPort.GetPortNames(); do { index += 1; comboBox1.Items.Add(ArrayComPortsNames[index]); } while (!((ArrayComPortsNames[index] == ComPortName) || (index == ArrayComPortsNames.GetUpperBound(0)))); Array.Sort(ArrayComPortsNames); //want to get first out if (index == ArrayComPortsNames.GetUpperBound(0)) { ComPortName = ArrayComPortsNames[0]; } comboBox1.Text = ArrayComPortsNames[0]; }


另一个应用是:


and another application is:

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 Hand_Held_Data_Transporter;
using System.IO.Ports;

namespace handheld_Responce
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            serialPort1.Open();
            serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
        }

        private void send_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {




        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                String RecievedData;
                RecievedData = serialPort1.ReadExisting();
                if (!(RecievedData == ""))
                {
                    textBox2.Text += RecievedData;
                }
            }
        }
    }
}





请告诉我如何交流两个应用程序

[edit]已添加代码块-OriginalGriff [/edit]





plz tell me how can i communicate both applications

[edit]Code block added - OriginalGriff[/edit]


好吧,对于初学者而言,请停止如此频繁地添加DataRecieved处理程序.如果在构造函数中执行此操作,则无需再次执行此操作,尤其是每次按下按钮时都不需要这样做!

然后,我首先从一个应用程序向另一个应用程序发送一个变化的字符以证明它完全可以工作:打开串行端口,然后循环播放:
Well, for starters, stop adding DataRecieved handlers so often. If you do it in your constructor, there is no need to do it again, particularly no every time you press a button!

Then I would start by just sending a changing character from one app to the other to prove it works at all: Open the serial port, then sit in a loop:
for (int i = 0; i < 1000; i++)
   {
   serialPort1.Write((i % 10).ToString();
   Thread.Sleep(100);
   }

如果您在其他应用程序上收到确认,则可以考虑开始发送实际数据.

If you are receiving ok on the other app, then you can look at starting to send actual data.


这篇关于serialport两个c#winforms应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆