Bonjour tout le monde,
J'essaye, sans succès, depuis ce matin de faire communiquer deux de mes ports série: le com11 et le com10 que j'ai relié à l'aide d'un cable db9 croisé.
Malheureusement, j'ai l'impression que les données, qui semblent bien être émises, n'arrivent jamais.
Voici mon code :
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 #include <QCoreApplication> #include <QtSerialPort/QtSerialPort> #include "windows.h" #include <iostream> using namespace std; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //***************************************************************************************************************** //Here I create two serial ports : the first one to emit and the second one to receive QSerialPort *SerialEmitter = new QSerialPort; QSerialPort *SerialReceiver = new QSerialPort; //***************************************************************************************************************** //Here we initialize serial ports SerialEmitter->setPortName("COM11"); SerialEmitter->setBaudRate(QSerialPort::Baud115200); SerialEmitter->setDataBits(QSerialPort::Data8); SerialEmitter->setParity(QSerialPort::NoParity); SerialEmitter->setStopBits(QSerialPort::OneStop); SerialEmitter->setFlowControl(QSerialPort::NoFlowControl); SerialEmitter->open(QSerialPort::OpenModeFlag::ReadWrite); //***************************************************************************************************************** //Here I initialize the message i am going to send SerialReceiver->setPortName("COM10"); SerialReceiver->setBaudRate(QSerialPort::Baud115200); SerialReceiver->setDataBits(QSerialPort::Data8); SerialReceiver->setParity(QSerialPort::NoParity); SerialReceiver->setStopBits(QSerialPort::OneStop); SerialReceiver->setFlowControl(QSerialPort::NoFlowControl); SerialReceiver->open(QSerialPort::OpenModeFlag::ReadWrite); //***************************************************************************************************************** //Here I initialize the message i am going to send char * TheMessage= new char[3]; TheMessage[0]='1'; TheMessage[1]='2'; TheMessage[2]='3'; //***************************************************************************************************************** //Here I also initialize the buffer to get back "The message" char * dataToReceive = new char; cout<< "The value retrun of this function is "<< SerialEmitter->write(TheMessage)<<endl; Sleep(200); cout<< "The value retrun of this function is "<< SerialReceiver->read(dataToReceive,3*sizeof(char))<<endl; //***************************************************************************************************************** cout<<"Here we write what has been received"<<endl; for (int i=0; i<5; i++) { cout<<dataToReceive[i]<<endl; } //***************************************************************************************************************** SerialEmitter->close(); SerialReceiver->close(); return a.exec(); }
Voici ce que j'ai dans la console :
The value retrun of this function is 3
The value retrun of this function is 0
Here we write what has been received
t
-
D
Comme vous pouvez le constater, il n'y a rien dans mon vecteur de retour. Savez-vous à quoi cela est dû?
Partager