The problem of Arduino Leonardo connect to Flash AS3(new)

I have a problem that using Arduino Leonardo cannot send data to Flash AS3. First, I connect a phone to the leonardo via bluetooth device and Leonardo is connected to PC using the USB cable. It is successful to communicate each other. When I type “hello” in the phone, the serial mon of Leonardo show “Hello”. It means the phone connect to the Leonardo successfully.

phone (bluetooth) -----> (bluetooth) Leonardo (USB) -------> (USB) PC

Then I use it to connect the Flash AS3 project via serproxy. After opening the serproxy, the serial port of Leonardo could be used and the serial mon can’t open. In the terminal, it will tell you the connection state and it is success.

The problem is that although the above connection is successful, the Flash project can’t receive any data from my phone. When I type “Hello” and send it in my phone, the output of the Flash project do not show any character.

In the case of using Uno board to place Leonardo board and use the same code, Flash can receive data from my phone and the output of the Flash project will show “Hello” if I send “Hello” from my phone.

I think the problem is about the mega32u4 in the Leonardo.

The code of Arduino Leonardo

#include <SoftwareSerial.h>


SoftwareSerial mySerial(10, 11); // RX, TX
byte c;
byte d;

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("Start!!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
  
  while (!Serial) { 
  ; // wait for serial port to connect. Needed for Leonardo only 
  }
}

void loop() // run over and over
{
  if (mySerial.available()){
    c = mySerial.read();
    Serial.write(c);
  }if (Serial.available()){
    d = Serial.read();
    mySerial.write(d);
    mySerial.println("");
  }
}

I use Serial1 to try again and it also appear this problem.

byte c;
byte d;

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(38400);
  Serial.println("Start!!");

  // set the data rate for the SoftwareSerial port
  Serial1.begin(9600);
  Serial1.println("Hello, world?");
  
  while (!Serial) { 
  ; // wait for serial port to connect. Needed for Leonardo only 
  }
}

void loop() // run over and over
{
  if (Serial1.available()){
    c = Serial1.read();
    Serial.print(c);
    Serial.print((byte)0);
  }if (Serial.available()){
    d = Serial.read();
    Serial1.write(d);
    Serial1.println("");
  }
}

There are 4 main components needed to getting an Arduino to communicate with Flash:

  1. Code to run on the Arduino: This sends data over the USB virtual serial port.

  2. A Serial to Socket Server: This takes data From the serial port and sends it over a network socket. Design ed to be used with any client software (Flash etc) and any microcontroller.

  3. A Flash Socket Library: Actionscript code in flash that accepts and interprets the data coming in from the network socket. You can do this yourself.

4 Your Flash Code (and Sample code): You custom actionscript code that does something interesting with the data. Some resources have sample actionscript code so you can learn by example.

refer to http://playground.arduino.cc/Interfacing/Flash, I get the serproxy and use it in the above.

The code in .cfg which the serproxy will use.

newlines_to_nils=false

comm_baud=9600

comm_databits=8

comm_stopbits=1

comm_parity=none

timeout=300

comm_ports=4 
#when I use Uno, comm_ports=3
net_port4=5331
#when I use Uno, net_port3=5331

The code in Flash.

import flash.events.Event;
import flash.display.Sprite;
import flash.net.Socket;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.utils.Endian;

const TOGGLE_LED_STATE:String= "t";
const EOL_DELIMITER:String = "\n";
var _socket:Socket;
var _proxyAddress:String = "127.0.0.1";
var _proxyPort:int = 5331;

function onAddedToStage():void
{
 removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
 stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
 _socket = new Socket();
 _socket.addEventListener( Event.CONNECT, onConnect ); 
 _socket.addEventListener( Event.CLOSE, onClose ); 
 _socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );
 _socket.addEventListener( IOErrorEvent.IO_ERROR, onIOError );
 _socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSecurityError );
 _socket.endian = Endian.LITTLE_ENDIAN;
 _socket.connect(_proxyAddress, _proxyPort);
}

function onConnect(event:Event):void
{
 trace("Socket Connected");
}

function onSocketData(event:ProgressEvent):void{
 var data:String = _socket.readUTFBytes( _socket.bytesAvailable );
 trace(data);
}

function onKeyPressed(evt:KeyboardEvent):void
{
 trace("onKeyPressed");
 if(!_socket.connected)
 {
 trace("You must be connected to send a command to the Arduino.");
 return;
 }
 switch (evt.charCode) {
 case Keyboard.A:
 trace(bg_mc.currentFrame);
 trace(bg_mc.totalFrames);
 break;
 case Keyboard.B:
 bg_mc.nextFrame();
 break;
 default:
 trace("keyCode:", evt.keyCode, "charCode:", evt.charCode);
 }
 _socket.flush();
}

function onClose(event:Event):void
{
 trace("Socket Closed");
}

function onIOError(event:IOErrorEvent):void
{
 timer.stop();
 timer.removeEventListener(TimerEvent.TIMER,counting);
 question_mc.visible = false;
 trace("IOErrorEvent : " + event.text);
}

function onSecurityError(event:SecurityErrorEvent):void
{
 trace("SecurityErrorEvent : " + event.text);
}

onAddedToStage();

Theoretically, Flash should take the serial port of Leonardo and get the data from the Serial. But no any data I inputted can pass to Flash as3.

The Leonardo has 2 hardware serial ports. Why are you using SoftwareSerial?

I have tried Serial1 and SoftwareSerial, I think the problem is not here.

You have still failed to explain the role of Flash and serproxy. I presume they exist on the PC.

If the PC can receive data from the Leonardo in the Serial Monitor then the entire chain phone --->Leonardo --->PC is working properly and the problem you have is internal to your PC and is NOT an Arduino problem.

And, as @PaulS has said, there should be no need for SoftwareSerial on a Leonardo in your project. Indeed I thought you had said in your other Thread that you were using Serial1.

...R

Robin2: You have still failed to explain the role of Flash and serproxy. I presume they exist on the PC...

I copied the role of Flash and serproxy from the other website and added the code of using the Serial1. My project also take reference in http://playground.arduino.cc/Interfacing/Flash, all of the above information can find in this page.