Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

serial communication using GUI

Status
Not open for further replies.

jerkymotion

Member level 4
Joined
Oct 12, 2010
Messages
73
Helped
28
Reputation
56
Reaction score
28
Trophy points
1,298
Location
NEPAL
Activity points
1,824
hi
I've already made GUI in C# that is used to transfer the serial data from PC to microcontroller via serial port...............
it works pretty well when i am transmitting from PC but i am unable to recieve the data in my GUI from pic16f877a....also i want to plot the graphs of my recieved data..................
please help me .....every helo shall be appreciated
 

have you checked the PC transmits and receives serial data OK? try connecing a couple of PCs back to back using a null modem (pins 2 and 3 crossed).
When the PIC transmits do you receive anything (even rubbish)? if so the baud rate is probably wrong. Check your clock frequency and UART divisors.
 
how do you know the PIC16 is receiving data if you cannot send an acknowledgement back to the PC?
Have you tried putting an oscillosope on the Rx and Tx pins of the c16 to see if anything is happening?
have you checked the SPBRG values etc for the correct baud rate setting
the following is any example I used on the Microchip PICDEM Mechatronics board many years ago
Code:
__CONFIG (EC & WDTDIS & PWRTDIS  & UNPROTECT &  MCLREN & BORDIS & FCMEN & IESOEN);

void UARTInitialise(void)
{
        TXSTA=0;                        // set up transmitter
        TXEN=1;                         // Asychronous, 8 bit, BRGH=1
        BRGH=1;
        TRMT=1;
        TRISC6=0;                       // C6 - TX  set as output pin

        RCSTA=0;                        // set up receiver
        SPEN=1;                         // Asychronous, 8 bit, continuous receive, serial enable
        CREN=1;    
        TRISC7=1;                       // C7 - RX receive set as input pin

// calculate Baud Rate = FOSC/(16 (X + 1))
//#define BaudRate      51              //SPBRG value for 9600 Baud when Fosc=8MHz and BRGH=1
#define BaudRate    8                   //SPBRG value for 57600 Baud when Fosc=8MHz and BRGH=1
        SPBRG=BaudRate;                 // setup baud rate
 
actually i sent it through the GUI to display in LCD(some characters)...it worked welll
but when i wanted any data such as volage for example could not recieve in my gui.....Infact i have no idea how to recieve in gui made in c#
please help
 
  • Like
Reactions: ssingh

    ssingh

    Points: 2
    Helpful Answer Positive Rating
Using Visual studio C++ (and other languages etc) I use a sperate thread to recieve serial data - it is then copied into a TextArea or similar component
I am sure C# can create sperate threads of control - have a look in the Toolbox (it is called backgroundWorker in C++)
 
PLEASE help still not solved......
i need not only to recieve data in gui but also to plot the real time graph as well in gui.........
if C# is not good ....please suggest me other software which would complete my task easily
 
  • Like
Reactions: ssingh

    ssingh

    Points: 2
    Helpful Answer Positive Rating
C#, C++, Java could all do this it depends what you are used to
I would use Matlab (or its free equivalent Octave) as graph plotting is simple- however, if you have never used it and you have C# that would do OK
I have just looked at my Visual Studio 2008 C# and in the Toolbox under Components is BackgroundWorker which will create a seperate thread for you to receive data
 
hi

if could post the c# code that you have tried for receiving data from serial port, somebody could tell you where the mistake is.

serial port component has datareceived event which you use for copying received data to buffer or textbox.

once you have the data graph creation part should be easy. i had done a similar project using attiny2313.
 
I was able to transmit my data from PC via serial port and monitored in software (serial port monitor)...my code is as folllow:
serialPort1.PortName = "COM1";
serialPort1.BaudRate = bRate;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;

//opening the serial port
serialPort1.Open();

//write data to serial port
serialPort1.Write(tex);

//close the port
serialPort1.Close();


but...i have completely no idea to get the any data back from the PIC 16f877a in my PC.....please help
 

I did a quick web search on "C# serial port" and it turned up a number of links including
Communicating with Serial Port in C#

you will probably have to created a thread (called a BackgroundWorker in Visual Studio toolbox) to deal with the serial data received
 
thank u so much...
but what i really needed is to build GUI for serial communication purposes..the one u suggested me was console application where GUI can't be build...
please help me
 

you create a GUI
(1) with a TextBox - when a key is pressed you transmit it down the serial port
(2) with a BackgroundWorker - this has loop reading characters from the serial line and adding them to the Textbox

do part 1 first - when that works do part 2
if you have problems post your code
 
here i tried to capture the data in textbox2 where i sent the same data from textbox1.(i sent serially data from textbox1 and wanted to recieve the data in serial port in textbox2 ) I monitored my program in serial port monitor where my data sending task was successfully completed but my recieving task was not completed....
i might be completely wrong here please help me to recieve the data serially in my gui
my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace recieving_in_serial_port
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

string ss,Rx;
private void button1_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;

//opening the serial port
serialPort1.Open();

//write data to serial port
serialPort1.Write(ss);

//close the port
serialPort1.Close();


}

private void textBox1_TextChanged(object sender, EventArgs e)
{
ss = textBox1.Text;

}


private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}

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

private void DisplayText(object sender, EventArgs e)
{
textBox2.AppendText(Rx);
}

}
}
please help
 

I used backgroundWorker with a USB receiver - as you say we can use an serial event handler to receive data
using Visual C++ I created a form with two textboxes 1 for keypresses and sending data and 2 to display received data. This what it ended up looking like (I don't use Visual Studio unless I can avoid it so the code is not elegant)
Code:
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			serialPort1->Open();
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::IO::Ports::SerialPort^  serialPort1;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::ComponentModel::IContainer^  components;
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->SuspendLayout();
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(12, 174);
			this->textBox1->Multiline = true;
			this->textBox1->Name = L"textBox1";
			this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Both;
			this->textBox1->Size = System::Drawing::Size(515, 150);
			this->textBox1->TabIndex = 0;
			this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::textBox1_KeyPress);
			// 
			// serialPort1
			// 
			this->serialPort1->BaudRate = 19200;
			this->serialPort1->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::serialPort1_DataReceived);
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(12, 12);
			this->textBox2->Multiline = true;
			this->textBox2->Name = L"textBox2";
			this->textBox2->ScrollBars = System::Windows::Forms::ScrollBars::Both;
			this->textBox2->Size = System::Drawing::Size(515, 156);
			this->textBox2->TabIndex = 1;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(594, 351);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void textBox1_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
		// key hit over textbox send down serial line
		cout << "key press " << char(e->KeyChar) << endl;
		char data[2]="";
		data[0]=char(e->KeyChar);
		String ^sdata=gcnew String(data);
		serialPort1->Write(sdata);
		}
	private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {
		// character received from serial line, display it in textbox
		String^ message = serialPort1->ReadLine();
                textBox2->Text = textBox2->Text + message + "\r\n";				 
			 }
	};
}
when I connect it to a Microchip Exploer 16 witha PIC24 it displays data received and when I hit a key the explorer echos it back OK
 
can u please post the code in c sharp performing the similar task
 

below is a C# terminal emulator - I don't know C# so it is not very elegent
it contains
1. a TextBox for display of sent and received characters - when a key is pressed it is transmitted down the serial port
2. a SerialPort
3. a BackgroundWorker - a thread that receives serial port data and displays on text box (gave up trying to use SerialPort DataReceived event it was very slow and appeared to loose data)

This worked OK with Explorer 16 running at 115200baud
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CsharpTerminal
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            serialPort1.Open();
            backgroundWorker1.RunWorkerAsync();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // key on textbox pressed, read key and transmit down serial line
            Console.WriteLine("keypress " + e.KeyChar);
            char[] data = { e.KeyChar };
            string s = new string(data);
            serialPort1.Write(s);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Console.WriteLine("background worker");
            while(true)
            {
                // check for characters received from serial port and display on textbox
                char[] data=new char[100];
                serialPort1.Read(data,0,100);
                // replace "\n" with "\r\n" for newlines in textbox
                char[] data2 = new char[200];
                int i, j;
                for (i = j = 0; i < 100; i++)
                    if(data[i]=='\n')
                        { data2[j++] = '\r'; data2[j++] = '\n'; }
                    else
                        data2[j++]=data[i];
                string sdata = new string(data2);
                textBox1.AppendText(sdata);
            }
        }
    }
}
 
thank u very much...
would you mind posting the whole code....
because i have no abou what to include in header section(using system ......
 

@horace1
would you mind posting complete code for this one which you previously posted

public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
serialPort1->Open();
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
private: System::IO::ports::SerialPort^ serialPort1;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::ComponentModel::IContainer^ components;
protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>


#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->serialPort1 = (gcnew System::IO::ports::SerialPort(this->components));
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Location = System::Drawing::point(12, 174);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Both;
this->textBox1->Size = System::Drawing::Size(515, 150);
this->textBox1->TabIndex = 0;
this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::textBox1_KeyPress);
//
// serialPort1
//
this->serialPort1->BaudRate = 19200;
this->serialPort1->DataReceived += gcnew System::IO::ports::SerialDataReceivedEventHandler(this, &Form1::serialPort1_DataReceived);
//
// textBox2
//
this->textBox2->Location = System::Drawing::point(12, 12);
this->textBox2->Multiline = true;
this->textBox2->Name = L"textBox2";
this->textBox2->ScrollBars = System::Windows::Forms::ScrollBars::Both;
this->textBox2->Size = System::Drawing::Size(515, 156);
this->textBox2->TabIndex = 1;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(594, 351);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void textBox1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
// key hit over textbox send down serial line
cout << "key press " << char(e->KeyChar) << endl;
char data[2]="";
data[0]=char(e->KeyChar);
String ^sdata=gcnew String(data);
serialPort1->Write(sdata);
}
private: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::ports::SerialDataReceivedEventArgs^ e) {
// character received from serial line, display it in textbox
String^ message = serialPort1->ReadLine();
textBox2->Text = textBox2->Text + message + "\r\n";
}
};
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top