none
Muncul error Error: The name 'txtData' does not exist in the current context_

    Pertanyaan

  • Dear Master,

    Saya ingin mencetak data dari serial port dengan event serialreceive pada sebuah textbox, tetapi kok muncul error

    berikut kode saya:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.IO;
    using System.IO.Ports;
    using ZedGraph;
    using System.Runtime.InteropServices;
    using System.Globalization;
    using System.Data.SqlClient;
    using System.Management;


    namespace Arduino_1
    {
        public partial class Form1 : Form
        {

            public Form1()
            {
                InitializeComponent();
               // ListComPorts();
                var reader = new ArduinoSerialReader("COM1");
                Console.ReadLine();
                
            }


            static void ListComPorts()
            {
                // Get a list of serial port names.
                string[] ports = SerialPort.GetPortNames();

              //  Console.WriteLine("The following serial ports were found:");

                // Display each port name to the console.
                foreach (string port in ports)
                {
                    Console.WriteLine(port);

                    
                }

            }

            private void Form1_Load(object sender, EventArgs e)
            {
                tempzg1.GraphPane.XAxis.Title.FontSpec.Family = "Tahoma";
                tempzg1.GraphPane.XAxis.Title.FontSpec.Size = 10;
                tempzg1.GraphPane.YAxis.Title.FontSpec.Family = "Tahoma";
                tempzg1.GraphPane.YAxis.Title.FontSpec.Size = 10;
                tempzg1.GraphPane.Legend.FontSpec.Size = 10;
                tempzg1.GraphPane.XAxis.Type = AxisType.Linear;
                tempzg1.GraphPane.YAxis.Title.Text = "Temperature [°C]";
                tempzg1.GraphPane.XAxis.Title.Text = "Samples";
                tempzg1.GraphPane.XAxis.MinorGrid.IsVisible = true;
                tempzg1.GraphPane.XAxis.MajorGrid.IsVisible = true;
                tempzg1.GraphPane.YAxis.MinorGrid.IsVisible = true;
                tempzg1.GraphPane.YAxis.MajorGrid.IsVisible = true;
                tempzg1.GraphPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);
                tempzg1.GraphPane.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);
                //tempzg1.GraphPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);
                tempzg1.GraphPane.Legend.IsVisible = true;
                tempzg1.GraphPane.Title.Text = "Temperature logger";
            }
        }

        public class ArduinoSerialReader : IDisposable
        {
            private SerialPort _serialPort;

            public ArduinoSerialReader(string portName)
            {
                _serialPort = new SerialPort(portName);
                _serialPort.Open();
                _serialPort.DataReceived += serialPort_DataReceived;
            }

            private void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
            {
                Console.WriteLine(_serialPort.ReadLine());
                txtData.Text = txtData.Text + _serialPort.ReadExisting();
            }

            public void Dispose()
            {
                if (_serialPort != null)
                {
                    _serialPort.Dispose();
                };
            }
        }
    }

    Demikian dan terima kasih

    22 Februari 2017 2:24

Semua Balasan