Tom3333
asked on
serial port in c#
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:
Any idea what is going wrong ?
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) ));
}
}
}
}
Any idea what is going wrong ?
What is/are the error(s)?
ASKER
The errors are :
Error 1 The name 'DataReceivedHandler' does not exist in the current context C:\Visual Studio Projects\serial_port\seria l_port\For m1.cs 40 67 serial_port
Error 2 The name 'invoke' does not exist in the current context C:\Visual Studio Projects\serial_port\seria l_port\For m1.cs 49 17 serial_port
Error 3 The name 'richTextbox1' does not exist in the current context C:\Visual Studio Projects\serial_port\seria l_port\For m1.cs 49 40 serial_port
Error 1 The name 'DataReceivedHandler' does not exist in the current context C:\Visual Studio Projects\serial_port\seria
Error 2 The name 'invoke' does not exist in the current context C:\Visual Studio Projects\serial_port\seria
Error 3 The name 'richTextbox1' does not exist in the current context C:\Visual Studio Projects\serial_port\seria
1) Change line 40 to:
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.
sp.DataReceived += new SerialDataReceivedEventHandler(ReceivedHandrler);
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.
ASKER
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?
Why is not recognized it?
ASKER
any suggestions?
ASKER
any suggestions ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 :
UnauthorizedAccessExceptio n was unhandled
An unhandled exception of type 'System.UnauthorizedAccess Exception' occurred in System.dll
how to solve this problem ???
When i tried to run the code , i receive the message bellow :
at the line :
sp.Open();
UnauthorizedAccessExceptio
An unhandled exception of type 'System.UnauthorizedAccess
how to solve this problem ???
ASKER
any suggestions?
According to the documentation:
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).
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).