Entwickler-Ecke

Netzwerk - Comport ansprechen...


tomycat - Do 27.04.23 11:45
Titel: Comport ansprechen...
hallo,
ich will NUR auf dem Comport 5 eine Zeichenkette 1234 senden.

Mein Muster: SerialPort [https://learn.microsoft.com/de-de/dotnet/api/system.io.ports.serialport?view=dotnet-plat-ext-7.0]

Mein Projekt mit Konsole C# mit VS 2019
Alles Original, von meinem Muster ...

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:
using System;
using System.IO.Ports;
using System.Threading;

public class SerialPort : System.ComponentModel.Component 
{ }

public class PortChat
{
    static bool _continue;
    static SerialPort _serialPort;

    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort.PortName = SetPortName(_serialPort.PortName);  /////// Portname ist rot ?!?!
................schnipp.........

Zitat:
SerialPort enthält keine Definition für PortName.


Moderiert von user profile iconTh69: URL-Titel hinzugefügt
Moderiert von user profile iconTh69: C#-Tags hinzugefügt


jaenicke - Do 27.04.23 11:59

user profile icontomycat hat folgendes geschrieben Zum zitierten Posting springen:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
public class SerialPort : System.ComponentModel.Component 
{ }

public class PortChat
{
    static bool _continue;
    static SerialPort _serialPort;

Die Deklaration von SerialPort ist dort nur zur Information vorhanden. Wenn du die bei dir einfügst, legst du eine neue leere Klasse an. Die enthält dann auch nicht PortName.


tomycat - Do 27.04.23 13:44

thx,
sorry, bin auf dem Schlauch, was soll ich einfügen jetzt?


Ralf Jansen - Do 27.04.23 14:32

Nicht einfügen du sollst etwas wegnehmen.
Am Anfang deines Codes hast du das hier eingefügt


C#-Quelltext
1:
2:
public class SerialPort : System.ComponentModel.Component 
{ }


Das musst du wegmachen. Damit hast du eine neue "SerialPort" Klasse erfunden und die kann nichts wie du festgestellt hast. Die SerialPort Klasse gibt es schon im Framework und die existierende möchtest du benutzen. Die ist momentan von deiner überdeckt und der Compiler findet die falsche deine neu erzeugte Klasse.


tomycat - Mi 03.05.23 14:11

thx,

sooo habs weggenommen.

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:
using System;
using System.IO.Ports;
using System.Threading;

public class PortChat
{
    static bool _continue;
    static SerialPort _serialPort; ////////////////// SerialPort <--- ist ROT

    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort(); ////////////////// SerialPort <--- ist ROT

        // Allow the user to set the appropriate properties.
        _serialPort.PortName = SetPortName(_serialPort.PortName);

-----------------schnipp-----------------------


Das rot hinterlegte:
Der Typname"SerialPort" konnte nicht im Namesspace "System.IO.Ports" gefunden werden. Dieser Tpe wurde an Assembly "System.IO.Ports,Version4.0.1.0 Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51 weitergeleitet. Sie sollten einen Verweis auf die Assembly hinzufügen.

Mit dem Form1 Projekt C# habe ich es hinbekommen.
Ich brauche aber den Comport unter der Konsolen App

Leider finde ich kein Assembly


Th69 - Mi 03.05.23 14:48

Binde System.IO.Ports [https://www.nuget.org/packages/System.IO.Ports/#versions-body-tab] (entsprechend der von dir verwendeten .NET Version) per NuGet ein.