Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: having trouble sending data from arduino to C#  (Read 415 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//#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:
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.
« Last Edit: April 01, 2013, 02:34:57 pm by Coding Badly » Logged

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: