Author Topic: Writing and Reading Data from the PIC 16f877 via the Serial Port  (Read 26853 times)

Olakunle

  • Member
  • ***
  • Posts: 17
Dear Sirs/Mas
Please for the first time, I would like to interface a C# 2008 application with a pic 16f877 microcontroller so as to write a string of data and have the microcontroller echo back it received texts. i.e not just a single byte.
I already have a usb to TTL device to communicate serially with the pic 16f877. both when the Tx and Rx of the usb to TTL device are directly connected together and when the Tx of TTL Device connects with Rx of '877 and Tx of '877 connects with Rx of TTL Device, no data was received after clicking the Send button.
As shown in the C# Code below, I have a command button that when clicked invokes the GetPortName to populate the Combobox with the port names. Then when the Send button is clicked the selected port name and data are both sent to the code that helps write the data to the pic 16f877 Rx port C pin 7. Then the pic is expected to write the data back to the Windows 7 C# 2008 Application. When the Application was runned, no data was received. The C# 2008 App code and the PIC Code are both pasted below.
Please ma kindly help me to correct the codes. or how do you think I should get started with With Windows App serial port interfacing with the pic microcontroller ? I have read part of your book serial port complete that have helped me thus far with some serial port understanding but I havent still been able to receive Data from the pic microcontroller. Please kindly help me. Thanks.

Kunle.

C# 2008 Application 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 SerialPortApp_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();

            foreach (string port in ports)
            {
                comboBox1.Items.Add(port);
            }
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            string t = comboBox1.Text.ToString();
            string s = richTextBox1.Text.ToString();
            sErial(t, s);
           
        }
        void sErial(string Port_Name, string Data_Send)
        {
            SerialPort sp = new SerialPort(Port_Name, 115200, Parity.None, 8, StopBits.One);
            string t = comboBox1.Text.ToString();
            sp.Open();
            sp.Write(Data_Send);
            sp.Close();
            sErial2(t);
           
         }
        SerialPort sp;
        void sErial2(string Port_Name)
        {
            sp = new SerialPort(Port_Name, 115200, Parity.None, 8, StopBits.One);
            sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            sp.Open();
        }
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string w = sp.ReadLine();
            //string msg = sp.ReadExisting();

            if (w != String.Empty)
            {
                Invoke(new Action(() => richTextBox2.AppendText(w)));
            }
        }


       
       

    }
}

PIC 16f877 code

void newline(){
UART1_Write(13);
UART1_Write(10);
}
char i;
 void main() {
 TRISB = 0;

 PORTB = 0;
 PORTC = 0;
 TRISC = 0xBF;

 UART1_Init(115200);
 Delay_ms(100);
 while(1){
 if(UART1_Data_Ready()== 1){
  UART1_Read_Text(i, ".", 30);
  UART1_Write_Text(i);
  newline();
}
}
}

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Writing and Reading Data from the PIC 16f877 via the Serial Port
« Reply #1 on: February 23, 2016, 10:31:50 am »
When you say "no data was received," do you mean that you have verified that the PC has sent no data to the PIC, or do you mean that the PC did not receive a response from the PIC?

If it's the first, I would start by using whatever debugging tools you have to verify that the PIC received the data.

I have example code here:

http://janaxelson.com/serport.htm

Olakunle

  • Member
  • ***
  • Posts: 17
Re: Writing and Reading Data from the PIC 16f877 via the Serial Port
« Reply #2 on: February 23, 2016, 10:48:06 am »
I mean the PC did not receive a response from the PIC?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Writing and Reading Data from the PIC 16f877 via the Serial Port
« Reply #3 on: February 23, 2016, 11:49:25 am »
To isolate the problem, use whatever debugging tools you have (breakpoints, watch variables, etc.) to find out if the PC sent the data to the PIC and if the PIC sent the data back. That will give you an idea of where to start looking for the problem.


Olakunle

  • Member
  • ***
  • Posts: 17
Re: Writing and Reading Data from the PIC 16f877 via the Serial Port
« Reply #4 on: February 24, 2016, 06:43:23 am »
Thanks for your help so far.
The PC App have started receiving data from the PIC.
the problem that remain is that when I typed Olakunle.
on the richtextbox1, I expected the pic to capture that data
and reply me with
"Welcome to the Pacific Energy :
Olakunle" on the richtextbox2
however, it now only display
Welcome to the Pacific Energy : without including the received Olakunle.
Please how can I know or make the pic receive my sent data from the PC and have the pic reply with
Welcome to the Pacific Energy :
Olakunle
or the pic reply with Welcome to the Pacific Energy :
and any other data I transmitted to the pic.
 the code is as shown below.
Thanks.
Kunle.

PC Serial App 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 SerialPortApp_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();

            foreach (string port in ports)
            {
                comboBox1.Items.Add(port);
            }
        }

       
           

        SerialPort sp;
        void sErial(string Port_Name)
        {
            sp = new SerialPort(Port_Name, 9600, Parity.None, 8, StopBits.One);
            sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            sp.Open();
        }
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string w = sp.ReadLine();
            //string msg = sp.ReadExisting();

            if (w != String.Empty)
            {
                Invoke(new Action(() => richTextBox2.AppendText(w)));
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            string t = comboBox1.Text.ToString();
            sErial(t);
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string t = comboBox1.Text.ToString();
            string s = richTextBox1.Text.ToString();
            sErial2(t, s);

        }
        void sErial2(string Port_Name, string Data_Send)
        {
            sp = new SerialPort(Port_Name, 9600, Parity.None, 8, StopBits.One);
            sp.Open();
            sp.Write(Data_Send);
            sp.Close();
        }

       
       

    }
}

PIC Code

void newline(){
UART1_Write(13);
UART1_Write(10);
}
char txt[60];
 void main() {

 UART1_Init(9600);
 UART1_Write_Text("What is your Name ?");
 if(UART1_Data_Ready()==1){
 UART1_Read_Text(txt, ".", 8);
 while(1){
  UART1_Write_Text("Welcome to the Pacific Energy : ");
  Delay_ms(1000);
  newline();
  UART1_Write_Text(txt);
   Delay_ms(1000);
   newline();
   }
}
}

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Writing and Reading Data from the PIC 16f877 via the Serial Port
« Reply #5 on: February 24, 2016, 11:09:40 am »
ReadLine waits for one line of text. If there are multiple lines, you need to call ReadLine again. Set the port's ReadTimeout property to time out if no data arrives. You could call ReadLine continuously in a loop that ends on a timeout.

Olakunle

  • Member
  • ***
  • Posts: 17
Re: Writing and Reading Data from the PIC 16f877 via the Serial Port
« Reply #6 on: March 12, 2016, 02:44:25 pm »
Dear Mrs Jan Axelson,
Thank you for all the help you have given me so far on my serial port application.
I now have a bidirectional communication between the pic microcontroller and the Windows 7 visual C# 2008 app although it's only half duplex communication.
The only problem that remains on my getting started with the serial port app is plotting the graph of the received data
On let say matlab or any other available data plotting tool. My frequency counter device sends a 2 digit integer data to Windows app at every 1 seconds after
Measuring the data for 1 seconds. Please how should I get started with plotting real time data on Windows.
Thanks.
Kunle.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Writing and Reading Data from the PIC 16f877 via the Serial Port
« Reply #7 on: March 12, 2016, 08:20:19 pm »
I'm glad to hear you got the communications working.

A web search on:

.net graph data matlab

brought this result with some suggestions and examples:

http://stackoverflow.com/questions/10119192/matlab-plotting-tools-in-c-sharp-application

Olakunle

  • Member
  • ***
  • Posts: 17
Re: Writing and Reading Data from the PIC 16f877 via the Serial Port
« Reply #8 on: March 22, 2016, 12:34:37 pm »
please how can I stop the visual c# datareceived event which seems to forever read data from the serial port to stop receiving data and also close the port at a button's click event.
What I mean is that when I click the read frequency button, it should open d port and start reading the frequency data. but when I click the stop reading frequency button, it should stop the reading of data and also close the port. when I click read frequency button again, it should resume the reading of the frequency data. Thanks.
Kunle.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Writing and Reading Data from the PIC 16f877 via the Serial Port
« Reply #9 on: March 23, 2016, 10:56:07 am »
You can open the port in one click event's code and close the port on the other click event's code. In my example code, see OpenComPort and CloseComPort.

Or you could unsubscribe from the events using -=

SelectedPort.DataReceived -= SerialDataReceivedEventHandler1;

See:
http://stackoverflow.com/questions/9101766/serialport-close-and-event-handlers