using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO.Ports;using System.Threading;
namespace WindowsApplication14{ public partial class Form1 : Form { SerialPort _serialPort;
private delegate void SetTextDeleg(string text);
public Form1() { InitializeComponent(); }
private void btnStart_Click(object sender, EventArgs e) { try { if (!_serialPort.IsOpen) _serialPort.Open();
_serialPort.Write("SI\r\n"); } catch (Exception ex) { MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!"); } }
private void Form1_Load(object sender, EventArgs e) { // all of the options for a serial device // can be sent through the constructor of the SerialPort class // PortName = "COM1", Baud Rate = 19200, Parity = None, // Data Bits = 8, Stop Bits = One, Handshake = None _serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); //_serialPort.Handshake = Handshake.None; _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); _serialPort.ReadTimeout = 2000; _serialPort.WriteTimeout = 500; _serialPort.Open(); }
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) { Thread.Sleep(500); string data = _serialPort.ReadLine(); this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data }); }
private void si_DataReceived(string data) { textBox1.Text = data.Trim(); } }}
please help me if possible