Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,167 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,874 people online right now. Registration is fast and FREE... Join Now!




c# serial readline problems

 
Reply to this topicStart new topic

c# serial readline problems

bill2003
29 Dec, 2007 - 10:19 AM
Post #1

New D.I.C Head
*

Joined: 29 Dec, 2007
Posts: 2

i'm new to C# and i'm using the following code to read a serial port. it seems to work fine until i try to close the serial port. the program will hang and then crash. i will apreciate any help provided.

CODE

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 = "COM1";
            serialPort1.BaudRate = 9600;
            this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);        

            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.DiscardInBuffer;
                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)
        {
                    }

        private void DisplayText(object sender, EventArgs e)
        {
            textBox1.AppendText(RxString);
        }

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


Edit: Please use code tags when posting code smile.gif , like ==> code.gif

This post has been edited by PsychoCoder: 29 Dec, 2007 - 10:22 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: C# Serial Readline Problems
29 Dec, 2007 - 10:28 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
I don't know if it will completely solve your problem, but not a bad place to start. Heres a tutorial on Serial Port Communication in C# that may give you some ideas.
User is offlineProfile CardPM
+Quote Post

phatSolutions
RE: C# Serial Readline Problems
31 Dec, 2007 - 08:12 AM
Post #3

New D.I.C Head
*

Joined: 21 Dec, 2007
Posts: 12

QUOTE(PsychoCoder @ 29 Dec, 2007 - 11:28 AM) *

I don't know if it will completely solve your problem, but not a bad place to start. Heres a tutorial on Serial Port Communication in C# that may give you some ideas.


Does it supply and error message when crashing? If so what is the message?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:19AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month