5 Replies - 4133 Views - Last Post: 05 October 2010 - 11:59 AM Rate Topic: -----

#1 kaspernet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-October 10

serial port convert string to int problem

Posted 02 October 2010 - 11:14 AM

hi
I send data de type string throw serial port and i receive it by my program built by c#
my data is numbers from 0 to 100 but i convert it string before i send it throw serial port to my program built by c#

my code convert it when receive frist numer 1 or 55 or 4 (from 0 to 100) and when my program receive second number cann't convert it. i don't know which the problem help me please

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SimpleSerial
{
    public partial class Form1 : Form
    {
        // Add this variable

        string RxString; 
                       
        public Form1()
        {
            InitializeComponent();
            
        }

        private void buttonstart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM2";
            serialPort1.BaudRate = 9600;
            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                buttonstart.Enabled = false;
                buttonstop.Enabled = true;
                textBox1.ReadOnly = false;
            }
        }

        private void buttonstop_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                buttonstart.Enabled = true;
                buttonstop.Enabled = false;
                textBox1.ReadOnly = true;
            }

        }

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

       private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // If the port is closed, don't try to send a character.
            if (!serialPort1.IsOpen) return;

            // If the port is Open, declare a char[] array with one element.
            char[] buff = new char[1];

            // Load element 0 with the key character.
            buff[0] = e.KeyChar;

            // Send the one character buffer.
            serialPort1.Write(buff, 0, 1);

            // Set the KeyPress event as handled so the character won't
            // display locally. If you want it to display, omit the next line.
            e.Handled = true;
        }

        private void DisplayText(object sender, EventArgs e)
        {  eepro = "00";
           textBox1.AppendText(RxString);

          
           thermometer1.Value = Convert.ToInt32(RxString); 
       
        }

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

       private void Form1_Load(object sender, EventArgs e)
        {    
            
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: serial port convert string to int problem

#2 lordofduct   User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2668
  • View blog
  • Posts: 4,786
  • Joined: 24-September 10

Re: serial port convert string to int problem

Posted 02 October 2010 - 01:59 PM

Have you tried displaying RxString with out converting it? What is the value specifically? Maybe it's getting buggered up in transfer and isn't a numeric value anymore.

Also, if you want to do an invoke, try the 'Action' delegate type... like so:

        private void DisplayText(string sval)
        {
            eepro = "00";
            textBox2.AppendText(sval);
            thermometer1.Value = Convert.ToInt32(RxString);
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string sval = serialPort1.ReadExisting();
            this.Invoke(new Action<string>(this.DisplayText), sval);
        }


Was This Post Helpful? 0
  • +
  • -

#3 kaspernet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-October 10

Re: serial port convert string to int problem

Posted 03 October 2010 - 06:25 AM

Have you tried displaying RxString with out converting it? What is the value specifically? Maybe it's getting buggered up in transfer and isn't a numeric value anymore
-------------
yes, and i receive number in textbox. if i send 1 i receive 1 if i send 37 i receive it without any problem.
but when i try to covert number received in textbox my code juste convert the frist data and don't work more (bug)
Was This Post Helpful? 0
  • +
  • -

#4 kaspernet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-October 10

Re: serial port convert string to int problem

Posted 03 October 2010 - 09:10 AM

mybe i must Clear Buffer before my Second Convert.ToInt32(RxString); so how can do that?
Was This Post Helpful? 0
  • +
  • -

#5 kaspernet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-October 10

Re: serial port convert string to int problem

Posted 04 October 2010 - 11:27 AM

my program juste convert the frist data received from serial port than it crush?
Was This Post Helpful? 0
  • +
  • -

#6 kaspernet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-October 10

Re: serial port convert string to int problem

Posted 05 October 2010 - 11:59 AM

help me please
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1