Click here to Skip to main content
16,008,942 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

im working on "serial Port".

It's new for me, so Please help to solve my Problem.

How do I read from Serial port and write to serial port via C#?

I tried it, but I didn't get proper result.
I can connect to the com port but i cant read the data
I give a my code.
Please verify and give me a solution.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication1

{
    
   
    
    public partial class Form1 : Form
    {
        
    
       
        private delegate void SetTextDeleg(string text);
        string data = string.Empty;
        public Form1()
        {
            InitializeComponent();

           
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            for (int i = 0; i < ports.Length; i++)
            {
                comboBox1.Items.Add(ports[i]);
            }
        }
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                
                serialPort1.PortName = "COM1";
                serialPort1.BaudRate = 9600;
                serialPort1.DataBits = 8;
                serialPort1.Parity = Parity.None;
                serialPort1.StopBits = StopBits.One;
                serialPort1.Open();
                serialPort1.ReadTimeout = 2000;
                serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
                MessageBox.Show("Success to Connect");
                serialPort1.Close();
               
               
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed because" + ex.Message);

            }
        }



          private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

              
            Thread.Sleep(500);
            data = serialPort1.ReadLine();
           
            this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data }); 
            
             
        }

       
        private void si_DataReceived(string data)
        {
            listBox1.Items.Add(data);
        }
Posted
Updated 1-Feb-13 0:33am
v2

If you close the serial port immediately after you've open it, how can it possibly receive any data?
Remove the
Quote:
serialPort1.Close();

line in the button1_Click event handler.
 
Share this answer
 
I think is that you are hard coding the portname in Button click event i.e., COM1
But you mentioned clearly in combobox you got lot of ports that are available in the system.

If your device is connected to COM3. you will get definitely an error because you are reading from COM1 only.
So don't hard code you COM PORT.

And try to close your ports when you finished up with you job using close().
 
Share this answer
 
Comments
Member 8608811 1-Feb-13 22:54pm    
i removed the line
serialPort1.Close(); in button1_click()
but still i am not able to read the data.
I have checked with all the available ports its connected to the COM1 itself and even if i don't hard code the port name ,i am not able to read the data,plz may i know some other solution

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