Solved

serial port in c#

Posted on 2014-09-30
10
351 Views
Last Modified: 2014-10-22
I try to write this code for a serial interface but there are some errors that I don't know how to solve it.

The Code which I used is presented below:

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

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

        void sErial(string Port_name)
        {
            SerialPort sp = new SerialPort(Port_name, 9600, Parity.None, 8, StopBits.One);
            sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            sp.Open();
        }
        private void ReceivedHandrler (object sender,SerialDataReceivedEventArgs e )
        {
            SerialPort sp = (SerialPort)sender;
            string w = sp.ReadLine();
            if(w != string.Empty)
            {
                invoke(new Action (()=>richTextbox1.AppendText(w) ));
            }

        }
    }
}

Open in new window


Any idea what is going wrong ?
0
Comment
Question by:Tom3333
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40353238
What is/are the error(s)?
0
 

Author Comment

by:Tom3333
ID: 40353248
The errors are :

Error      1      The name 'DataReceivedHandler' does not exist in the current context      C:\Visual Studio Projects\serial_port\serial_port\Form1.cs      40      67      serial_port

Error      2      The name 'invoke' does not exist in the current context      C:\Visual Studio Projects\serial_port\serial_port\Form1.cs      49      17      serial_port

Error      3      The name 'richTextbox1' does not exist in the current context      C:\Visual Studio Projects\serial_port\serial_port\Form1.cs      49      40      serial_port
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40353267
1) Change line 40 to:

sp.DataReceived += new SerialDataReceivedEventHandler(ReceivedHandrler);

Open in new window


2) Change "invoke" to "Invoke" (uppercase i) in line 49.

3) You need to check that you actually have a RichTextBox control on your form, and its name should be richTextBox1.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:Tom3333
ID: 40353516
the first and second errors are fixed. About the third i have a RichTextBox on the form with the name richTextBox1.

Why is not recognized it?
0
 

Author Comment

by:Tom3333
ID: 40354225
any suggestions?
0
 

Author Comment

by:Tom3333
ID: 40354227
any suggestions ?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40354287
If you type in:

this.rich

...within the editor, does Intellisense show the RTB?
0
 

Author Comment

by:Tom3333
ID: 40355869
with the change "this.rich"  now can compile successfully the code.

When i tried to run the code , i receive the message bellow :
at the line :
sp.Open();

Open in new window



UnauthorizedAccessException was unhandled
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.dll

how to solve this problem ???
0
 

Author Comment

by:Tom3333
ID: 40357865
any suggestions?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40357871
According to the documentation:

Access is denied to the port.
- or -
The current process, or another process on the system, already has the specified COM port open either by a SerialPort instance or in unmanaged code.

Check the permissions of the account you are running under, and then check to make sure that no other application has already opened that port. You may even try restarting Visual Studio just to make sure its not your own app locking it (from a previous run).
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question