Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi,

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:
C#
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:

C#
<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#");
            }
            
        }
    }
}
Posted
Comments
Member 10263519 14-Dec-13 0:33am    
ok working fine.
So what is the problem?
Sergey Alexandrovich Kryukov 14-Dec-13 1:09am    
You are mixing together communication and UI and, as far as I can see, don't use separate threads for communication scenario, hard-code using "magic numbers" 1, 2, 3... so what do you want? No wonder the functionality is also messed up. Design code more thoroughly, implement accurately, write the code in a neat way, and then perhaps it could be easier to put to work; and even if you fail at first, you will be able to isolate things and ask more clear questions. Right now, it's the pain to look at your code...

Ah... I see, working fine, but don't think your code is fine. You may have further troubles. I'm telling you: do everything accurately, otherwise supporting your code and fixing it will be increasingly difficult.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900