Interfacing Arduino with PC (2-way communication)

What I see as confusing is the whole read one character at a time.

Isn't that how everyone reads?

PaulS:

What I see as confusing is the whole read one character at a time.

Isn't that how everyone reads?

I guess it would be... I really need to watch what I say. I meant that I would like it to just read a whole string out of the serial buffer at once so that I can work with it.

I meant that I would like it to just read a whole string out of the serial buffer at once so that I can work with it.

There are a number of new methods in the Serial (Stream) class with 1.0. But, they basically read one character at a time.

The trickiest part of "just read a whole string" at once is defining what constitutes a packet.

With anything else we read, punctuation is important. For instance, we know that there are different packets in the stuff we read, called words, and packets called sentences, and packets called paragraphs, and packets called chapters, and packets called books.

If you read the data, yourself, one character at a time, you can recognize the various end-of-packet markers yourself, and take appropriate action when they arrive.

If you have a specific definition of a packet, run it by us for comment, and help writing code to read that packet, one character at a time, and to make use of that packet contents.

If you have a specific definition of a packet, run it by us for comment, and help writing code to read that packet, one character at a time, and to make use of that packet contents.

To me a serial data packet would be the variable and the value to set it to. What would be the best start/end of packet marker?

Would it be easier for the PC to tell the Arduino it needs data from a sensor then have the Arduino send it, or to just have the Arduino send it every time a command is received and processed?

What would be the best start/end of packet marker?

I don't know about best, but I use < and >, and this code to read the serial data.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where it says "Process the packet", you can deal with the name = value data.

Would it be easier for the PC to tell the Arduino it needs data from a sensor then have the Arduino send it, or to just have the Arduino send it every time a command is received and processed?

It would be easiest, but not necessarily best, to have the Arduino send a response after every command.

It would be easiest, but not necessarily best, to have the Arduino send a response after every command.

I need to keep latency down so that this will be pretty close to realtime, because with 2 3000w motors on a 25lb contaption, it could get moving pretty fast. I also will be running an if statement with a few mathematical equations thrown in there, because I need to vary the range of differential drive turning depending upon the forward speed so I don't throw tracks or flip the thing :astonished:.
Would that method of sending sensor data cause any noticeable slowdown?

PaulS:

What I see as confusing is the whole read one character at a time.

Isn't that how everyone reads?

Actually no. I believe that the prevailing model is still that most people (certainly in the English and European based languages) read a word (or more) at a time, based on the 'shape' of the words.

I have a similar problem to solve. I need an application to be able to do things like check for Arduino connected through ethernet and determine their IP address. I also need to be able to update their IP address from the application.

The next task would be to change some PWM channel values and display some general information to the user about the device (status, etc..).

Serial would be nice, but I may end up going with ethernet only and using UDP (?). One thought is to send UDP packets to update the controller.

I need an application to be able to do things like check for Arduino connected through ethernet and determine their IP address.

If your PC is acting as a server, your server software should have tools to manage that. Apache does, for instance.

I also need to be able to update their IP address from the application.

Good luck with that. The server gets a request, and servers up a response. Then, the connection is closed. Until the client connects again. A server can not initiate communication with a client.

The next task would be to change some PWM channel values and display some general information to the user about the device (status, etc..).

You can supply new values when a client requests them. You can not push new values to a client.

Serial would be nice, but I may end up going with ethernet only and using UDP (?). One thought is to send UDP packets to update the controller.

Everything up to this point assumed that the Arduino-as-client was connecting via ethernet...

hello.

i just want some advice/help :slight_smile:

my project is about sending data from pc-xbee(Tx)-xbee(Rx)-fan. in simple words, i just want to switch on/off the fan.. however, i find it very hard to start but i think http://tutorial.cytron.com.my/2011/08/13/project-14-wireless-temperature-logging/ was a very good tutorial for my project right ??

i have ..

2 Arduino Duemilanove(Compatible)-Main Board
2 Arduino-XBee Shield (without module)
2 XBee 1mW Wire Antenna - Series 2

can i use this tutorial Antipasto Hardware Blog: XBee Shield to XBee Shield Communication to make my xbee communicate to each other first?? if not, can someone help to link other tutorial (similar to my project)?? please teach me... thanx~

First question you need to answer is why you bought mesh-network radios for point-to-point use, instead of point-to-point radios for point-to-point use.

thanx PaulS :slight_smile: ,

errr.. i just put wrong device ...
XBee 1mW Wire Antenna - Series 1 not series 2.. sorry for mistake...

so..in your opinion, can i use the tutorial i mention before ?? or do u have any suggestion :~ please help.. =( thank you..

No, you can't use that tutorial. That is for series 2.5 devices. Using X-CTU, read the modem configuration. Then change PAN ID, MY and DL. Set PAN ID to the same value on both XBees. Set MY on one to some value other than 0. Set DL to a different value, other than 0. On the other XBee, use the same values, but in opposite fields. MY on one should be DL on the other.

That is all that you need to set.

Which XBee shields did you get?

ooo..thanx for helping...

i'm using Arduino-XBee Shield (without module)

4855.jpg

i'm using Arduino-XBee Shield (without module)

That will probably work better WITH a module attached. There are jumpers on that shield that can be moved to allow the programming of the Arduino will the shield still attached, and then moved to connect the XBee to the serial port pins.

did u mean like this module ?? i do have XBee 1mW Wire Antenna - Series 1 attach to the Arduino-XBee Shield .. is it ok ?? i think i do have that jumper on the xbee shield also... :smiley:

PaulS:

There are jumpers on that shield that can be moved ....

4856.jpg

did u mean like this module ??

Yes.

thanx again.. my friend ask, can we use x-ctu version 5.2.7.5 for 64bits window 7??

can we use x-ctu version 5.2.7.5 for 64bits window 7??

I do. Works fine.