Bonjour,
j'essaie de faire un espion RS232 avec visual studio C++ 2008

COM_A COM_B
RX ---> display on console ---> TX
TX <--- display on console <--- RX

j'utilise les classes par défaut
System::IO:orts::SerialPort pour le port série
System::IO:orts::SerialDataReceivedEventArgs pour la gestion d'événement

le problème est que je n'est pas accès au COM_B lorsque de la gestion de l'événement SerialDataReceivedEventArgs sur le COMA (et inversement)

Comment dois-je faire?

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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
 
/*=============================================================================*/
/*
 
  FILE NAME          :   .\Snifer_UART\Snifer_UART.cpp - 0.1
 
  PROJECT NAME       :   Snifer_UART
 
  COMPONENT          :
 
  LAST MODIFIED DATE :   07/08/2015
 
  FILE PURPOSE       :
 
  REFERENCE DOCUMENTS:
 
 
  Date          Modified by CRPRNr       TASKNr  Maintenance description
  -------------|-----------|------------|-------|------------------------------------
  07/08/2015   |  xxx|            |       |  create file
  -------------|-----------|------------|-------|------------------------------------
*/
/*=============================================================================*/
#include "stdafx.h"
 
ref class PortDataReceived
	{
	public:
		/*==============================constructor=====================================*/
		PortDataReceived(void)
			{
			//config port A
			this->systemIOPortSerialPortToUseA = gcnew System::IO::Ports::SerialPort("COM3");
			this->systemIOPortSerialPortToUseA->BaudRate = 9600;
			this->systemIOPortSerialPortToUseA->Parity = System::IO::Ports::Parity::None;
			this->systemIOPortSerialPortToUseA->StopBits = System::IO::Ports::StopBits::One;
			this->systemIOPortSerialPortToUseA->DataBits = 8;
			this->systemIOPortSerialPortToUseA->Handshake = System::IO::Ports::Handshake::None;
 
			//config port B
			this->systemIOPortSerialPortToUseB = gcnew System::IO::Ports::SerialPort("COM5");
			this->systemIOPortSerialPortToUseB->BaudRate = 9600;
			this->systemIOPortSerialPortToUseB->Parity = System::IO::Ports::Parity::None;
			this->systemIOPortSerialPortToUseB->StopBits = System::IO::Ports::StopBits::One;
			this->systemIOPortSerialPortToUseB->DataBits = 8;
			this->systemIOPortSerialPortToUseB->Handshake = System::IO::Ports::Handshake::None;
 
			this->systemIOPortSerialPortToUseA->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(PortDataReceived::DataReceivedHandlerA);
			this->systemIOPortSerialPortToUseB->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(PortDataReceived::DataReceivedHandlerB);
			}
 
		/*==============================destructor=====================================*/
		~PortDataReceived(void)
			{
			//none
			}
 
		/*==============================Event=====================================*/	
 
 
		void Main()
			{
			//open port A
			try
				{
				this->systemIOPortSerialPortToUseA->Open();
				DEBUGGMESSAGE(L"COM PORT " + this->systemIOPortSerialPortToUseA->PortName + " open");
				}
			catch (System::InvalidOperationException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseA->PortName + "The specified port is already open, not mount");
				}
			catch (System::ArgumentOutOfRangeException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseA->PortName + "Argument out of range, not mount");
				}
			catch (System::ArgumentException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseA->PortName + "The port name does not begin with \"COM\". - or -The file type of the port is not supported., not mount");
				}
			catch (System::IO::IOException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseA->PortName + "The port is in an invalid state, not mount");
				}
			catch (System::UnauthorizedAccessException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseA->PortName + "Access denied, not mount");
				}
			catch (System::Exception ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseA->PortName + "Unkwon exception, not mount");
				}
 
			//open port B
			try
				{
				this->systemIOPortSerialPortToUseB->Open();
				DEBUGGMESSAGE(L"COM PORT " + this->systemIOPortSerialPortToUseB->PortName + " open");
				}
			catch (System::InvalidOperationException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseB->PortName + "The specified port is already open, not mount");
				}
			catch (System::ArgumentOutOfRangeException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseB->PortName + "Argument out of range, not mount");
				}
			catch (System::ArgumentException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseB->PortName + "The port name does not begin with \"COM\". - or -The file type of the port is not supported., not mount");
				}
			catch (System::IO::IOException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseB->PortName + "The port is in an invalid state, not mount");
				}
			catch (System::UnauthorizedAccessException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseB->PortName + "Access denied, not mount");
				}
			catch (System::Exception ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseB->PortName + "Unkwon exception, not mount");
				}
 
			//wait exit key
			System::Console::WriteLine("Press Q to quit...");
			System::String^ presskey = "";
			bool Stop = false;
			while( Stop == false )
			{
				System::Console::WriteLine();
				presskey = System::Console::ReadLine();
				if( presskey == "Q" )
				{
					Stop = true;
				}
				else
				{
 
				}
			}
 
			//close port A
			try
				{
				this->systemIOPortSerialPortToUseA->Close();
				DEBUGGMESSAGE(L"COM PORT " + this->systemIOPortSerialPortToUseA->PortName + " close");
				}
			catch (System::InvalidOperationException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseA->PortName + "The specified port is not open, not unmount");
				}
			catch (System::Exception ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseA->PortName + "Unkwon exception, not unmount");
				}
 
			//close port B
			try
				{
				this->systemIOPortSerialPortToUseB->Close();
				DEBUGGMESSAGE(L"COM PORT " + this->systemIOPortSerialPortToUseB->PortName + " close");
				}
			catch (System::InvalidOperationException ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseB->PortName + "The specified port is not open, not unmount");
				}
			catch (System::Exception ^)
				{
				DEBUGWINDOWMESSAGEBOX("COM PORT " + this->systemIOPortSerialPortToUseB->PortName + "Unkwon exception, not unmount");
				}
 
			}
 
	private:
		static void PortDataReceived::DataReceivedHandlerA(Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e)
			{
			System::IO::Ports::SerialPort^ sp = (System::IO::Ports::SerialPort^)sender;
			//System::String^ indata = sp->ReadExisting();
			System::String^ indata = sp->ReadLine();
			//System::Console::WriteLine("Data Received from A:");
			System::Console::Write(indata);
			//System::Console::WriteLine();
			//systemIOPortSerialPortToUseB->Write(indata);
			//sp->Write(indata);
			}
 
		static void PortDataReceived::DataReceivedHandlerB(Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e)
			{
			System::IO::Ports::SerialPort^ sp = (System::IO::Ports::SerialPort^)sender;
			//System::String^ indata = sp->ReadExisting();
			System::String^ indata = sp->ReadLine();
			//System::Console::WriteLine("Data Received from B:");
			System::Console::Write(indata);
			//System::Console::WriteLine();
			//systemIOPortSerialPortToUseA->Write(indata);
			//sp->Write(indata);
			}
 
	protected:
		System::IO::Ports::SerialPort ^ systemIOPortSerialPortToUseA;
		System::IO::Ports::SerialPort ^ systemIOPortSerialPortToUseB;
	};
 
//[STAThread]
/// <summary>Main function of application</summary>
/// <param name='args'>0 or more string</param>
/// <returns>int as code execution</returns>
/// <example>error main(XXXX,YYYY,ZZZZ,....)</example>
int main(array<System::String^ > ^args)
	{
	CONSOLEMESSAGE(L"START RS232 SNIFFER");
	//check of GUI can be show
	//Enabling Windows XP visual effects before any controls are created
	System::Windows::Forms::Application::EnableVisualStyles();
	System::Windows::Forms::Application::SetCompatibleTextRenderingDefault(true);
 
	//used to patch bug windows
	//http://support.microsoft.com/kb/320369
	System::Threading::Thread^ threadSnifer_UART = System::Threading::Thread::CurrentThread::get();
	threadSnifer_UART->CurrentCulture = System::Globalization::CultureInfo::CreateSpecificCulture("en-US");
 
	// Create the main window and run it
	//Application::Run(gcnew Form1());
	PortDataReceived^ MyNewSpy = gcnew PortDataReceived;
	MyNewSpy->Main();
 
	CONSOLEMESSAGE(L"PROGRAMM SNIFFER_UART ENDING");
	return 0;
}