Mam problem z wysłaniem przez UART danych z atmegi8 na PC a raczej z ich odbiorem za pomocą c#. Przykładowy kod Atmegi:
Kod: Zaznacz cały
while(1) {
receive = odbierz();
if (receive==255)
{
usart_putc(0xFF);
usart_putc(101);
usart_putc(200);
usart_putc(255);
usart_putc(125);
usart_putc(205);
usart_putc(53);
/*
usart_putc(35);
usart_putc(68);
usart_putc(27);
usart_putc(55);
usart_putc('\n');*/
}
}
}Kod: Zaznacz cały
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_com
{
public partial class Form1 : Form
{
int[] indata= new int[10];
string[] dane = new string[10];
int flaga_synch = 0;
int flaga_startu = 0;
int i = 0;
string txt;
public Form1()
{
InitializeComponent();
//serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
/*
public void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
txt = serialPort1.ReadTo("\n");
this.Invoke(new EventHandler(DisplayText));
}*/
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (flaga_synch == 0)
{
flaga_startu = serialPort1.ReadByte();
if (flaga_startu == 255)
{
flaga_synch = 1;
}
}
if (flaga_synch == 1)
{
indata[0] = serialPort1.ReadByte();
/*indata[1] = serialPort1.ReadByte();
indata[2] = serialPort1.ReadByte();
indata[3] = serialPort1.ReadByte();
indata[4] = serialPort1.ReadByte();
indata[5] = serialPort1.ReadByte();*/
flaga_synch = 0;
}
}
private void DisplayText(object sender, EventArgs e)
{
dane = txt.Split(',');
textBox1.AppendText(dane[0]);
}
/*
public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
//indata += serialPort1.ReadExisting().ToString();
//SerialPort sp = (SerialPort)sender;
string read_str;
read_str = serialPort1.ReadTo("/n");
dane = read_str.Split(',');
/*indata[i] = sp.ReadByte();
i++;
if (i == 10)
i = 0;
}*/
/*
delegate void SetTextCallback(string text);
private void SetText(string text)
{
// łądny opis o wątkach po angielsku, nie chce mi się tłumaczyć
//
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.read.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.read.Text = text;
}
}*/
private void button1_Click(object sender, EventArgs e)
{
if (!(serialPort1.IsOpen))
{
serialPort1.Open();
button1.Enabled = false;
button2.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
if ((serialPort1.IsOpen))
{
serialPort1.DiscardInBuffer();
serialPort1.Close();
button1.Enabled = true;
button2.Enabled = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
/*string temp = send.Text;
double temp2 = Convert.ToDouble(temp);
int temp3 = Convert.ToInt16(temp2);
byte temp5 = Convert.ToByte(temp3);*/
byte[] temp4 = new byte[2];
temp4[0] = 255;
serialPort1.Write(temp4, 0, 1);
}
}
private void button4_Click(object sender, EventArgs e)
{
if (indata[0] == 101)
label1.Enabled = true;
/*
double[] temp6 = new double[10];
string[] temp7 = new string[10];
for (int j = 0; j < 10; j++)
{
temp6[j] = Convert.ToDouble(dane[j]);
temp7[j] = Convert.ToString(temp6[j]);
}
read.Text = temp7[0];
textBox1.Text = temp7[1];
textBox2.Text = temp7[2];
textBox3.Text = temp7[3];
textBox4.Text = temp7[4];
textBox5.Text = temp7[5];
textBox6.Text = temp7[6];
textBox7.Text = temp7[7];
textBox8.Text = temp7[8];
textBox9.Text = temp7[9];
*/
}
}
}