Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i try to connect with machine from setup(exe) then it gives error and hang setup, it is not all time but some time it should be connect and some times gives error.

when i try same with asp.net platform, like that is run by asp.net code at that time no any error is occurs.(it works fine).

What I have tried:

C#
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {             
                Thread.Sleep(10);
                _rxString = "";
                _rxString = serialPort.ReadExisting();
                                         
                this.Invoke(new EventHandler(StartUTM));
              
               }
            catch (Exception ex)
            { 
                log.Error("Error", ex);
            }
Posted
Updated 18-Feb-18 22:25pm
v2
Comments
OriginalGriff 19-Feb-18 4:24am    
And what error do you get?

1 solution

I think you need a try ... catch around the part that initializes serialPort, and use the sender object, something like this:
var serialPort = new SerialPort(Rs232Port);

        private static void SerialDataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                var sp = (SerialPort)sender;
                var message = sp.ReadExisting();
                Debug.Print("Serial data received: " + message);
            }
            catch (Exception ex)
            {
                Debug.Print(@"SerialDataReceivedHandler() " + ex.Message);
            }
        }
 
Share this answer
 
v2

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