Sie sind auf Seite 1von 3

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;

namespace SerialTest
{
public partial class Form1 : Form
{
public int RB0=0;
int RB1;
int RB2;
int RB3;
int RB4;
int RB5;
int RB6;
int RB7;

public int TimerValue;

public SerialPort port = new SerialPort("COM10", 9600, Parity.None, 8,


StopBits.One);

int time = 0;

public Form1()
{
InitializeComponent();
}

public void Form1_Load(object sender, EventArgs e)


{
port.Close();

// Console.WriteLine("Incoming Data:");

// Attach a method to be called when there


// is data waiting in the port's buffer
port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);

// Begin communications
port.Open();
}

public void LOOP()


{
//Console.WriteLine("Incoming Data:");

// Attach a method to be called when there


// is data waiting in the port's buffer
port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);

// Begin communications
port.Open();
// port.DiscardOutBuffer();
}

private void port_DataReceived(object sender,


SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
// Console.WriteLine(port.ReadExisting());
string data;
char last;
data = port.ReadExisting();

last = data.Last();
AppendTextBox(data);
if (last.ToString()=="u")
{

AppendTextBox(Environment.NewLine);
}
//port.DiscardOutBuffer();
}

public void button1_Click(object sender, EventArgs e)


{

port.Write(textBox2.Text+"*");

// port.Write("*");

// port.DiscardInBuffer();

}
public void AppendTextBox(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(AppendTextBox), new object[] { value
});
return;
}
textBox1.Text += value;

public void timer1_Tick(object sender, EventArgs e)


{

// string test = RB0.ToString();


// textBox1.Text = textBox1.Text+ test;
if (RB0 == 1)
{
port.Write("rbnj*");
}

if (RB0 == 0)
{
port.Write("rbnn*");
}

public void button2_Click(object sender, EventArgs e)


{

RB0 = 1;

__delay_ms(10);

RB0 = 0;

// __delay_ms(1000);

public void __delay_ms(int msc)


{
// int a;
Delay.Interval =100;
TimerValue = 0;

Delay.Enabled = true;

LOOP:
if (msc!=TimerValue)
{
goto LOOP;
}
Delay.Enabled = false;

public void Delay_Tick(object sender, EventArgs e)


{
TimerValue++;
}
}
}

Das könnte Ihnen auch gefallen