Bonjour,

j'essaye de réaliser un programme C++ qui me permet d'envoyer des commandes (chaînes de caractères) et de lire les réponses d'un Arduino Mega2560. Autant avec la console Arduino, le système est très réactif : j'envoie la commande et l'Arduino répond dans la foulée (environ 1 à 2ms). Autant avec mon programme C++, le système devient très lent : j'ai pratiquement 1s entre l'envoi des données et la réponse !
Je pense que l'un des soucis est dans le contrôle des flux (DSR, DTR, RTS, CTS), je ne dois pas les gérer correctement.

Savez-vous comment je peux accélérer la communication entre mon PC et mon Arduino ? Quels sont les flags que je dois activer ou désactiver ?

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
 
// ArduinoCom.cpp : Ce fichier contient la fonction 'main'. L'exécution du programme commence et se termine à cet endroit.
//
 
#include <iostream>
#include <string>
#include <windows.h>
#include <chrono>
 
using namespace std;
using namespace chrono;
 
class ArduinoCom
{
	public:
		ArduinoCom() {};
		~ArduinoCom() {};
 
		int initArduino(string comPort)
		{
			bool fSuccess = true;
			wstring ws;
			ws = wstring(comPort.begin(), comPort.end());
 
			hCom = CreateFile(ws.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
 
			if (hCom == INVALID_HANDLE_VALUE)
			{
				return -1;
			}
 
			comParams.DCBlength = sizeof(comParams);
			GetCommState(hCom, &comParams);
			comParams.BaudRate = 1200;
			comParams.ByteSize = 8;
			comParams.Parity = NOPARITY;
			comParams.StopBits = ONESTOPBIT;
 
			/*
			comParams.fOutxCtsFlow = false;
			comParams.fRtsControl = RTS_CONTROL_DISABLE;
 
			comParams.fOutxDsrFlow = false;
			comParams.fDtrControl = DTR_CONTROL_DISABLE;
 
			comParams.fBinary = true;
			comParams.fDsrSensitivity = false;
			comParams.fTXContinueOnXoff = true;
			comParams.fOutX = true;
			comParams.fInX = true;
			*/
			fSuccess = SetCommState(hCom, &comParams);
 
			CloseHandle(hCom);
			return 0;
		}
 
 
		int openArduino(string comPort, DWORD baudRate)
		{
			bool fSuccess = true;
			wstring ws;
			ws = wstring(comPort.begin(), comPort.end());
 
			hCom = CreateFile(ws.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
 
			if (hCom == INVALID_HANDLE_VALUE)
			{
				return -1;
			}
 
			comParams.DCBlength = sizeof(comParams);
			GetCommState(hCom, &comParams);
			comParams.BaudRate = baudRate;
			comParams.ByteSize = 8;
			comParams.Parity = NOPARITY;
			comParams.StopBits = ONESTOPBIT;
 
			comParams.fOutxCtsFlow = true;
			comParams.fRtsControl = RTS_CONTROL_ENABLE;
 
			comParams.fOutxDsrFlow = true;
			comParams.fDtrControl = DTR_CONTROL_ENABLE;
 
			comParams.fBinary = true;
			comParams.fDsrSensitivity = true;
			comParams.fTXContinueOnXoff = true;
			comParams.fOutX = true;
			comParams.fInX = true;
 
 
			fSuccess = SetCommState(hCom, &comParams);
 
			Sleep(1500);
 
			if (!fSuccess)
				return -1;
			return 0;
		}
 
		int writeArduino(string cmd)
		{
			bool fSuccess;
			const char *buf;
			DWORD nbw;
 
			buf = cmd.c_str();
			fSuccess = WriteFile(hCom, buf, strlen(buf), &nbw, NULL);
			//FlushFileBuffers(hCom);
 
			return fSuccess;
		}
 
		int readArduino(string &resp, char endChar)
		{
			char buf;
			bool fSuccess;
			DWORD nbr;
 
			while (true)
			{
				fSuccess = ReadFile(hCom, &buf, 1, &nbr, NULL);
				cout << buf;
				if (buf == endChar)
					break;
			}
			//resp = buf;
			return fSuccess;
		}
 
		int closeArduino()
		{
			if (hCom == INVALID_HANDLE_VALUE)
				return -1;
 
			CloseHandle(hCom);
			return 0;
		}
 
	private:
		HANDLE hCom;
		DCB comParams = { 0 };
};
 
 
 
int main()
{
	ArduinoCom *ac = new ArduinoCom();
	string msg = "-";
 
	cout << "init " << ac->initArduino("COM7") << "\n";
	Sleep(1000);
 
	cout << "open " << ac->openArduino("COM7", 115200) << "\n";
 
	auto start = high_resolution_clock::now();
 
	cout << "Top écriture ";
	cout << "write " << ac->writeArduino("BrLed,Pulse,31,2500\\n") << "\n";
 
	cout << "Top lecture ";
	ac->readArduino(msg, '\n');
	cout << msg << "\n";
 
	auto stop = high_resolution_clock::now();
 
	auto duration = duration_cast<milliseconds>(stop - start);
	cout << duration.count() << endl;
 
 
	cout << "close " << ac->closeArduino();
	free(ac);
 
	system("pause");
    //std::cout << "Hello World!\n";
}