2015-04-16 99 views
1

我正在爲學校開展一個簡單的項目。我的麪包板上有4位數12針顯示,所有連接都正確,我可以在程序中設置一個默認值,它顯示得很好。 (或得到一個隨機數,增量這是所有的工作,所以我不需要任何意見在這個。AS3長時間發送給arduino?

但是我需要從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使這些2通信。

我現在又裝起來(我有這個問題了一段時間),現在我看到的Arduino默認值(N)不斷復位,我從閃存發送數據後。數據總是1。

我很想知道爲什麼和我在做什麼錯。

+0

可以使用例如寫長篇['BigInteger'] (https://code.google.com/p/as3crypto/source/browse/trunk/as3crypto/src/com/hurlant/crypto/rsa/BigInteger.as?r=3)從as3crypto,或['LongInt'] (https://code.google.com/p/lodgamebox/source/browse/trunk/com/lordofduct/util/LongInt.as)from lodgamebox – fsbmain

+0

嗯,有趣的話題!我搜索了三個舊項目,我看到的只有這個:'_socket.writeUTFBytes(data); _socket.flush();',其中數據是** String **。說實話 - 我不記得爲什麼,但它總是一個字符串表示,總是UTF。或者你不想投?我可以分享的另一件事是我們用來讀取數據的方式 - 使用分隔符,因爲有時候只有部分包可以到達(但這是另一個話題)。 –

+0

我明白了,我知道如何發送字符串等,但是有沒有什麼機會讓你有一個很好的例子,如何在Arduino上閱讀它? –

回答

2

是否串行速度匹配的serproxy和Arduino的草圖?

parseInt()不處理二進制數據的工作,預計數字爲ASCII字符:「1」,而不是1

嘗試採用Socket類的字符串方法之一來發送數據,例如:

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

我會在家裏試試!我猜是有道理的。 –

+0

那麼如果他發送0作爲數字呢? :P –

+0

好問題。根據Stream :: parseInt的源代碼:'return 0;如果超時,則返回零。我不知道如何區分超時值和零值。這是一個幸福的地方 - 不要碰!!! :)。 –