| |

VerySource

 Forgot password?
 Register
Search
View: 3419|Reply: 26

The problem with messages in c #, how to receive messages from multiple message sources to determine?

[Copy link]

1

Threads

15

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-5 16:00:01
| Show all posts |Read mode
The function of sending messages is defined in a dll,
bool StartMonitor (HANDLE hComm, HWND hOwner)
{
DWORD dwThreadId;


CommInfo.hComm = hComm;
CommInfo.hOwner = hOwner;

hThread = CreateThread ((LPSECURITY_ATTRIBUTES) NULL, 0, ThreadProc,&CommInfo, 0,&dwThreadId);
if (hThread == NULL)
return false;

return true;
}

DWORD WINAPI ThreadProc (LPVOID lpParameter)
{
COMMINFO * pCommInfo;
DWORD Bytes = 0;
DWORD BytesToRead = 0;
DWORD ErrorFlags = 0;
char CnfMsg [MAX_BUF_SIZE] = "\0";
char szReadBuf [MAX_BUF_SIZE] = "\0";
DWORD dwEvtMask = 0;
DWORD dwSignal = 0;
Ranch
pCommInfo = (COMMINFO *) lpParameter;

// Clear the serial buffer
PurgeComm (pCommInfo-> hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
Ranch
// Which serial events need to be monitored
SetCommMask (pCommInfo-> hComm, EV_ERR | EV_RLSD | EV_RING | EV_RXCHAR | EV_TXEMPTY);
    
    while (1) {
WaitCommEvent (pCommInfo-> hComm,&dwEvtMask,&Overlapped);
dwSignal = WaitForMultipleObjects (2, hEvents, FALSE, INFINITE);

if (dwSignal == WAIT_OBJECT_0) {
if ((dwEvtMask&EV_RXCHAR) == EV_RXCHAR) {
Sleep (100);
RecvChar (pCommInfo-> hComm);
if (strlen (RecvBuf)! = 0) {

strupr (RecvBuf);

#ifdef SMS_DLL_DEBUG
sprintf (RecvDebugInfo, "<-% s", RecvBuf);
if (strncmp (RecvBuf, "ATWITS", 6))
PostMessage (pCommInfo-> hOwner, WM_RECVDATA, 1, (LPARAM)&RecvDebugInfo);
else
if (DispFlag)
PostMessage (pCommInfo-> hOwner, WM_RECVDATA, 1, (LPARAM)&RecvDebugInfo);
#endif

if (! strncmp (RecvBuf, "ATWITS + CMTI: 1", 13)) {
if (pCommInfo-> hOwner! = NULL)
PostMessage (pCommInfo-> hOwner, WM_RECVSMS, (LPARAM) pCommInfo-> hComm, 0);
}
// else if (! strncmp (RecvBuf, "ATWITS", 6)) {
// if (pCommInfo-> hOwner! = NULL)
// PostMessage (pCommInfo-> hOwner, WM_POWERON, 0, 0);
//}
else if (strlen (RecvBuf)! = 0) {
// Increase critical section control
memset (RetInfo, 0, MAX_BUF_SIZE);
strcpy (RetInfo, RecvBuf);
SetEvent (hRecvEvent);
}
Ranch
memset (RecvBuf, 0, MAX_BUF_SIZE);
}
}

ResetEvent (Overlapped.hEvent);
}
else if (dwSignal == WAIT_OBJECT_0 + 1) {
SendChar (pCommInfo-> hComm);
}
}

return 0;
}

Calling StartMonitor in the c # program can enable monitoring of the com port, but the program needs to monitor multiple serial devices on a computer, so the winproc function is overloaded in c #. m.WParam, phoneNum, msg); the above message is not received well, I wonder if everyone has a good way
Reply

Use magic Report

1

Threads

15

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-1-5 19:48:01
| Show all posts
Hope you give some constructive comments
Reply

Use magic Report

1

Threads

15

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-1-7 13:12:01
| Show all posts
No one to help
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-1-7 15:54:01
| Show all posts
Bangding, I don't understand communication!
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-1-7 21:09:01
| Show all posts
come and see..

Learn + like ..
Reply

Use magic Report

0

Threads

13

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-1-11 00:00:01
| Show all posts
learning
Reply

Use magic Report

0

Threads

23

Posts

20.00

Credits

Newbie

Rank: 1

Credits
20.00

 China

Post time: 2020-1-13 20:00:01
| Show all posts
Can't receive well? How bad is it, please describe in detail
Reply

Use magic Report

1

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-1-18 21:09:01
| Show all posts
Don't understand
Reply

Use magic Report

0

Threads

15

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-19 18:54:01
| Show all posts
I don't understand your code too much.My program also needs to monitor multiple serial ports.I wrote a class to send and receive data to the serial port, and then instantiate this class in the main form. Instantiate multiple classes, and use the timer to accept serial data in the class, and then send it to the main form.
Reply

Use magic Report

1

Threads

15

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-1-20 13:27:01
| Show all posts
In c #, I open each serial port in a loop. After all the devices are opened, the main program can only receive messages from the last serial port. The previous serial ports are not working. So I would like to ask everyone to help.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list