3 Replies - 2504 Views - Last Post: 31 August 2012 - 03:33 AM Rate Topic: -----

#1 remyamanoj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-October 09

Send data from textbox to serialport using c#

Posted 31 August 2012 - 01:10 AM

could anyone tell me how to send data from multiple textboxes to serialport.iam using 4 textboxes and i need the data entered in the textboxes to be send to serialport to pic and get output from pic.when iam sending data only one textbox data is send through serialport.here is 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;

namespace testing2
{
    public partial class Form2 : Form
    {
        Formload callingForm;
        public Form2(Formload form)
        {
            InitializeComponent();
            callingForm = form;
        }


        private void sndbtn_Click(object sender, EventArgs e)
        {
            if (!callingForm.serialPort1.IsOpen)
                callingForm.serialPort1.Open();

            float Mx1;
           float Mx2;
           float Mx3;
           float Mx4;
            float Cx1;
           float Cx2;
            float Cx3;
            float Cx4;
           Mx1 = float.Parse(txtMx1.Text);
            Cx1 = float.Parse(txtCx1.Text);
           Mx2 = float.Parse(txtMx2.Text);
            Cx2 = float.Parse(txtCx2.Text);
         Mx3 = float.Parse(txtMx3.Text);
            Cx3 = float.Parse(txtCx3.Text);
            Mx4 = float.Parse(txtMx4.Text);
           Cx4 = float.Parse(txtCx4.Text);
           string s = "\x2" + "a" + txtMx1.Text + "b" + txtCx1.Text + "\x3" + "\x2" + "c" + txtMx2.Text + "e" + txtCx2.Text + "\x3"+"\x2" + "w" + txtMx3.Text + "x" + txtCx3.Text + "\x3"+"\x2" + "y" + txtMx4.Text + "z" + txtCx4.Text + "\x3";
            char[] cArr = s.ToCharArray();

            foreach (char c in cArr)
            {
                char[] buff = { c };

                callingForm.serialPort1.Write(cArr, 0, s.Length);
            }
        }

        
    }
}

This post has been edited by JackOfAllTrades: 31 August 2012 - 03:27 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Send data from textbox to serialport using c#

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Send data from textbox to serialport using c#

Posted 31 August 2012 - 01:29 AM

Have you debugged the value of s and cArr in your code? What about stepping through each character being sent to the serial port?

Also, why do you do a float parse and then ignore those variables to construct a string?
Was This Post Helpful? 0
  • +
  • -

#3 remyamanoj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-October 09

Re: Send data from textbox to serialport using c#

Posted 31 August 2012 - 03:22 AM

when debugging iam getting proper output,but not getting it in my PIC
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Send data from textbox to serialport using c#

Posted 31 August 2012 - 03:33 AM

If Write is being called properly, the next item to check would be if BytesToWrite has anything in it, thus implying that the data hasn't yet being sent.

This is not normally the case for serial port communications, though. The best way to find out if the problem is with your application of the PIC would be to monitor the actual data that is being sent (using some form of serial port traffic monitor) and ensuring that it is indeed being sent. If it is, the problem is with your PIC.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1