Salut ici.

alors, partant de ça :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public static void Main()
    {
        SerialPort mySerialPort = new SerialPort("COM1");
 
        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
 
        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceviedHandler);
 
        mySerialPort.Open();
 
        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }
 
    private static void DataReceviedHandler(
                        object sender,
                        SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.WriteLine("Data Received:");
        Console.Write(indata);
    }
je cherche donc à renvoyer indata à une méthode appelante. Dans mon exemple qui suit, une classe fait appel à SendCmd surchargé (ne demandez pas pourquoi, j'en ai besoin ponctuellement) et attend la réponse answer :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        public string SendCmd(string command)
        {
            byte[] commandLine = Encoding.ASCII.GetBytes(command);
            comX.Write(commandLine, 0, commandLine.Length);
            string answer = comX.ReadExisting();
            while (answer == "")
                answer = comX.ReadExisting();
            return answer;
        }
 
        // overloaded method use for identification methods
        public string SendCmd(string command, string port)
        {
            comX_Opening(port);
            return SendCmd(command);
        }
 
        public XElement UpdateTrackerList(XElement trackerlist)
        {
            return connex.GetPortList(trackerlist);
        }
 
        public void comX_Opening(string port)
        {
            if (comX.IsOpen) comX.Close();
            this.connex = new ConnectionManager(port);
            this.comX = connex.serialConnex;
            comX.DataReceived += new SerialDataReceivedEventHandler(comX_DataReceived);
            comX.Open();
        }
 
        public void comX_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //answer=comX.ReadExisting();
        }
Je ne sais pas comment renvoyer answer