Autor Beitrag
blutengel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59

Win XP, WIN 7
VS#2008 Prof
BeitragVerfasst: Di 17.08.10 11:19 
Hallo und guten Morgen!

Habe da mal ein Problem. Und zwar rufe ich über Com Port einen bestimmten Wert ab, hier die 85. Mein Problem ist jetzt das mein Programm sich aufhängt wenn die Zielharware nicht antwortet.ie bekomme ich es hin das es so eine Art Timeout gibt.Also das er nach einer bestimmten Zeit den Lesevorgang auf dem Com Port abricht und eine Meldung rausgibt?
Hier meine Code:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
private void button3_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                try
                {
                       byte[] busy = new byte[1];
                        COM1.Open();
                        COM1.Write("U");
                        System.Threading.Thread.Sleep(1);
                        int message = COM1.ReadByte();
                        COM1.Close();

                        if (message == 85)
                        {
                            //MessageBox.Show(Convert.ToString(message));
                            MessageBox.Show("Programmiermodus OK!", Application.ProductName,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Asterisk);
                            btn_send.Enabled = true;
                        }

                }
                catch  
                {
                    MessageBox.Show("COM1 nicht verfügbar." + Environment.NewLine + "Bitte einen anderen Port wählen", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
            else
                if (radioButton2.Checked)
                {
                    try
                    {
                        byte[] busy = new byte[1];
                        COM2.Open();
                        COM2.Write("U");
                        System.Threading.Thread.Sleep(1);
                        int message = COM2.ReadByte();
                        COM2.Close();
                        if (message == 85)
                        {
                            //MessageBox.Show(Convert.ToString(message));
                            MessageBox.Show("Programmiermodus OK!", Application.ProductName,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Asterisk);
                            btn_send.Enabled = true;

                            
                        }
                    }
                    catch
                    {
                        MessageBox.Show("COM2 nicht verfügbar." + Environment.NewLine + "Bitte einen anderen Port wählen", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

        }



MFG


blutengel


Moderiert von user profile iconKha: Topic aus WinForms verschoben am Di 17.08.2010 um 18:29
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4807
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 17.08.10 12:56 
Schau dir mal die SerialPort.ReadTimeout-Eigenschaft an...
blutengel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59

Win XP, WIN 7
VS#2008 Prof
BeitragVerfasst: Di 17.08.10 13:07 
Das habe ich mir ja schon angeschaut.Habe auch srausgefunden das man mit serialPort.ReadTimeOut(500); die Zeite einstellen kann. Aber ich verstehe einfach nicht wie man nach den 500 Millisekunden eine Fehlermeldung rausgeben kann und den Port schließen so das sich das Programm nicht mehr aufhängt.


MFG

blutengel
Greenberet
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 339
Erhaltene Danke: 20

Win 10
C# (VS 2012), C++ (VS 2012/GCC), PAWN(Notepad++), Java(NetBeans)
BeitragVerfasst: Di 17.08.10 13:09 
Schonmal den MSDN Artikel dazugelesen?

msdn.microsoft.com/d...ort.readtimeout.aspx


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
//....
public static void Read()
{
    while (_continue)
    {
        try
        {
            string message = _serialPort.ReadLine();
            Console.WriteLine(message);
        }
        catch (TimeoutException) { }
    }
}

Für diesen Beitrag haben gedankt: blutengel