AS3 发送长到 arduino?

AS3 Sending long to arduino?

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

然而,我需要从闪存到 Arduino 获取一个 long 值(好吧,它在 actionscript 中是 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。

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

serproxy 和 Arduino sketch 中的串口速度匹配吗?

parseInt() 不适用于二进制数据,它期望数字为 ascii 字符:“1”,而不是 1。

尝试通过 Socket class 的字符串方法之一发送数据,例如:

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

并尝试将数据手动输入到以下草图的串行监视器中,以查看 parseInt() 的工作原理:

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 ;
        }
    }
}