Go Down

Topic: AS3 to Arduino - Passing Strings (Read 691 times) previous topic - next topic

I have a problem with trying to receive string data from AS3. It is being sent as a socket.writeUTFBytes. I am not sure what I am missing. In the Arduino code below, I am expecting three various strings from the serial, U, D, and motor###. This works great in the serial monitor, but not when sending anything from Flash to the serial socket. Can someone please direct me toward the right direction?

Code: [Select]
#include <stdio.h>
#include <Stepper.h>

const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
const int steps = 200;
Stepper myStepper(steps, dirA, dirB);

int led = 6;

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
 
  myStepper.setSpeed(100);

  // Turn on pulse width modulation
  pinMode(pwmA, OUTPUT);
  digitalWrite(pwmA, HIGH);
  pinMode(pwmB, OUTPUT);
  digitalWrite(pwmB, HIGH);

  // Turn off the brakes
  pinMode(brakeA, OUTPUT);
  digitalWrite(brakeA, LOW);
  pinMode(brakeB, OUTPUT);
  digitalWrite(brakeB, LOW);
 
  Serial.println("OK");
}

char command[1024];
char commandBuffer[128];
int commandBufferSize = 0;

void readCommandBuffer(int bytesToRead) {
int i = 0;
char c = 0;
while (i < 128 && (i < bytesToRead || bytesToRead <= 0)) {
while (!Serial.available())
;
c = Serial.read();
if (c == '\r' || c == '\n') {
break;
}
commandBuffer[i] = c;
i++;
}
commandBufferSize = i;
}

void readCommand() {
command[0] = '\0';
readCommandBuffer(0);
if (strncmp(commandBuffer, "RCV", 3) == 0) {
commandBuffer[commandBufferSize] = '\0';
int expectedSize = atoi(commandBuffer + 4);
if (expectedSize <= 0 || expectedSize > 1024) {
return;
}
Serial.println("RDY");
int bytesRead = 0;
while (bytesRead < expectedSize) {
readCommandBuffer(expectedSize - bytesRead);
memcpy(command + bytesRead, commandBuffer, commandBufferSize);
bytesRead += commandBufferSize;
Serial.print("ACK ");
Serial.println(commandBufferSize);
}
command[bytesRead] = '\0';
} else {
memcpy(command, commandBuffer, commandBufferSize);
command[commandBufferSize] = '\0';
}     
}

void loop() {
  while(Serial.available() == 0);
 
  //readCommand();
  //Serial.println(command);
 
 
  readCommand();
  String str = String(command);
  Serial.println(str);
 
  if(str.indexOf("motor") >= 0){
    int newPosition = str.substring(5,str.length()).toInt();
    digitalWrite(pwmA, HIGH);
    digitalWrite(pwmB, HIGH);
    myStepper.step(newPosition);
    digitalWrite(pwmA, LOW);
    digitalWrite(pwmB, LOW);
  }else{
    digitalWrite(pwmA, LOW);
    digitalWrite(pwmB, LOW); 
    if(str == "U"){
      digitalWrite(led, HIGH);
    } else if(str == "D"){
      digitalWrite(led, LOW);
    }
  }
 
}



Cybernetician

change this
Code: [Select]

while(Serial.available() == 0);
 
  //readCommand();
  //Serial.println(command);
 
 
  readCommand();

to
Code: [Select]

if(Serial.available() > 0)
{
readCommand();
}


this
Code: [Select]

while (!Serial.available())
;
c = Serial.read();

to
Code: [Select]

while(Serial.available() > 0)
{
c = Serial.read();
if (c == '\r' || c == '\n') {
break;
}
commandBuffer[i] = c;
i++;
}
From Idea To Invention

This solved the problem. Do you know of a way to trace or output the data from the AS3 socket that the Arduino is reading? I want to ensure that the string I am sending is being read correctly.

PaulS

You could write to an LCD, or to an SD card. The SD card isn't exactly real time, but it could work, with patience. Or use a Mega, Leonardo, or Micro with multiple serial ports, and an FTDI cable to connect the other serial port to the PC.

Cybernetician

Quote

This solved the problem. Do you know of a way to trace or output the data from the AS3 socket that the Arduino is reading? I want to ensure that the string I am sending is being read correctly.


best Solution PaulS reply
Quote

You could write to an LCD, or to an SD card. The SD card isn't exactly real time, but it could work, with patience. Or use a Mega, Leonardo, or Micro with multiple serial ports, and an FTDI cable to connect the other serial port to the PC.


Quote
I want to ensure that the string I am sending is being read correctly.


For testing purpose you can even do it by blink a led if you know what your sending.
From Idea To Invention

Go Up