Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I develop serial port data receive software.
I this i want to make when start button press check is data in buffer if not then check if data receive start further process.

This code is data receive code and this code is in other class file.And my start button is at Mainscreen page.
C#
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                if (comPort.IsOpen == true)
                {
                    int bytes = comPort.BytesToRead;
                        
                    byte[] comBuffer = new byte[bytes];
                    comPort.Read(comBuffer, 0, bytes);
                    MainScreen._message = ByteToHex(comBuffer);
                }
            }
Posted
Updated 24-Nov-11 20:11pm
v2

1 solution

I'm not sure what your problem is, but it may be related to instances.
Your code shows
C#
MainScreen._message = ByteToHex(comBuffer);
The problem is that _message would normally be the name given to an internal field, used as a property base, and declared as private (maybe protected). If your code compiles, then if (as you say the comPort code is in a different class then it is accessing a field in the MainScreen class. Not only that, but it would appear to be a static field - which means it is not related to any particular instance of the main screen.

Check this! If you are accessing a field, rather than a property, then change that - you should not in general expose fields to elements outside the class they are defined in. You probably need an instance of the MainScreen class as well, or it won't know where to display it...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900