2

I've been working on a simple XBee/Arduino/Python transfer system. Here's how it works: A hex command is sent from python over xbee, which is received by the arduino. This triggers it to take a picture using an Adafruit TTL Serial Camera. The image is then held in memory and sent over the xbee to the computer, 32 bytes at a time. The Python script then adds the bytes to a .jpg, to be viewed when it finishes.

As of now, both run fine, albeit a tad slow (~25 sec round-trip) for my needs. The problem is, switching from 57600 baud to 115200 in the xbee firmware and programs causes them to fail. They will sometimes give a .jpg roughly half the normal size, or not transmit at all, in both cases being unreadable from a viewer. I've tried changing the timeout in the python side, and modifying the xbee's interfacing options, all to no avail.

Here is the Arduino sketch (adapted largely from the Snapshot example sketch in the Adafruit VC0706 examples library):

#include <Adafruit_VC0706.h>
#include <SoftwareSerial.h>

#define chipSelect 10

SoftwareSerial cameraConnection = SoftwareSerial(2,3);

Adafruit_VC0706 cam = Adafruit_VC0706(&cameraConnection);

void setup()
{
  Serial.begin(57600);
  pinMode(8, OUTPUT);

  if (cam.begin()){}
  else { return; } //Abort the transfer if camera does not initialize

  cam.setImageSize(VC0706_640x480);
}

void loop()
{
  if (Serial.read() == 0x01) //Wait for send command
  {
    snapAndSend();
    cam.reset();
  }
}
void snapAndSend()
{
  cam.takePicture();
  uint16_t jpgLen = cam.frameLength();

  while (jpgLen > 0)
  {                     //Send off 32 bytes of data at a time
    uint8_t *buffer;
    uint8_t bytesToRead = min(32, jpgLen);
    buffer = cam.readPicture(bytesToRead);
    Serial.write(buffer, bytesToRead);
    jpgLen -= bytesToRead;
  }
}

And the Python script:

import serial
import time


link = serial.Serial('COM12', 57600)
print("Engage!")

while True:
    answer = input("Press enter to take a picture (type ' to exit): ")
    if answer == "'":
        break
    file = open('imageTest.jpg', 'wb')
    link.write(b"\x01")
    time1 = time.time()
    while True:
        if link.inWaiting() > 0:
            file.write(link.read())
            time1 = time.time()
        else:
            time2 = time.time()
            if (time2 - time1) > .5:
                break


    print ("Complete! Closing file now...")
    file.close()

I'm still a bit new to serial communication and xbees, so I may be overlooking something here. Anyone more experienced have any thoughts on why a switch in baudrate breaks it?

Horizon
  • 21
  • 1

1 Answers1

0

I'm fairly certain that this is going to be related to flow control.

The XBee module can't keep up with a steady stream of data at 115200, so you need to configure it to use hardware flow control. In your case, you need to watch the CTS (clear to send) line from the XBee module, and have your program hold off on sending more data whenever it's not asserted (held low).

You can also configure the XBee to use one of its inputs for an RTS (request to send) signal -- the line the host uses to tell the XBee module when it's ready to receive data.

You may need to modify your Arduino code to receive the entire image from the camera into a buffer, and then slowly send it back out to the XBee module. If you were to check the CTS line before sending each byte, you could hold off whenever the XBee module's buffers were full. You'd need to call a function in your main loop to check the status of your buffered data, and the status of the pin to see if it was possible to send another byte.

tomlogic
  • 11,489
  • 3
  • 33
  • 59
  • Sounds like a reasonable cause. I didn't realize the XBees couldn't keep up at that speed. I'll start on coding in a flow control system. – Horizon Jun 11 '14 at 16:23
  • I have noticed an odd issue, though. When I hardwire the Arduino to my laptop and set it to 115200 baud, the transmission rate is roughly the same as 57.6k (~23-26 sec). I'm thinking the slowness has something to do with the buffer I'm using. – Horizon Jun 11 '14 at 16:32