| |

VerySource

 Forgot password?
 Register
Search
Author: hyukio35

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

[Copy link]

0

Threads

15

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-20 17:36:01
| Show all posts
I have encountered the same problem as you before, consider using the class method to solve it, instantiate a class, a class is a serial port
Reply

Use magic Report

1

Threads

15

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 United States

 Author| Post time: 2020-1-21 14:54:02
| Show all posts
Let's take a look at the classes I use
 class threadCom
    {
        private string _com;
        private IntPtr _handle;

        public threadCom ()
        {

        }

        public threadCom (string com, IntPtr handle)
        {
            this._com = com;
            this._handle = handle;
        }

        private void openCom ()
        {
             operateCom.OpenComm (_com, _handle);
        }

        public void startThread ()
        {
            ThreadStart thr_star_fun = new ThreadStart (openCom);
            Thread fthread = new Thread (thr_star_fun);
            fthread.Start ();
        }

    }
Called in the main function
  foreach (string s in avaliableEquipment) where s is com1 and com2
            {
              threadCom tCom = new threadCom (s, this.Handle);
                tCom.startThread ();
            }
But I still can't receive the first com port message, I can only receive com2, I don't know what happened?
Reply

Use magic Report

1

Threads

15

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-1-21 23:18:01
| Show all posts
Please help
Reply

Use magic Report

0

Threads

15

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-22 00:54:01
| Show all posts
You will know if you execute it in a single step.Com1 has finished executing com2. Finally, only com2 exists, and com2 covers com1.
Reply

Use magic Report

1

Threads

15

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-1-22 08:00:01
| Show all posts
So what should I do?
Reply

Use magic Report

1

Threads

15

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-1-22 10:09:01
| Show all posts
I changed the program to the following or not
  threadCom [] tcom = new threadCom [avaliableEquipment.Length];
            int i = 0;

            foreach (string s in avaliableEquipment)
            {
                tcom [i] = new threadCom (s, this.Handle);
              // threadCom tCom = new threadCom (s, this.Handle);
                tcom [i] .startThread ();
            }
Reply

Use magic Report

0

Threads

15

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-22 10:27:01
| Show all posts
Put operations on the serial port such as open, close, send, and receive data in a class.Each time a class is instantiated, it is equivalent to generating a serial port, so you can have multiple serial ports to send and receive at the same time.
C # is object-oriented. Use object-oriented thinking to consider problems. A form is also a class. You can instantiate multiple forms. The operations between multiple forms do not affect each other. Right, then you can instantiate Multiple serial ports will not interfere with each other.
Reply

Use magic Report

1

Threads

15

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-1-22 11:00:01
| Show all posts
Can you tell me more in detail? I still don't quite understand this
Reply

Use magic Report

1

Threads

15

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-1-22 14:18:01
| Show all posts
I am just a beginner, so I hope everyone can give more detailed instructions, thank you in advance!
Reply

Use magic Report

1

Threads

15

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-1-25 07:36:02
| Show all posts
Masters please come, brothers are really not broken now.
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