dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Serial Communication Data Receive

Hybrid View

  1. #1
    Join Date
    Jun 2015
    Posts
    2

    Serial Communication Data Receive

    Hello Everyone

    I have a board attached with some sensors.I need to perform these steps.
    1:.Send some commands to microcontroller and then read the data after sending each command.
    2:. Then I have to do some calculations on received data and then saved it in to a text file.

    I am stuck at data read from Serial Port.
    I know how to write data but I don't know how to call data read event every time(after sending different commands to microcontroller).
    Here is my code.Your help will be appreciated.

    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 WindowsFormsApplication2
    {
    public partial class Form1 : Form
    {

    public Form1()
    {

    InitializeComponent();
    comboBox1.Items.AddRange(SerialPort.GetPortNames());
    serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
    }
    private void button1_Click(object sender, EventArgs e)

    {
    serialPort1.PortName = comboBox1.Text;


    serialPort1.Open();
    serialPort1.Write("$g helloboard!");

    }
    string abc;
    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {

    int bytes = serialPort1.BytesToRead;
    byte[] buffer = new byte[16];
    serialPort1.Read(buffer, 0, 16);

    System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
    abc = enc.GetString(buffer);


    }
    }

    }

  2. #2
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: Serial Communication Data Receive

    DataReceived event will be fired when the serial interface gets any data into its buffer. Your code seems fine, but you might be missing the Baudrate setup? Your board might not be understanding what you're sending.

    You could use 'bufer = new byte[bytes]' and then 'ReadExisting()' into buffer. Might avoid data loss.

    And I would also wrap the serialPort1.Open() in a try-catch block, since it might be in use already and that will throw an exception. This would be very unlikely in your case since you're getting the ports list from SerialPort.GetPortNames() and GetPortNames will only return available ports, but still... anything can happen from the point you ask for the list and the moment you actually decide to use one of them.

    Also, and this really is very important, don't forget to close the port after using it. The port will not close if you change port number and it might even be left open after your application exits (and only your application can close the port since it's its "owner" at that time). I've had serious problems with Windows (XP at least) not closing ports, even after restarting a billion times. I had to disable the port, restart, re-enable and restart.

  3. #3
    Join Date
    Jun 2015
    Posts
    2

    Re: Serial Communication Data Receive

    Thank you Nikel for your reply.
    Actually your suggestions are not clear to me regarding data receive event. Are you saying that data receive event will be automatically fired after sending command?
    Problem is I want to to receive data multiple times(after sending different commands). How can I achieve this. Can you do some changings in code inside button_1 where I want to send and receive data and process data. Specifically inside this button.

    Regards

  4. #4
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: Serial Communication Data Receive

    No, DataReceived Event will be fired when (every time) the PC-side serial interface receives data. The PC (your C# program) sends data to the Board, the board then replies to the PC, that's when DataReceived will fire. As far as I can see, you are missing the Baudrate, so when the PC sends data to the board, the Board is not understanding the message, thus it never replies and DataReceived is never fired.

    What you should include in the button1_Click event is:

    serialPort1.Baudrate = 9600? 14400? What is the Board expecting?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)