Go Down

Topic: having trouble sending data from arduino to C# (Read 445 times) previous topic - next topic

Apr 01, 2013, 09:31 pm Last Edit: Apr 01, 2013, 09:34 pm by Coding Badly Reason: 1
hi everyone, I am trying to send data from the arduino to C# using a ethernet shield but C# keep giving me a exemption error and I cant figure out what it is, can someone point me on the right direction please.. thank you

arduino code

Code: [Select]
//#include <Dhcp.h>
//#include <Dns.h>
#include <Ethernet.h>
//#include <EthernetClient.h>
//#include <EthernetServer.h>
//#include <EthernetUdp.h>
#include <util.h>
#include <SPI.h>
//#include <Ethernet.h>

byte mac[] = { 0x02, 0xDD, 0xEE, 0x77, 0x33, 0x99  };  //mac (physical address) of arduino
byte ip[] = { 10, 0, 0, 177 };        //ip of Arduino
byte server[] = { 10, 0, 0, 176 }; //PC ip address

EthernetClient client;

void setup()
{
 Ethernet.begin(mac, ip);
 Serial.begin(9600);

 delay(1000);

 Serial.println("connecting...");

 if (client.connect(server, 3005)) {
   Serial.println("connected");
   client.println("Hello World!");
   client.println();
   client.stop(); //remove this in real app
 } else {
   Serial.println("connection failed");
 }
}

void loop()
{
 /*if (client.available()) {
   char c = client.read();
   Serial.print(c);
 }

 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;;)
     ;
 }*/
}




C# code

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace Server
{
   class Program
   {
       static TcpClient connected;
       static void Main(string[] args)
       {
           TcpListener listener = new TcpListener(IPAddress.Parse("10.0.0.177"), 80);

           listener.Start();
           connected = listener.AcceptTcpClient();
           while (true)
           {
               //sendevent:
               send();
               // goto receiveevent;
               // receiveevent:
               receive();
               // goto sendevent;
           }
       }
       static byte[] buffer = new byte[4096];
       static void receive()
       {
           NetworkStream read = connected.GetStream();
           int data = read.Read(buffer, 0, 4096);
           Console.WriteLine("Client: " + Encoding.ASCII.GetString(buffer, 0, data));
       }
       static void send()
       {
           byte[] data = Encoding.ASCII.GetBytes(Console.ReadLine());
           NetworkStream stream = connected.GetStream();
           stream.Write(data, 0, data.Length);
       }
   }



Moderator edit: [code] [/code] tags added.

Go Up