Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hello Good Fellows.
Trying to receive data, from mk, using DataReceived and handler event, what i do is -
push a button on a program(code is below) then LED on mk will torn on, then the data should be sent back to program(expecting 1, on byte value, but also tried string value, doesnt work). Sennding side is working, but recieving....not
seems like im missing something. Any help apreciate it. Thx in Further

I checked with mdsn console sample, i receiving data well.
So the problem i think in code above.
There are a sample:
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx

What I have tried:

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 WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
         
        
        }
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) // As i understood, here we configure where i data will be shown,
                                                                                       // trying to get it on TextBox1
        {

            SerialPort sp = (SerialPort)sender;
            richTextBox1.Text += sp.ReadExisting() + "\n";
        }

        private void button1_Click(object sender, EventArgs e)                                      // There are a main actions, first i receive data then send data by a click.    
        {
            serialPort1.Write("\u0001");
            serialPort1.Close();

            System.ComponentModel.IContainer components = new System.ComponentModel.Container();  //  
            serialPort1 = new System.IO.Ports.SerialPort(components);
            serialPort1.PortName = "COM4";
            serialPort1.BaudRate = 9600;
            serialPort1.DtrEnable = true;
            serialPort1.Open();
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

          
        }
    }
}
Posted
Updated 29-Jan-17 19:09pm
v3
Comments
Richard MacCutchan 30-Jan-17 4:44am    
The code in your button1_Click method is out of order; how does it even compile?

1 solution

First thing to do is debug serial link.
You need to find if microcontroler fail to send answer or the windows app fail to receive it, or if both fail.
Use a terminal emulator instead of your app and send the command manually, if microcontroler works, you will the answer on terminal. Then do the same on the other side and see if it fail or not.
This will tell you if cable works and help to narrow the research.

I don't know where you found examples of serial com, but it is weird.
The order of things is:
- set serial parameters
- open comm
- send command
- wait for answer
- close comm
 
Share this answer
 
v2
Comments
JeezyWonder 30-Jan-17 1:06am    
I checked with mdsn console sample, i receiving data well.
So the problem i think in code above.
There are a sample:
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx
Patrice T 30-Jan-17 1:07am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
S Houghtelin 30-Jan-17 8:06am    
Yes, you are correct, the problem is in your code. I will ask you a question; If you close the serial port before setting up the receive event, how is the serial port going to receive new data if it is closed? Go back to the sample you linked to and carefully note the order as ppolymorphe so clearly laid out for you.

Look at it this way, if someone knocks on your door and you say "come in", and then you close the door in their face, how are they to enter?
JeezyWonder 30-Jan-17 8:34am    
Thanks. i changed it, now i open and close ports by button clicks:
private void button2_Click(object sender, EventArgs e)
{ serialPort1.Open(); }
private void button3_Click(object sender, EventArgs e)
{serialPort1.Close();}
But the result is the same (((
Patrice T 30-Jan-17 8:38am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

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