- Home /
Access to serial port on Android device
Hello there !
My problem is the following : I've got an android application who is installed on a Xperia Tablet linked to an Arduino with USB. I need to open a serial port to send data to Arduino board but nothing happen. So my questions are :
Does Serial Ports are enabled on Android ?
Does anyone knows how to enable it in case of they're not enabled ?
I used System.IO.ports, it's worked perfectly on PC but not on Android device. So I've got an error in console when i run my application :
Dll not found Exception : MonoPosixHelper
Does anyone knows what is it talking about ?
Thanks for reading !
Hello @Zeterro! I have the same problem. I get Dll not found Exception : $$anonymous$$onoPosixHelper. Can you solve this?
Answer by Landern · Aug 19, 2014 at 02:45 PM
Hmm, it shows it's supported in the Normal Net 2.0, if your android is set to Subset 2.0 then it will fail.
Check out the namespace compatibility here (search for "Namespace System.IO.Ports" without the quotes on the page.)
Also check your Player Settings for android <- you will need to scroll down for android.
Hmm, thanks but Api compatibility Level is set to Normal Net 2.0 in my Player Settings and the namespace is compatible... :/
Not to beat a dead "animal name here", but you checked with the player settings set to PC and then you checked Android correct, knowing that they can be different?
Yeah, that's the same in Android tab and PC tab : .NET 2.0.
Answer by Zeterro · Aug 19, 2014 at 05:22 PM
Here is a Exemple of my code. Nothing strange i think, but not working on tablet.
using UnityEngine;
using System.Collections;
using System;
using System.Threading;
using System.IO;
using System.IO.Ports;
public class TeensyNetWork : MonoBehaviour
{
static private SerialPort serialPort = new SerialPort("COM6", 9600);
void Start()
{
Debug.Log("Connecting ...");
showPort();
OpenConnection();
}
void showPort()
{
// Get a list of serial port names.
string[] ports = SerialPort.GetPortNames();
Debug.Log("The following serial ports were found:");
// Display each port name to the console.
foreach (string port in ports)
{
Debug.Log(port);
}
}
void OpenConnection()
{
if (serialPort != null)
{
if (!serialPort.IsOpen)
{
serialPort.Open(); // ouverture de la connection
serialPort.ReadTimeout = 1;
Debug.Log("Port ouvert");
}
}
else
{
Debug.Log("Port == null");
}
}
public static void SendIntMessage(int pin, int value)
{
string pinMessage = Convert.ToString(pin);
string valueMessage = Convert.ToString(value);
char[] message = new char[6]; ;
if (value >= 100) // si la valeur est superieure a 100
{
if (pin < 10)
{
message[0] = '0';
message[1] = pinMessage[0];
message[2] = valueMessage[0];
message[3] = valueMessage[1];
message[4] = valueMessage[2];
message[5] = '\n';
}
else
{
message[0] = pinMessage[0];
message[1] = pinMessage[1];
message[2] = valueMessage[0];
message[3] = valueMessage[1];
message[4] = valueMessage[2];
message[5] = '\n';
}
}
else if (value < 100 && value >= 10) // si la valeur est entre 99 & 10
{
if (pin < 10)
{
message[0] = '0';
message[1] = pinMessage[0];
message[2] = '0';
message[3] = valueMessage[0];
message[4] = valueMessage[1];
message[5] = '\n';
}
else
{
message[0] = pinMessage[0];
message[1] = pinMessage[1];
message[2] = '0';
message[3] = valueMessage[0];
message[4] = valueMessage[1];
message[5] = '\n';
}
}
else if (value < 10) // si la valeur est inferieure a 10
{
if (pin < 10)
{
message[0] = '0';
message[1] = pinMessage[0];
message[2] = '0';
message[3] = '0';
message[4] = valueMessage[0];
message[5] = '\n';
}
else
{
message[0] = pinMessage[0];
message[1] = pinMessage[1];
message[2] = '0';
message[3] = '0';
message[4] = valueMessage[0];
message[5] = '\n';
}
}
serialPort.Write(message, 0, 6);
//Debug.Log("send");
}
}
Answer by $$anonymous$$ · Jul 28, 2016 at 01:19 PM
@Zeterro have you sloved this problem? Can you tell me how?
Answer by NGC6543 · May 18, 2017 at 11:29 AM
Are you looking for something like this? https://forum.unity3d.com/threads/serial-ports-in-android.363621/#post-3074558
Your answer
Follow this Question
Related Questions
Arduino through an OTG + Android 2 Answers
Android USB Custom HID 0 Answers
Unity 2018 Android deployment problem,Android problem with Unity 2018.1 0 Answers
Load from Xml on Iphone and Android 2 Answers