Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to access in serial port
what the code
any one know about how ???
in c#

and win 7 32bit


plz help
Posted
Comments
agent_kruger 18-Nov-13 8:04am    
what do you want to do with ports send data to GSM modem or something others?

 
Share this answer
 
Comments
SoMad 24-Feb-13 18:46pm    
I like that number 1, 3 and 4 on that list are CodeProject articles.

Soren Madsen
Richard MacCutchan 25-Feb-13 6:05am    
Yet another reason why CodeProject is such a great site.
Sergey Alexandrovich Kryukov 24-Feb-13 23:38pm    
5ed.
—SA
 
Share this answer
 
Comments
SoMad 24-Feb-13 19:04pm    
:thumbsup: Nice links. In a comment to the first article, someone had posted a link to a page where he had created a simple walk-through. It looks like a good step-by-step guide for a beginner: http://thecomputerstudents.com/programming/c-sharp/serial-port-communications-in-c-sharp-with-example[^]

Soren Madsen
Thomas Daniels 25-Feb-13 12:54pm    
Thank you!
Sergey Alexandrovich Kryukov 24-Feb-13 23:39pm    
Agree with Soren, a 5.
—SA
Thomas Daniels 25-Feb-13 12:55pm    
Thank you!
Hi
try this:
C#
// Author: Jegan Thiyagesan
// Date: 12/02/2013

using System;
using System.IO.Ports;

namespace PortHandler
{
        /// <summary>
    /// Port handler that handles receiving and sending data over serial port.
        /// </summary>
    public class PortHandler
    {
        private const string Delimiter = "\r";
        private const string Rx = "Rx << ";
        private const string Tx = "Tx >> ";

        private string m_data;
        private SerialPort m_serialPort;

        /// <summary>
        /// Data received event handler
        /// </summary>
        public event EventHandler<EventArgs<string>> DataReceived;
        /// <summary>
        /// Data send event handler
        /// </summary>
        public event EventHandler<EventArgs<string>> DataSend;

        /// <summary>
        /// Error message event handler
        /// </summary>
        public event EventHandler<EventArgs<string>> ErrorMessage;

        /// <summary>
        /// Initiate data received event
        /// </summary>
        /// <param name="dataReceived"></param>
        public void OnDataReceived(string dataReceived)
        {
            var handler = DataReceived;
            if (handler != null)
            {
                handler(this, new EventArgs<string>(dataReceived));
            }
        }
        /// <summary>
        /// Initiate data send event
        /// </summary>
        /// <param name="dataSend"></param>
        public void OnDataSend(string dataSend)
        {
            var handler = DataSend;
            if (handler != null)
            {
                handler(this, new EventArgs<string>(dataSend));
            }
        }

        /// <summary>
        /// Initiate error message event.
        /// </summary>
        /// <param name="errorMessage"></param>
        public void OnErrorMessage(string errorMessage)
        {
            var handler = ErrorMessage;
            if (handler != null)
            {
                handler(this, new EventArgs<string>(errorMessage));
            }
        }

        /// <summary>
        /// return collections of available ports
        /// </summary>
        public string[] Ports
        {
            get
            {
                return SerialPort.GetPortNames();
            }
        }

        /// <summary>
        /// check is port open
        /// </summary>
        public bool IsPortOpen
        {
            get
            {
                return (m_serialPort != null && m_serialPort.IsOpen);
            }
        }

        /// <summary>
        /// basic constructor
        /// </summary>
        public PortHandler()
        {
        }

        /// <summary>
        /// Disconnect port
        /// </summary>
        public void DisconnectPort()
        {
            if (m_serialPort != null)
            {
                CloseSerialPort();
            }
        }

        /// <summary>
        /// Connect port
        /// </summary>
        /// <param name="portName"></param>
        /// <param name="baudRate"></param>
        public bool ConnectPort(string portName, int baudRate)
        {
            if (m_serialPort == null)
            {
                m_serialPort = new SerialPort
                                   {
                                       PortName = portName,
                                       BaudRate = baudRate,
                                       Parity = Parity.None,
                                       StopBits = StopBits.One,
                                       DataBits = 8,
                                       Handshake = Handshake.None,
                                       ReadTimeout = 500 //500 ms
                                   };

            }
            else
            {
                CloseSerialPort();
                m_serialPort.PortName = portName;
                m_serialPort.BaudRate = baudRate;
            }
            OpenSerialPort();
            return IsPortOpen;
        }

        /// <summary>
        /// Send data over port
        /// </summary>
        /// <param name="data"></param>
        public void SendData(string data)
        {
            if ((m_serialPort != null) && m_serialPort.IsOpen)
            {
                m_serialPort.DiscardOutBuffer();
                m_serialPort.Write(data + Delimiter);
                OnDataSend(Tx + data + Delimiter);
            }
        }

        private void OpenSerialPort()
        {
            try
            {
                m_serialPort.DataReceived += SerialPortDataReceived;
                m_serialPort.Open();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message + ex.StackTrace);
            }
        }

        private void CloseSerialPort()
        {
            if (m_serialPort.IsOpen)
            {
                m_serialPort.DataReceived -= SerialPortDataReceived;
                m_serialPort.Close();
            }
        }

        private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            var serialPort = (SerialPort)sender;
            var data = serialPort.ReadExisting();
            ProcessData(data);
        }

        private void ProcessData(string data)
        {
            Console.Out.WriteLine(data + "\r");
        }
    }
}


Regards
jegan
 
Share this answer
 
v2
Comments
SoMad 24-Feb-13 18:52pm    
The code needs to be modified in order to be generic. The "Delimiter" you insert at the end of each send makes the data specific to your implementation.

Soren Madsen
Jegan Thiyagesan 25-Feb-13 4:39am    
It is not specific to my implementation, it is to show that there should be a end of data mark. The user can leave it as it is or change it to something that they want once they know how to.

If I make the code generic, yes, the experienced developers will understand, BUT the beginners will get confused. This code is for a beginner who have never worked on serial comm before to get the gist of how to open a serial port and send/receive data.

Regards
Jegan
just add system.io class.
and create object of serial port and inthat have methods serialport.read and serial port.write.

if you read then serial port will read it and another software will read it. so this the the preocess of sending.
 
Share this answer
 
Comments
CHill60 18-Nov-13 11:15am    
Reason for downvote ... unclear and needs far more detail to be of any use
Add using system.IO.port namespace in you code behind class.

Add communication manager class

You will get this class from code project.

then create object of communication manager class.

and when you will read any data from serial port one datarecievedeventhandler event generates. in this event write conditions for reading data. then use comport.write and comport.read method.
 
Share this answer
 

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