Home > Software engineering >  Using the SerialPort class implements a serial port communication, serial port two serial number and
Using the SerialPort class implements a serial port communication, serial port two serial number and

Time:11-12

I try to weave a two serial port to send and receive the number of software at the same time, using the SerialPort class implements the serial port, now send separately for each serial number has been completed, but the two serial ports open at the same time, send them separately with the com wizard number, there will be a serial port 1 received the number 2, the serial port will receive a number 1, 2, excuse me, what is the problem that how to change?
This is a serial port 1 open
Void CComTestToolDlg: : OnBnClickedButtonCom4open ()
{
//TODO: Add your the control notification handler code here
Cstrings STR.
GetDlgItemText (IDC_BUTTON_Com4Open, STR);
The CWnd * h1.
H1=GetDlgItem (IDC_BUTTON_Com4Open);

If (STR==_T (" open the serial port 4 "))
{

M_Ports [0]. InitPort (this, 1115, 200);
M_Ports [0]. StartMonitoring ();
STR=_T (" close a serial port 4 ");
The UpdateData (true);
H1 - & gt; SetWindowText (STR);
}
The else
{
If (STR==_T (" close a serial port 4 "))
{
STR=_T (" open the serial port 4 ");
UpdateData(TRUE);
H1 - & gt; SetWindowText (STR);
}
M_Ports [0]. StopMonitoring ();
M_Ports [0]. ClosePort ();
}

}
This is a serial port 2 open
Void CComTestToolDlg: : OnBnClickedButtonCom6open ()
{
//TODO: Add your the control notification handler code here
Cstrings STR.
GetDlgItemText (IDC_BUTTON_Com6Open, STR);
The CWnd * h1.
H1=GetDlgItem (IDC_BUTTON_Com6Open);

If (STR==_T (" opens a serial port 6 "))
{

M_Ports [1]. InitPort (this, 3115, 200);
M_Ports [1]. The StartMonitoring ();
STR=_T (" close a serial port 6 ");
The UpdateData (true);
H1 - & gt; SetWindowText (STR);
}
The else
{
If (STR==_T (" close a serial port 6 "))
{
STR=_T (" to open a serial port 6 ");
UpdateData(TRUE);
H1 - & gt; SetWindowText (STR);
}
M_Ports [1]. StopMonitoring ();
M_Ports [1]. ClosePort ();
}
}
This is to run screenshot

CodePudding user response:

On the physical serial port connection is? Is independent of the four serial ports two connections?
If only open a set of serial port to send and receive is normal?


CodePudding user response:

reference 1st floor zgl7903 response:
physical serial port is how to connect? Is independent of the four serial ports two connections?
If only open a set of serial port to send and receive is normal?
no physical serial port, I use virtual serial ports created two serial ports, software serial port and serial port elf 2 is a pair of 4, 6 and serial port serial elves are 4 pair, each group of serial port to send and receive alone is correct

CodePudding user response:

Launch of four com wizard each other mutually close?
A serial port to receive and display refresh you is how to deal with?

CodePudding user response:

The
reference 3 floor zgl7903 response:
launch of four com wizard send the mutually close?
A serial port to receive and display refresh you is how to deal with?
no problem between com wizard, SerialPort class initialization of a serial port and thread function to receive data, read data and send a message to a dialog box, and then display in the listbox,
This section of the program in initialization
M_hComm=CreateFile (szPort,//the communication port string (COMX)
GENERIC_READ | GENERIC_WRITE,//read/write types
0,//comm devices must be the opened with exclusive access
NULL,//no security attributes
OPEN_EXISTING,//comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED,//Async I/O
0);//the template must be zero for comm devices
I think it is both a serial port calls the initialization, actually generate a file only m_hComm, the received data were stored in the file, cause when reading data of each string will happen, but I also tried to write two initialization program, two serial port initialization, CreateFile m_hComm1 also created two files, m_hComm2, but is still the same result

CodePudding user response:

Write a complete, start the two threads/instance



CodePudding user response:

reference 5 floor zgl7903 reply:
write a complete, start the two threads/instance
BOOL CSerialPort: : StartMonitoring1 ()
{
if (! (m_Thread=AfxBeginThread (CommThread1, this)))
return FALSE;
TRACE (" Thread started \ n ");
return TRUE;
}
BOOL CSerialPort: : StartMonitoring2 ()
{
if (! (m_Thread=AfxBeginThread (CommThread2, this)))
return FALSE;
TRACE (" Thread started \ n ");
return TRUE;
Is that right, but the result is still the same

CodePudding user response:

Threads or class of incoming connection port and window handle, like
Typedef struct _tagThreadParam_t
{
Int nPort.//associated port
HANDLE hCom.//port handle
The HWND hWndEdit;//associated edit box
};


CodePudding user response:

SerialPort receiving data message has a parameter is the serial number

CodePudding user response:

SerialPort receiving data message has a parameter is a serial number,
SerialPort class has a bug

CodePudding user response:

A serial port, a object, handle to manage themselves, how can go wrong?

CodePudding user response:

You should be so, for example, if you use a serial port virtual assistant, two of the virtual serial port 3, 4, 5, 6, open the serial port that you now in the procedures section 4 and 6 serial port to send data, it should be a serial port 3 received a serial port 4 to send data, a serial port to receive a serial port 6 send 5 data, and now you send data to the serial port is a serial port 4 5, a serial port 6 send data to the serial port of 3?
This is impossible, I think you are confused? Ha ha ha ha, look at your code, m_Ports [0]. InitPort (this, 1115, 200); Written comments on four, open the serial port serial number is 1, m_Ports [1]. The InitPort (this, 3115, 200); Open the serial port 6, written in the annotations open serial port 3 indeed, you have to clear up the relationship between serial port first?

CodePudding user response:

Upstairs said several very for Mr, the building Lord problem solving, is on a computer, with two pairs of virtual serial port, such as 1 and 2, 3 and 4, then respectively corresponding to two serial ports, with two objects,
  • Related