串口两个c#应用程序没有发送/接收 [英] serial port two c# applications not sending/receiving

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

问题描述





昨天我的项目工作得很好。但是现在它没有用。实际上是在两个c#winform应用程序上,只是为了测试它们是发送还是接收。



一旦第二个应用程序接收到这些数据,第一个应用程序需要逐行发送excel数据,这将返回一些信息($ machineID#)。



这里是代码o第一个应用程序:



yesterday my project worked fine.but now it's not working.actually am woking on two c# winform applications just for testing whether they are sending or receiving.

1st application need to send excel data row by row once this data received by second application that will sen back some info($machineID#).

here is the code o 1st application:

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 = 2; i <= rowCount; i++)
            {

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

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

                        //upload1 = xlRange.Cells[i, j].Value.ToString();
                    if (j == 2)
                        custId = xlRange.Cells[i, j].Value.ToString();
                   
                  
                    if (j == 3)
                        name = xlRange.Cells[i, j].Value.ToString();
                
                    if (j == 4)
                        totalBal= xlRange.Cells[i, j].Value.ToString();
                    
                     if (j == 5)
                        paid = xlRange.Cells[i, j].Value.ToString();
                     
                    if (j == 6)
                        lastPaid= xlRange.Cells[i, j].Value.ToString();
                    
                    if (j == 7)
                       due = xlRange.Cells[i, j].Value.ToString();


                    final = "$ POST," + machId + "," + custId + "," + name + "," + totalBal + "," + paid + "," + lastPaid + "," + due + " # ";


                }

                if (serialPort1.IsOpen)
                {
                    if(machId.Length<=5 && custId.Length<=10 && name.Length<=15 && totalBal.Length<=7 && paid.Length<=7 && lastPaid.Length<=14 && due.Length<=7 )
                  serialPort1.Write(final);
                    

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







和第二个申请是:






and second application is:

<pre lang="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 Hand_Held_Data_Transporter;
using System.IO.Ports;
using System.Threading;

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;
                        textBox2.Invoke((MethodInvoker)delegate { textBox2.Text += RecievedData; });
                    }
                    Thread.Sleep(100);
                    serialPort1.WriteLine("$M121,0#");
            }
            
        }
    }
}

推荐答案

machineID# )。



这里是代码o第一个应用程序:

machineID#).

here is the code o 1st application:
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 = 2; i <= rowCount; i++)
            {

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

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

                        //upload1 = xlRange.Cells[i, j].Value.ToString();
                    if (j == 2)
                        custId = xlRange.Cells[i, j].Value.ToString();
                   
                  
                    if (j == 3)
                        name = xlRange.Cells[i, j].Value.ToString();
                
                    if (j == 4)
                        totalBal= xlRange.Cells[i, j].Value.ToString();
                    
                     if (j == 5)
                        paid = xlRange.Cells[i, j].Value.ToString();
                     
                    if (j == 6)
                        lastPaid= xlRange.Cells[i, j].Value.ToString();
                    
                    if (j == 7)
                       due = xlRange.Cells[i, j].Value.ToString();


                    final = "


POST, + machId + + custId + + name + + totalBal + + paid + + lastPaid + + due + ;


}

if (serialPort1.IsOpen)
{
if (machId.Length< = 5 && custId.Length< = 10 && name.Length< = 15 && totalBal.Length< = 7 && paid.Length< = 7 && lastPaid.Length< = 14 && due.Length< = 7
serialPort1.Write(final);


} serialPort1.DataReceived + = new SerialDataReceivedEventHandler(serialPort1_DataReceived);
POST," + machId + "," + custId + "," + name + "," + totalBal + "," + paid + "," + lastPaid + "," + due + " # "; } if (serialPort1.IsOpen) { if(machId.Length<=5 && custId.Length<=10 && name.Length<=15 && totalBal.Length<=7 && paid.Length<=7 && lastPaid.Length<=14 && due.Length<=7 ) serialPort1.Write(final); } serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);







和第二个申请是:






and second application is:

<pre lang="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 Hand_Held_Data_Transporter;
using System.IO.Ports;
using System.Threading;

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;
                        textBox2.Invoke((MethodInvoker)delegate { textBox2.Text += RecievedData; });
                    }
                    Thread.Sleep(100);
                    serialPort1.WriteLine("


M121,0#< /跨度>);
}

}
}
}
M121,0#"); } } } }


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

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