Click here to Skip to main content
15,943,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All

i was receive project read data from com port using the console application and save same out put at desktop in txt file the com port we r using is way bridge comport
my code are as blew pls let me know wt change i do than i will read data from comport

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.IO;

namespace PortChat
    public class PortChat
       // private static StreamWriter sw = new StreamWriter("filename.txt");
        static bool _continue;
        static SerialPort _serialPort;
        public static void Main(string[] args)
            string name;
            string message;
            //string path = "";
            //// Write
            //string path = "example.txt";
            //string[] myMenu = { "A", "B", "C" };
            //File.WriteAllLines(path, myMenu);

            //// Read
            //string[] readMenu = File.ReadAllLines(path);
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);
            // Create a new SerialPort object with default settings.
            _serialPort = new SerialPort();

            // Allow the user to set the appropriate properties.
            _serialPort.PortName = SetPortName(_serialPort.PortName);
            _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
            _serialPort.Parity = SetPortParity(_serialPort.Parity);
            _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
            _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
            _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

            // Set the read/write timeouts
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;

            _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
            _continue = true;

            //_serialPort.WriteLine("$");//Command to start Data Stream
            //// Wait for data or user input to continue.
           // Console.WriteLine("FILEPATH");
           // path = Console.ReadLine();
            Console.Write("Name: ");
            name = Console.ReadLine();
            Console.WriteLine("Type QUIT to exit");
            while (_continue)
                message = Console.ReadLine();

                if (stringComparer.Equals("quit", message))
                    _continue = false;
                        String.Format("<{0}>: {1}", name, message));

        static void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
            SerialPort _serialPort = (SerialPort)sender;

        public static void Read()
            while (_continue)
                    string message = _serialPort.ReadLine();
                catch (TimeoutException) { }
        public static string SetPortName(string defaultPortName)
            string portName="";

            Console.WriteLine("Available Ports:");
            foreach (string s in SerialPort.GetPortNames())
                Console.WriteLine("   {0}", s);
                portName = s;
            //my code
            _serialPort.PortName = portName;
            defaultPortName = portName;
            Console.Write("COM port({0}): ", defaultPortName);
            portName = Console.ReadLine();

            if (portName == "")
                portName = defaultPortName;
            return portName;
        public static int SetPortBaudRate(int defaultPortBaudRate)
            string baudRate;

            Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
            baudRate = Console.ReadLine();

            if (baudRate == "")
                baudRate = defaultPortBaudRate.ToString();

            return int.Parse(baudRate);
        public static Parity SetPortParity(Parity defaultPortParity)
            string parity;

            Console.WriteLine("Available Parity options:");
            foreach (string s in Enum.GetNames(typeof(Parity)))
                Console.WriteLine("   {0}", s);

            Console.Write("Parity({0}):", defaultPortParity.ToString());
            parity = Console.ReadLine();

            if (parity == "")
                parity = defaultPortParity.ToString();

            return (Parity)Enum.Parse(typeof(Parity), parity);
        public static int SetPortDataBits(int defaultPortDataBits)
            string dataBits;

            Console.Write("Data Bits({0}): ", defaultPortDataBits);
            dataBits = Console.ReadLine();

            if (dataBits == "")
                dataBits = defaultPortDataBits.ToString();

                return int.Parse(dataBits);
        public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
            string stopBits;

            Console.WriteLine("Available Stop Bits options:");
            foreach (string s in Enum.GetNames(typeof(StopBits)))
                Console.WriteLine("   {0}", s);

            Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString());
            stopBits = Console.ReadLine();

            if (stopBits == "")
                stopBits = defaultPortStopBits.ToString();

            return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
        public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
            string handshake;

            Console.WriteLine("Available Handshake options:");
            foreach (string s in Enum.GetNames(typeof(Handshake)))
                Console.WriteLine("   {0}", s);

            Console.Write("Handshake({0}):", defaultPortHandshake.ToString());
            handshake = Console.ReadLine();

            if (handshake == "")
                handshake = defaultPortHandshake.ToString();

            return (Handshake)Enum.Parse(typeof(Handshake), handshake);
Expert Coming 4-Dec-12 1:50am    
What isn't working?
Arjunwalmiki 4-Dec-12 2:01am    
It is working but data not receive from com port or i was not receive any error or how can i save output automatically at my desktop in the txt file.when i was press enter
1 st output Available port name.
2nd out when press enter bound rate 9600.
3rd out when press enter Available parity.parity<none>.
4th Data bite 8.
4th out when press enter Available stop bite.
5th out when press enter Available Handshake.
6th Name
7th Quit to exit
Expert Coming 4-Dec-12 2:06am    
Ah this is homework... My guess would be you have the serial port set to the wrong port.
Arjunwalmiki 4-Dec-12 2:10am    
Sir this is my 1st project i am new in .net so pls explain me proper way than i will do proper change in my project
Arjunwalmiki 4-Dec-12 2:16am    
Sir i am using rs232 male port and the way bridge port is also male port so use the female to female cable for connection

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