Click here to Skip to main content
12,303,873 members (73,638 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
 

 
namespace PortDataReceived
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort mySerialPort = new SerialPort("COM1");
            if (mySerialPort.IsOpen == true) mySerialPort.Close();
            mySerialPort.BaudRate = 2400;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.Open();
            Console.WriteLine("Press any key to continue...");
            Console.WriteLine();
            mySerialPort.ReadTimeout = 500;
            string indata = mySerialPort.ReadExisting();
            if (string.IsNullOrEmpty(indata))
            {
                FileStream fs = new FileStream(@"c:\arjun.txt",FileMode.Append,FileAccess.Write);
                try
                {
                   
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine("0");
                    sw.Close();
                }
                finally
                {
                    fs.Close();
                }
                
            }
            else
            {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < indata.Length; i += 8)
                {
 
                    string section = indata.Substring(i, 8);
 
                    int ascii = 0;
                    try
                    {
                        ascii = Convert.ToInt32(section, 2);
                    }
                    catch
                    {
                        Console.WriteLine("Current Weight : " + section.ToString());
                        FileStream fs = new FileStream(@"c:\arjun.txt", FileMode.Append, FileAccess.Write);
                        try
                        {
                            StreamWriter sw = new StreamWriter(fs);
 
                            foreach (char c in section)
                            {
                                if (Char.IsLetterOrDigit(c))
                                {
                                    builder.Append(c);
                                }
                            }
 
                            sw.WriteLine(builder.ToString());
                            sw.Close();
                            break;
                        }
                        finally
                        {
                            fs.Close();
                        
                        }
 
                    }
                   
                }
            }
           // Console.WriteLine(builder.ToString());
            Console.WriteLine(indata);
            Console.WriteLine("Data Received:");
            Console.Write(indata);
            Console.ReadKey();
            mySerialPort.Close();
        }
    }
}




hello all the above is my code in this code i am read data from com port but problem is i was received 6 to 7 zero than received actual data from comport i was not under what was problem in my code pls let me know i was very thank full to all.i want solution on priority pls
Posted 14-Dec-12 22:33pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,
There could be few reasons. But the most critical one is whether the serial port that you created can read the data. I focus on the Baud Rate of this serial port. How you came up with 2400 for this. When specifying this value you need to make sure that the application that writes the data should have the same baud rate (both sender and receiver should have same baud rate). So as a start point you can set the Baud Rate as same as the sender. After that you can move forward with spending some debugging.

If there is some unwanted data bytes it should be related to
mySerialPort.ReadExisting();
As this method reads all data in the com port buffer. So think a way to clean it before send data again.
Regards.
  Permalink  
v2
Comments
Arjunwalmiki 15-Dec-12 13:18pm
   
sir i am find out the Baud Rate 2400 is correct some time my program read data or some time not read

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160530.1 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100