Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello.

I have an issue with receiving and displaying in a text box.

I can send data with no problem (to the microcontroller) but I can`t display what the micro is sending.

I tried every tutorial available but still can`t receive.


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            getAvailablePorts();     
        }
        void getAvailablePorts()
        {
            string[] ports = SerialPort.GetPortNames();
            comboBox1.Items.AddRange(ports);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if(comboBox1.Text==""){
                    readBox.Text = "Select Port COM";
                }
                else
                {
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = 115200;
                    serialPort1.Parity = Parity.None;
                    serialPort1.StopBits = StopBits.One;
                    serialPort1.Handshake = Handshake.None;
                    serialPort1.Open();
                    initButton.Enabled = false;

                    sendButton.Enabled = true;
                    readButton.Enabled = true;
                    disconnectButton.Enabled = true;
                    //
                    serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                }
            }
            catch (UnauthorizedAccessException)
            {
                readBox.Text = "Error";
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.Text = serialPort1.PortName;    
        }

        private void sendButton_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine(input1.Text);
            input1.Text = "";         
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private string DispString; 
        
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            DispString = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
            
        }
        //
        private void DisplayText(object sender, EventArgs e)
        {
            readBox.AppendText(DispString);    
        }


        private void readButton_Click_1(object sender, EventArgs e)
        {
            try
            {
                //readBox.Text = serialPort1.ReadExisting();
                 
            }
            catch (TimeoutException)
            {
                readBox.Text = "timeout";
            }
        }

        private void disconnectBotton_Click(object sender, EventArgs e)
        {
            initButton.Enabled = true;
            sendButton.Enabled = false;
            readButton.Enabled = false;
        }

        private void readBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
Posted
Updated 17-Aug-15 6:00am
v2
Comments
CPallini 17-Aug-15 12:07pm    
Did you actually receive something (that is DataReceived handler is called)?
Member 11915058 17-Aug-15 12:53pm    
I never received one character, and I never get an event here.
But Im 100%, I can send some data.

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
DispString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));

}
CPallini 17-Aug-15 12:57pm    
"But Im 100%, I can send some data."
How are you sure? Do you receive data on microcontroller side?

I would put a breakpoint inside DAtaReceived handler and make a test with microcontroller sending data in a neverending loop.
Member 11915058 17-Aug-15 13:22pm    
"How are you sure? Do you receive data on microcontroller side?"
I send to microController with Visual Studio someting like : "this is from Visual"
and the I read the string variable with a "working Serial Terminal" and I receive: "this is from from Visual".

"I would put a breakpoint inside DAtaReceived handler and make a test with microcontroller sending data in a neverending loop."
I send every 500ms from the micro a word but can't reach the breakpoint inside DAtaReceived handler.

I tried to read the serial buffer with a readButton but have nothing.
private void readButton_Click_1(object sender, EventArgs e)
{

readBox.Text = serialPort1.ReadExisting();

}
CPallini 17-Aug-15 15:17pm    
Reading with a 'working Serial Terminal' is just a proof you are able to send. Still maybe serial communication at the Microcontroller side is broken. Your C# application is able to receive from such 'working Serial Terminal'?

1 solution

OMG!

I found it, in port setting

DtrEnable=True

Kudos for
VB
Ed Tenholder
 
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