Şimdi Ara

c# arduino haberleşmesi yardım arıyorum

Daha Fazla
Bu Konudaki Kullanıcılar: Daha Az
2 Misafir (1 Mobil) - 1 Masaüstü1 Mobil
5 sn
2
Cevap
0
Favori
824
Tıklama
Daha Fazla
İstatistik
  • Konu İstatistikleri Yükleniyor
0 oy
Öne Çıkar
Sayfa: 1
Giriş
Mesaj
  • Merhaba

    arduino C# haberleşmesi üzerine çalışıyorum.
    Hem IP üzerinden hemde Serial Port üzerinden haberleşmesini istiyorum.

    Serial üzerinden haberleşmeyi yaptım. IP üzerinden ise veriyi gönderiyorum ve arduinodan alıyorum.
    Ancak arduinodan C# ye veriyi IP üzerinden göndermeyi bir türlü başaramadım.
    UDP protokü kullanıyorum.
    Projeyi tamamlamama yadım ederseniz çok sevinirim.



    C# Form dosyasının kodları va arduinonun kodları aşağıdadır,

     

    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.Net;
    using System.Net.Sockets;
    using System.IO.Ports;

    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    public static System.IO.Ports.SerialPort serialPort1;
    delegate void SetTextCallback(string text);

    String mode = "ip";
    String remember = "";


    public Form1()
    {

    InitializeComponent();

    getComPorts();
    setRadioButtons();

    }

    //SENDING DATA ===============

    private void sendButton_Click(object sender, EventArgs e)
    {
    sendData(dataBox1.Text);
    }

    public void sendData(String args)
    {
    if (mode == "ip")
    {
    Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    IPAddress send_to_address = IPAddress.Parse(IPBox.Text);
    IPEndPoint sending_end_point = new IPEndPoint(send_to_address, 8888);
    byte[] send_buffer = Encoding.ASCII.GetBytes(args);
    sending_socket.SendTo(send_buffer, sending_end_point);
    }


    if (mode == "com")
    {
    serialPort1.Write(args);
    }

    }


    //RADIO BUTTONS ========================

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
    mode = "com";
    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
    mode = "ip";
    }

    public void setRadioButtons()
    {
    if (mode == "com")
    {
    radioButton1.Checked = true;
    radioButton2.Checked = false;
    }
    else
    {
    radioButton1.Checked = false;
    radioButton2.Checked = true;
    }
    }


    //COMBO BOX ========================
    private void getComPorts()
    {
    comPorts.Items.Clear();
    foreach (string item in System.IO.Ports.SerialPort.GetPortNames())
    {
    comPorts.Items.Add(item);
    }

    }

    private void comPorts_SelectedIndexChanged(object sender, EventArgs e)
    {
    int selectedIndex = comPorts.SelectedIndex;
    Object selectedItem = comPorts.SelectedItem;
    //Properties.Settings.Default.comPort = comPort;
    //Properties.Settings.Default.Save();
    }

    //RECEIVING DATA==========================

    public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
    SerialPort sp = (SerialPort)sender;
    string indata = sp.ReadExisting();
    remember += indata;
    if (remember.Contains('&'))
    {
    SetText(remember);
    remember = "";
    }

    }


    private void SetText(string text)
    {
    // 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.textBox1.InvokeRequired)
    {
    SetTextCallback d = new SetTextCallback(SetText);
    this.Invoke(d, new object[] { text });
    }
    else
    {
    this.textBox1.Text = text;
    }
    }



    private void startButton_Click(object sender, EventArgs e)
    {
    System.ComponentModel.IContainer components = new System.ComponentModel.Container();
    serialPort1 = new System.IO.Ports.SerialPort(components); // Creating the new object.
    serialPort1.PortName = comPorts.Text; // Setting what port number.
    serialPort1.BaudRate = 9600;
    try
    {
    serialPort1.DtrEnable = true;
    //serialPort1.WriteTimeout = 500;
    serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
    serialPort1.Open(); // Open the port for use.

    }
    catch (Exception)
    {
    //Alert Text
    }
    }

    private void dataBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }
    }
    }





    Arduinonun kodları ise aşağıda.

     

    #include <SPI.h> // needed for Arduino versions later than 0018
    #include <Ethernet.h>
    #include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>

    LiquidCrystal_I2C lcd(0x27,16,2);

    String inputString="";
    String data[]={
    "","","","","","",""};
    String mode="ip";

    // Enter a MAC address and IP address for your controller below.
    // The IP address will be dependent on your local network:
    byte mac[] = {
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    IPAddress ip(192, 168, 2, 165);

    unsigned int localPort = 8888; // local port to listen on

    // buffers for receiving and sending data
    char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
    char ReplyBuffer[] = "alhaaaaaaaaaaaaaaaaa";

    // An EthernetUDP instance to let us send and receive packets over UDP
    EthernetUDP Udp;

    void setup() {
    inputString.reserve(1500);
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.print("Hello!");
    // start the Ethernet and UDP:
    Ethernet.begin(mac,ip);
    Udp.begin(localPort);

    Serial.begin(9600);
    }


    void loop() {

    // if there's data available, read a packet
    int packetSize = Udp.parsePacket();
    if(packetSize)
    {
    IPAddress remote = Udp.remoteIP();
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    //Serial.println(packetBuffer);
    String datam=packetBuffer;

    int i=0;
    while (i<packetSize) {
    char inChar = datam[i];
    if (inChar == '&') {
    split();
    }
    else{
    inputString += inChar;
    }
    i++;
    }
    }
    }

    void serialEvent() {
    while (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar == '&') {
    split();
    }
    else{
    inputString += inChar;
    }
    }
    }

    void split(){
    int myLength=inputString.length();
    int anchor=0;
    int counter=0;
    for(int i=0;i<myLength;i++){
    if(inputString[i]==','){
    data[counter]=inputString.substring(anchor,i);
    anchor=i+1;
    counter++;
    }
    }

    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(data[0]);
    lcd.setCursor(0,1);
    lcd.print(data[1]);

    if(mode=="ip"){
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.print(inputString);
    Udp.endPacket();
    }

    if(mode=="com"){
    /*
    Serial.print(data[0]);
    Serial.println();
    Serial.print(data[1]);
    Serial.println();
    Serial.print(data[2]);
    Serial.println();
    Serial.print(data[3]);
    */
    delay(500);
    inputString +="&";
    Serial.print(inputString);
    Serial.println();
    }


    inputString = "";
    data[0]="";
    data[1]="";
    data[2]="";
    data[3]="";
    data[4]="";
    }




    Form dosyasının fotoğrafınıda burada paylaşıyorum.

    http://www.hanerim.com/test/test.jpg







  • sendto dan önce bind yapman gerekiyor sanırsam

    sending_socket.Client.Bind(sending_end_point);
  • 
Sayfa: 1
- x
Bildirim
mesajınız kopyalandı (ctrl+v) yapıştırmak istediğiniz yere yapıştırabilirsiniz.