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);
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.
// 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++; } } }