|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Mar 2008
Messaggi: 215
|
[C#] Evento lettura seriale
Ciao a tutti, mi piacerebbe inserire nell'evento per la lettura della SERIALE una modifica a txt1(L'oggetto di un form), ecco il codice:
Codice:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static SerialPort SERIAL;
public Form1()
{
InitializeComponent();
SERIAL = new SerialPort();
//SETTINGS
SERIAL.PortName = "COM5";
SERIAL.BaudRate = 9600;
SERIAL.ReadTimeout = 500;
SERIAL.WriteTimeout = 500;
SERIAL.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
SERIAL.Open();
txt1.Text = "SERIAL COM Opened!";
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
// !! !! !! !! !! !! !!
txt1.Text = indata;
}
private void button1_Click(object sender, EventArgs e)
{
SERIAL.WriteLine("1");
}
private void btn2_Click(object sender, EventArgs e)
{
SERIAL.WriteLine("2");
}
private void btn3_Click(object sender, EventArgs e)
{
SERIAL.WriteLine("3");
}
private void btn4_Click(object sender, EventArgs e)
{
SERIAL.WriteLine("4");
}
}
}
Codice:
Error 1 An object reference is required for the non-static field, method, or property Qualcuno che mi aiuti?
__________________
P.S. Il mio computer è un HP Pavilion DV5 1102el |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: May 2004
Città: Londra (Torino)
Messaggi: 3692
|
I thread non c'entrano.
Togli tutte le parole "static" dal codice.
__________________
Se pensi che il tuo codice sia troppo complesso da capire senza commenti, e' segno che molto probabilmente il tuo codice e' semplicemente mal scritto. E se pensi di avere bisogno di un nuovo commento, significa che ti manca almeno un test. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 09:24.










Qualcuno che mi aiuti? 








