Bonjour
Je pense que je m'emele un peu les pinceaux
J'ai une classe form Toute simple qui instancie une classe qui va gerer une communications serie
J'utilise un delegate pour recuperer dans la form le resutat de la com
Mais je dois faire une betise qq part dans la communication entre la form et la classe
Voici ma Form
et la Classe
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
36 using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MCOpticon { public partial class Form1 : Form { C_BlueTooth BL; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { BL.OpenS(); } private void Form1_Load(object sender, EventArgs e) { BL = new C_BlueTooth(); BL.ShowScan += this.ShowText; } void ShowText(string Txt) { this.txbScan.Text = Txt; } } }
Lorsque des données arrivent sur le Serial un event appelle serialPort1_DataReceived
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 using System; using System.Linq; using System.Collections.Generic; using System.Text; using System.Net.Sockets; using System.Threading; using System.IO.Ports; namespace MCOpticon { public class C_BlueTooth { public delegate void ShowScanHandler(string AA); public event ShowScanHandler ShowScan; private event ShowScanHandler TransferScan; SerialPort sp = null; public C_BlueTooth() { sp=new SerialPort("COM5"); // sp.PortName = "COM5"; sp.BaudRate = 9600; sp.Parity = Parity.None; sp.StopBits = StopBits.One; sp.Handshake = Handshake.None; this.TransferScan = new ShowScanHandler(AddDataMethod); } public void AddDataMethod(String myString) { if (ShowScan != null) { ShowScan(myString); } } public void OpenS() { sp.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived); sp.Open(); //opens the port sp.ReadTimeout = 200; } private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { string DispString = sp.ReadExisting(); TransferScan(DispString); } } }
Ensuite j'utilise le delegate TransferScan pour envoyer le resultat a la form via un autre delegate ShowScan
Mais a l'arrivée dans la form la methode ShowText m'envoie un message d'erreur
"Control.Invoke must be used to interact with controls created on a separate thread.
Comment faut il faire : je n'ai pas mis asser de delegates ?
Merci de votre Aide
Partager