Click here to Skip to main content
16,016,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,

I have a project that I want to control some home applications based on PIC16F688 and C#.

I designed a user interface graphic use C# and send to PIC a command.
When I send to microcontroller a command then turn on/off light and something like that.
This function is OK.

But when I send to microcontroller a command and I want to received a response data from temperature sensor (that connect to PIC).
I received all data have in buffer of RS232 (both send data and receive data).
How can I do to only receive response data (temperature value)?

Everybody help me please!
Posted
Updated 22-Jun-11 22:04pm
v2
Comments
Dalek Dave 23-Jun-11 4:07am    
Edited for Grammar and Readability.
CPallini 23-Jun-11 5:01am    
Possibly your microcontroller echoes received command (it is not unusual).

All you need is using the class System.IO.Ports.SerialPort, see http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx[^].

There is not problem to receive and send data at the same time. It actually depends on some application-level protocol dictating by your device, so you need to address to its documentation.

You cannot read "just temperature value".
This is serial communication. You need to read all what is sent to you and interpret this data on the fly. Your best option is creating a separate thread which unconditionally reads everything and push data to other thread when something happen. Read operation is blocking, so you don't want to block you main thread.

For appropriate inter-thread communication read my short Tips/Tricks article Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

To notify UI thread, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also my collection of links to my past answers on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

[EDIT]

Actually, not everything is so simple as I described. I recently realized that there is a delicate problem with a reading thread waken up and explained the issue and the solution in detail. Please see: thread.suspend, thread.resume methods[^].

—SA
 
Share this answer
 
v3
Comments
hgthan74 23-Jun-11 3:49am    
Thank you for you read and solute it. I don't understand yet how to remove the data in Buffer of RS232. Because the received data have my command I sent before.
Sergey Alexandrovich Kryukov 23-Jun-11 4:09am    
You remove data from buffer by reading data.
--SA
Dalek Dave 23-Jun-11 4:07am    
Great Answer.
Sergey Alexandrovich Kryukov 23-Jun-11 4:09am    
Thank you, Dalek. Hope it will help OP.
--SA
This is my code, you read help me please.
Thank you!
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.IO.Ports;
using System.Threading;
using System.IO;

namespace GTPT_688
{
    public partial class Form1 : Form
    {
        //byte ack = 0;
        //byte[] buff;
        string valueOfTemperature;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //configuring the serial port
            serialPort1.PortName = "COM5";
            serialPort1.BaudRate = 19200;
            serialPort1.DataBits = 8;
            serialPort1.Parity = Parity.None;
            serialPort1.StopBits = StopBits.One;
            serialPort1.DtrEnable = true;
            serialPort1.RtsEnable = true;
            //valueOfTemperature = 0;
            //buff = new byte[1];
            if (!serialPort1.IsOpen)
            {
                try
                {
                    serialPort1.Open();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
            }
        }
       private void Send_Click(object sender, EventArgs e)
       {
           if (ck1.Checked == true)
           {
               byte[] mBuffer = new byte[1];
               mBuffer[0] = 0x6F; //ASCII for o;.
               serialPort1.Write(mBuffer, 0, mBuffer.Length);
           }
           if (ck1.Checked==false)
           {
               byte[] mBuffer = new byte[1];
               mBuffer[0] = 0x66; //ASCII for f;.
               serialPort1.Write(mBuffer, 0, mBuffer.Length);
           }
                if (ck2.Checked==true)
                {
                    byte[] mBuffer = new byte[1];
                    mBuffer[0] = 0x74; //ASCII for t;.
                    serialPort1.Write(mBuffer, 0, mBuffer.Length);
                }
        }
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
               valueOfTemperature += serialPort1.ReadExisting();
               this.Invoke(new EventHandler(DisplayText));
        }
        private void DisplayText(object sender, EventArgs e)
        {
            //if (sender == serialPort1)
            //byte[] buff = new byte[serialPort1.BytesToRead];
            //serialPort1.Read(buff, 0, buff.Length);
            //string str = Encoding.ASCII.GetString(buff);
                lb1.Text = valueOfTemperature;
        }
        private void bn1_Click(object sender, EventArgs e)
        {

        }
    }
}
 
Share this answer
 
Comments
Dalek Dave 23-Jun-11 4:07am    
Good Call.

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