AS3向arduino发送很长时间?

时间:2015-04-16 18:06:46

标签: actionscript-3 sockets flash arduino

我正在为一所简单的学校项目工作。我的面包板上有4位12针显示,所有都正确连接,我可以在我的程序中设置默认值,它显示就好了。 (或者获得一个随机数字,增加这一切都是有效的,所以我不需要任何建议。

然而我需要从flash到Arduino得到一个很长的值(在动作脚本中这是4位数,这可能是一个int但似乎arduino不会读取这个,因为它是8位)。

我似乎无法找到如何将此转移到我的arduino,我尝试了不同的东西,我总是有不同的结果,任何人都可以指出我做错了什么?

来自AS3的代码(据我所知这是相关的)和下面的arduino

Arduino的:

    //used for reading the float
//something I attempted unsuccessfully
union u_tag {
    byte b[4];
    float ival;
} u;

//segments
int a = 6;
int b = 7;
int c = 8;
int d = 9;
int e = 10;
int f = 11;
int g = 12;
int p = 13;
//digits
int d4 = 5;
int d3 = 4;
int d2 = 3;
int d1 = 2;
//other
int n = 401;//display value, this value will be displayed, can be changed
int x = 1;//use this if you want decimals e.g. 10 is 1 decimal
int del = 45;
char commandLetter;  // the delineator / command chooser
char numStr[4];      // the number characters and null
String inString;

void setup()
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(p, OUTPUT);
}

void loop()
{
  //digit 1 Nxxx
  clearLEDs();
  pickDigit(1);
  pickNumber((n/x/1000)%10);
  delayMicroseconds(del);

  //digit 2 xNxx
  clearLEDs();
  pickDigit(2);
  pickNumber((n/x/100)%10);
  delayMicroseconds(del);

  //digit 3 xxNx
  clearLEDs();
  pickDigit(3);
  pickNumber((n/x/10)%10);
  delayMicroseconds(del);

  //digit 4 xxxN
  clearLEDs();
  pickDigit(4);
  pickNumber(n/x%10);
  delayMicroseconds(del);

  //read new data here
  serialEvent();
}

void pickDigit(int x)
{
  digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
  digitalWrite(d4, HIGH);

  switch(x)
  {
    case 1: digitalWrite(d1, LOW); break;
    case 2: digitalWrite(d2, LOW); break;
    case 3: digitalWrite(d3, LOW); break;
    default: digitalWrite(d4, LOW); break;
  }
}

void pickNumber(int x)
{
  switch(x)
  {
    default: zero(); break;
    case 1: one(); break;
    case 2: two(); break;
    case 3: three(); break;
    case 4: four(); break;
    case 5: five(); break;
    case 6: six(); break;
    case 7: seven(); break;
    case 8: eight(); break;
    case 9: nine(); break;
  }
}

void dispDec(int x)
{
  digitalWrite(p, LOW);
}

void clearLEDs()
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(p, LOW);
}

void zero()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void one()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void two()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void three()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void four()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void five()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void six()
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void seven()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void eight()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void nine()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void serialEvent() {
  while(Serial.available() > 0) {
          int c = Serial.parseInt();
          if (c > 0) {
            n = c;
          }
      }
}

Actionscript(在框架中)

import flash.net.Socket;
import flash.events.Event;

    //only allow numbers, period and minus sign
    //numberInput.restrict = ".0-9\\-";

stage.addEventListener(MouseEvent.CLICK, onConnect);

var socket:Socket = new Socket()
    socket.addEventListener(Event.CONNECT, onConnect);
    socket.addEventListener(Event.CLOSE, onClose);
    socket.addEventListener( IOErrorEvent.IO_ERROR, onError );
    socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onError );
    socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );

    //disable until we connect
    this.enabled = false;

    //this is important! If you dont set this to
    //little endian, then Arduino wont understand
    //the bytes
    socket.endian = Endian.LITTLE_ENDIAN;

    socket.connect("127.0.0.1", 5331);

function onConnect( evt:Event):void
{
    //get the number that the user input
    var out:Number = Number("1200");

    //write it as a float to the server.
    //this is important.
    //socket.writeByte(1200);
    socket.writeByte(109);
    socket.flush();
    //if number is too big, then it will overflow on
    //the Arduino, and probably come back as 0.00000
}

function onClose(evt:Event):void {

}

function onError(evt:Event):void {


}

function onSocketData(evt:ProgressEvent):void {
var msg:String = ""; // create a buffer
    while (socket.bytesAvailable) { // while there is byte to read
        var byte:int = socket.readByte();

            msg += String.fromCharCode(byte); // else, we add the byte to our buffer

    }        
    trace("data:" + msg);
}

我使用serproxy来进行这两个沟通。

我现在再次加载它(我有一段时间有这个问题)现在我看到arduino中的默认值(n)在从闪存发送数据后保持重置。现在数据总是1。

我很想知道为什么以及我做错了什么。

1 个答案:

答案 0 :(得分:2)

Does serial speed match in serproxy and Arduino sketch?

parseInt() does not work with binary data, it expects digits as ascii characters: "1", not 1.

Try to send data by one of string methods of Socket class, e.g:

socket.writeMultiByte("125" , "us-ascii") ;
socket.flush();

and also try to input data manually into serial monitor of the following sketch to see how parseInt() works:

int n = 401;//display value, this value will be displayed, can be changed
boolean new_val = true ;

void setup() {

    Serial.begin(9600);           // set up Serial library at 9600 bps
    // This check is only needed on the Leonardo:
    while (!Serial) {
        ; // Wait for serial port to connect. Needed for Leonardo only
    }
    delay(100) ;
    Serial.println( "Started") ;
}

void loop() {
    serialEvent();

    if (new_val) {
       Serial.print( "n: " ); Serial.println( n , DEC ) ;       
       new_val = false ;
    }

    delay(200) ;
}

void serialEvent() {
    while(Serial.available() > 0) {
        int c = Serial.parseInt();
        if (c != 0) {
           n = c;
           new_val = true ;
        }
    }
}