Problem z komunikacją.

To forum jest dla wszystkich pasjonatów mikrokontrolerów AVR Atmela. Wymiana doświadczeń i pomoc dla początkujących w pisaniu programów zarówno w C, Asemblerze jak i BASCOM. Zapraszam znawców tematu, aby pomogli wszystkim początkującym!
ODPOWIEDZ
adi19887
-
Posty: 52
Rejestracja: 22 mar 2010, 23:48
Lokalizacja: Poznań

Problem z komunikacją.

Post autor: adi19887 » 13 gru 2013, 12:36

Witam serdecznie,

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');*/
		                	}
		                }
}
Chciałbym w ten sposób wysyłać info (czyli bajtami lub uchar jak kto woli). Na kompie mam postawiony taki program:

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];
            */
        }

        
    }
}
Wklejam cały kod bo nie wiem co będzie potrzebne. W tym miejscu ogromna prośba ponieważ męczę tą sprawę już tydzień i co najwyżej udało mi się za pomocą echa na uC przesłać pojedynczy bajt, o pomoc ponieważ już mi włosów na głowie brakuje do wyrywania. Może ktoś z użytkowników zaproponuje gotowe rozwiązanie (przeszukałem już chyba wszystko w internecie). Bardzo proszę o pomoc.

Awatar użytkownika
bsw
Użytkownik
Posty: 100
Rejestracja: 14 kwie 2008, 13:13
Lokalizacja: Małopolska
Kontakt:

Post autor: bsw » 13 gru 2013, 15:00

Na początek uruchom na kompie jakiś gotowy terminal rs232 (pod 7 nic gotowego niema niestety ale można coś doinstalować - np RealTerm) i pod nim sprawdzaj czy coś przesyłasz. Dopiero jak się upewnisz że uC dobrze nadaje napisz sobie własny "odbiornik".

adi19887
-
Posty: 52
Rejestracja: 22 mar 2010, 23:48
Lokalizacja: Poznań

Post autor: adi19887 » 13 gru 2013, 15:07

Ok znalazłem problem. Wystarczyło odchaczyć:

Kod: Zaznacz cały

serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
i zmienić nazwę metody, która miała by się zająć eventem.

Pozdrawiam, temat do zamknięcia.

Dziękuję za odpowiedź. Tak jak napisałem wyżej to było problemem.

ODPOWIEDZ