Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

c# serial textbox problem

Status
Not open for further replies.

aswathymohan

Member level 4
Joined
Nov 11, 2012
Messages
68
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,288
Activity points
1,727
Hi
I wann do create an form having button,textbox,serialport in visual c# 2010.My objective is when a button press the data coming from the serial port has to displayed in textbox.I copied a code from a tutorial.textbox is not showing anything,pl help..i am not familiar with c#.pl help.its urgent..here's is the code


Code C# - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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 ser1
{
    public partial class Form1 : Form
    {
        private string RxString;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM4";
            serialPort1.BaudRate = 9600;
 
            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                textBox1.ReadOnly = false;
            }
 
        }
 
        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)
        {
            textBox1.Text=RxString.Trim();
            textBox1.Show="h";
        }
 
 
       
 
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            RxString = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
 
        }
 
        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_TextChanged(object sender, EventArgs e)
        {
 
        }
    }
}

 

doraemon

Super Moderator
Staff member
Joined
Jun 21, 2009
Messages
1,068
Helped
273
Reputation
554
Reaction score
248
Trophy points
1,343
Location
Japan
Activity points
10,597
Hello!

This is a microcontroller group. You might want to write this to a more appropriate place.

Dora.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top