Click here to Skip to main content
15,909,324 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi am developing project on hyperterminal.

i sent data correctly , but am unable receive data from that serail port.plz help me.............


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 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;
      
        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++)
            {

                String final = null;
                String upload1 = null, upload2 = null, upload3 = null, upload4 = null, upload5 = null,upload6=null,upload7=null;
                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);

                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();
            
        }

        private void Download_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp = default(Excel.Application);
            Excel.Workbook xlWorkBook = default(Excel.Workbook);
            Excel.Worksheet xlWorkSheet = default(Excel.Worksheet);
           
            string fName = @"C:\STL\Download\Download_" + DateTime.Now.ToString("dd MMM YY HH mm").Trim() + ".xls ";
            try
            {
           
                object misValue = System.Reflection.Missing.Value;
                xlApp = new Excel.Application();
                xlWorkBook = xlApp.Workbooks.Add(misValue);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                xlWorkSheet.Cells[1, 1] = "Name";
                xlWorkSheet.Cells[1, 2] = "ID Number";
                xlWorkSheet.Cells[1, 3] = "Installment";
                xlWorkSheet.Cells[1, 4] = "Paid Amount";
                xlWorkSheet.Cells[1, 5] = "Due Amount";

                xlWorkSheet.Cells[1, 1].Interior.ColorIndex = 39;
                xlWorkSheet.Cells[1, 2].Interior.ColorIndex = 39;
                xlWorkSheet.Cells[1, 3].Interior.ColorIndex = 39;
                xlWorkSheet.Cells[1, 4].Interior.ColorIndex = 39;
                xlWorkSheet.Cells[1, 5].Interior.ColorIndex = 39;

               // serialPort1.Open();
               // String s = "$GET,Chsum#";
               // if (serialPort1.IsOpen)
              //  {
                  //  serialPort1.WriteLine(s);
                    
                  // String s1= serialPort1.ReadLine();
                  // MessageBox.Show(s1);
               // }


                xlWorkBook.SaveAs(fName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);
            }
            finally
            {
                if (xlApp != null)
                    releaseObject(xlApp);
                if (xlWorkBook != null)
                    releaseObject(xlWorkBook);
                if (xlWorkSheet != null)
                    releaseObject(xlWorkSheet);
            }
            if (System.IO.File.Exists(fName))
            {
                if (MessageBox.Show("Would you like to open the excel file?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        System.Diagnostics.Process.Start(fName);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error opening the excel file." + Environment.NewLine +
                          ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            

            }
        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                String inputdata = serialPort1.ReadExisting();
                MessageBox.Show(inputdata);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
}
}
}
Posted
Comments
thatraja 12-Dec-13 5:39am    
Mention what's your problem(though you mentioned that you're unable receive data but we need some details regarding that like error message, log details, etc.,)
Member 10263519 12-Dec-13 6:00am    
in serialport1_datareceived( , ) i have a message ,but it's not showing.
actually here my concept is:

first i need to select excel file by clicking browse button then click on upload button it's going to serialport,after receiving data that needs to send ok signal back.
Sayan Bera 12-Dec-13 10:23am    
What is the communication cable you are using? Sometimes coding is correct, but due to faulty cable, data is not transmitted.

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