UPD und Char to Char*


hallo ich bin neu hier. ich möchte steuerbefehle über tcp/udp den arduino mega mit ethernet shield und dmx. hab mich mit dem dem updsendrecivestring versucht. soweit läuft es auch nur es kommt ein packet von irgendwo und dann läuft es nicht mehr.

code: [select]


#include <dmxsimple.h>
#include <spi.h>         // needed arduino versions later 0018
#include <ethernet.h>
#include <ethernetudp.h>         // udp library from: bjoern@cs.stanford.edu 12/30/2008


// enter mac address , ip address controller below.
// ip address dependent on local network:
byte mac[] = { 
  0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed };
ipaddress ip(192, 168, 178, 210);

unsigned int localport = 8888;      // local port listen on

// buffers receiving , sending data
char packetbuffer[udp_tx_packet_max_size]; //buffer hold incoming packet,
char  replybuffer[] = "ack";       // string send back

// ethernetudp instance let send , receive packets on udp
ethernetudp udp;

void setup() {
  // start ethernet , udp:
  ethernet.begin(mac,ip);
  udp.begin(localport);
  dmxsimple.usepin(3);
  serial.begin(9600);
}

void loop() {
  // if there's data available, read packet
  int packetsize = udp.parsepacket();
 
    if(packetsize)
  {
    for(int i=0;i<udp_tx_packet_max_size;i++) packetbuffer[i] = 0;


    serial.print("received packet of size ");
    serial.println(packetsize);
    serial.print("from ");
    ipaddress remote = udp.remoteip();
    (int =0; < 4; i++)
    {
      serial.print(remote[i], dec);
      if (i < 3)
      {
        serial.print(".");
      }
    }
    serial.print(", port ");
    serial.println(udp.remoteport());

    memset(packetbuffer, 0, udp_tx_packet_max_size);
    // read packet packetbufffer
    udp.read(packetbuffer,udp_tx_packet_max_size);
    serial.println("contents:");
    serial.println(packetbuffer);

      char *ptr = packetbuffer;
           
      if (strncmp(ptr, "dmx", 3) == 0) {
      // data (format: dmx000v000)
      char buff[5];
      memset(buff, 0, 5);

      // channel
      strncpy (buff, &ptr[3], 3);
      int channel = atoi(buff);

      // value
      strncpy (buff, &ptr[7], 3);
      int value = atoi(buff);
     
      serial.println(channel);
      serial.println(value);
     
     
      //dmxsimple.write(channel,value)
      } 
   
    udp.beginpacket(udp.remoteip(), udp.remoteport());
    udp.write(replybuffer);
    udp.endpacket();
  }
 
  delay(10);
}


hier die ausgabe des serial monitors

received packet of size 10
from 192.168.178.21, port 8888
contents:
dmx000v000
0
0

received packet of size 10
from 192.168.178.21, port 8888
contents:
dmx000v000
0
0

received packet of size -19967
from 118.32.48.32, port 49312
contents:

sobald eine andere ip auftaucht läuft es nicht mehr. warum?
wo kommt die ip her?

ich habe auch versucht es über tcp (chat server) zulösen.

code: [select]

#include <spi.h>
#include <ethernet.h>

// enter mac address , ip address controller below.
// ip address dependent on local network.
// gateway , subnet optional:
byte mac[] = {
  0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed };
ipaddress ip(192,168,178, 210);
ipaddress gateway(192,168,178, 1);
ipaddress subnet(255, 255, 0, 0);


// telnet defaults port 23
ethernetserver server(23);
boolean alreadyconnected = false; // whether or not client connected previously

void setup() {
  // initialize ethernet device
  ethernet.begin(mac, ip, gateway, subnet);
  // start listening clients
  server.begin();
// open serial communications , wait port open:
  serial.begin(9600);
   while (!serial) {
    ; // wait serial port connect. needed leonardo only
  }


  serial.print("chat server address:");
  serial.println(ethernet.localip());
}

void loop() {
  // wait new client:
  ethernetclient client = server.available();

  // when client sends first byte, hello:
  if (client) {
    if (!alreadyconnected) {
      // clead out input buffer:
      client.flush();   
      serial.println("we have new client");
      client.println("hello, client!");
      alreadyconnected = true;
    }

      if (client.available() > 0) {
      // read bytes incoming client:
      char thischar = client.read();
       
      char *ptr = thischar;
     
      if (strncmp(ptr, "dmx", 3) == 0) {
      // data (format: dmx000v000)
      char buff[5];
      memset(buff, 0, 5);

      // channel
      strncpy (buff, &ptr[3], 3);
      int channel = atoi(buff);

      // value
      strncpy (buff, &ptr[7], 3);
      int value = atoi(buff);
     
      serial.println(channel);
      serial.println(value);


      // echo bytes client:
      server.write(thischar);
      // echo bytes server well:
      serial.write(thischar);
    }
  }
}



aber ich bekomme den client.read() nicht hin

  char thischar = client.read();
       
    char *ptr = thischar;   (invalid conversion 'char' 'char*')

  if (strncmp(ptr, "dmx", 3) == 0) {
      // data (format: dmx000v000)
      char buff[5];
      memset(buff, 0, 5);

      // channel
      strncpy (buff, &ptr[3], 3);
      int channel = atoi(buff);

      // value
      strncpy (buff, &ptr[7], 3);
      int value = atoi(buff);
     

vielleicht kann mir mal jemand einen tip geben. a" warum beim udp eine andere ip kommt und sich der arduino aufhängt. und b wie man von char char* convertiert.

so long
squaredrone
     

quote
wie man von char char* convertiert

gar nicht. da hat man falsch verstanden.
char* ist ein zeiger auf ein (oder mehrere)char.


Arduino Forum > International > Deutsch (Moderator: uwefed) > UPD und Char to Char*


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support

RC Receiver into Arduino as PC Controller (UnoJoy)