gpt4 book ai didi

actionscript-3 - AS3向arduino发送长消息?

转载 作者:行者123 更新时间:2023-12-03 12:00:19 26 4
gpt4 key购买 nike

我正在为学校做一个简单的项目。我的实验板上有4位12针显示器,所有显示器都已正确连接,可以在程序中设置默认值,并且显示效果很好。 (或获得一个随机数字,将其递增就可以了,所以我不需要任何建议。

但是我需要从Flash到Arduino获得一个长值(嗯,在 ActionScript 中是4位数字,这可以是一个整数,但是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草图中的串行速度匹配吗?
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 ;
}
}
}

关于actionscript-3 - AS3向arduino发送长消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29682714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com