Pages: [1]   Go Down
Author Topic: AS3 to Arduino - Passing Strings  (Read 516 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
    }
  }
 
}

Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

change this
Code:
while(Serial.available() == 0);
 
  //readCommand();
  //Serial.println(command);
 
 
  readCommand();
to
Code:
if(Serial.available() > 0)
{
readCommand();
}

this
Code:
while (!Serial.available())
;
c = Serial.read();
to
Code:
while(Serial.available() > 0)
{
c = Serial.read();
if (c == '\r' || c == '\n') {
break;
}
commandBuffer[i] = c;
i++;
}
Logged

From Idea To Invention

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

From Idea To Invention

Pages: [1]   Go Up
Jump to: