Merci ,
J'ai essayé l'idée de la boite noir ^^.
j'ai donc ce nouveau code qui fait planter le logiciel Unity 3D u__u
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO.Ports;
using System.Threading;
public class control : MonoBehaviour {
//communication
SerialPort serial;
public string myString;
float temps = 0.0f;
float delay = 0.0f;
public float comRapidity = 2.0f;
public string portName;
bool setPort = true;
//Servo 1
public int servoDegre1; //Degré value
public Transform servo1;
public Slider sliderServo1;
public Button buttonS1plus;
public Button buttonS1moins;
public Text textS1;
string A;
// Servo 2
public int servoDegre2; //Degré value
public Transform servo2;
public Slider sliderServo2;
public Button buttonS2plus;
public Button buttonS2moins;
public Text textS2;
string B;
// Autre servo test
// Use this for initialization
void Start () {
//communication
serial = new SerialPort();
//Button S1
buttonS1plus.onClick.AddListener(TaskOnClickS1plus);
buttonS1moins.onClick.AddListener(TaskOnClickS1moins);
//Button S2
buttonS2plus.onClick.AddListener(TaskOnClickS2plus);
buttonS2moins.onClick.AddListener(TaskOnClickS2moins);
}
// Update is called once per frame
void Update () {
//Servo 1
if (servoDegre1 != (sliderServo1.value))
{
if (servoDegre1 < (sliderServo1.value) )
{
servoDegre1 = servoDegre1 + 1 ;
}
if (servoDegre1 > (sliderServo1.value) )
{
servoDegre1 = servoDegre1 - 1;
}
}
servo1.localRotation = Quaternion.AngleAxis (-servoDegre1, Vector3.up);
textS1.text = servoDegre1.ToString() + "°";
//Servo 2
if (servoDegre2 != (sliderServo2.value))
{
if (servoDegre2 < (sliderServo2.value))
{
servoDegre2 = servoDegre2 + 1;
}
if (servoDegre2 > (sliderServo2.value))
{
servoDegre2 = servoDegre2 - 1;
}
}
servo2.localRotation = Quaternion.AngleAxis (-servoDegre2, Vector3.up);
textS2.text = servoDegre2.ToString() + "°";
//communication
A = servoDegre1.ToString("000");
B = servoDegre2.ToString("000");
myString=string.Concat(A,B);
temps = Time.time;
if ((delay + comRapidity) < temps)
{
Main (A, B);
delay = temps;
//Debug.Log (myString);
}
}
// button S1
void TaskOnClickS1plus ()
{
(sliderServo1.value) = (sliderServo1.value) + 1;
}
void TaskOnClickS1moins ()
{
(sliderServo1.value) = (sliderServo1.value) - 1;
}
// button S2
void TaskOnClickS2plus ()
{
(sliderServo2.value) = (sliderServo2.value) + 1;
}
void TaskOnClickS2moins ()
{
(sliderServo2.value) = (sliderServo2.value) - 1;
}
//communication
/*
Émission en boucle (1000) de lignes contenant 2 entiers.
Un CRC8 est ajouté en début de ligne pour validation par le récepteur.
Le récepteur renvoie :
* "0" : ok
* "1" : mauvais format de ligne.
* "2" : mauvais CRC
modifier => Arrêt du programme si erreur détectée.
*/
// from pololu.com (https://www.pololu.com/docs/0J44/6.7.6)
static byte GetCRC(string message)
{
byte j, crc = 0;
const byte CRC7_POLY = 0x91;
int i;
int length = message.Length;
for (i = 0; i < length; i++)
{
crc ^= (byte) message[i];
for (j = 0; j < 8; j++)
{
if ((crc & 1) == 1)
crc ^= CRC7_POLY;
crc >>= 1;
}
}
return crc;
}
static public void Main (string A, string B)
{
SerialPort serial;
serial = new SerialPort();
serial.PortName = "COM3" ;
serial.Parity = Parity.None;
serial.BaudRate = 115200;
serial.DataBits = 8;
serial.StopBits = StopBits.One;
serial.Open ();
// Pour laisser le temps à la carte arduino de rebooter
Debug.Log ("Wait arduino reboot (2s) ...");
Thread.Sleep(2000);
// On émet (à fond) 1000 couples (m1 m2)
for(int i=0; i<1000; ++i) {
string msg=A+" "+B;
// Calcul du CRC 8
byte crc=GetCRC(msg);
// falsifie le CRC pour test détection mauvais crc
// crc++;
// falsifie le message pour détection mauvais format
//msg="123 bad";
// CRC en hexa sur 2 octets (simplification du code coté arduino)
string crcHex = crc.ToString("X");
if(crcHex.Length==1) crcHex="0"+crcHex;
// Ajout du CRC en début de message
msg=crcHex+" "+msg;
Debug.Log ("Envoi : \""+msg+"\"");
// envoi
serial.WriteLine(msg);
// attente de la réponse (on retire '\n' final).
string response=serial.ReadLine().TrimEnd();
// Affichage de la réponse
Debug.Log ("Recu ("+i+") : \""+response+"\"");
if(response != "0")
{
Debug.Log ("Erreur !");
}
// pour une attente entre les messages
//Thread.Sleep(500);
}
serial.Close ();
}
void OnApplicationQuit()
{
Debug.Log("Application ending after " + Time.time + "seconds");
serial.Close ();
}
}
J'ai remplacé les console.writeline qui ne sont pas reconnu. J'utilise Debug.Log à la place, cela permet d'afficher le message dans la console d'unity 3D.