3

I am currently using the .NET 7.0 framework in Visual Studio. I am trying to transfer a file over a serial port however I am having some trouble. I am just wondering if there is a NuGet package or something I must do in order to enable me to transfer said file. I have attached below the code I have tried, let me know if I have made any mistakes.

In .NET4.8 there is a SerialPort object however it doesn't apper in any later versions.
DOES ANYBODY KNOW IF THIS OBJECT IS OBTAINABLE IN .NET7.0

SerialPort myPort = new SerialPort("COM3", 19200, Parity.None, 8, StopBits.One);
myPort.PortName = "COM3";
myPort.BaudRate = 19200;
myPort.Parity = Parity.None;
myPort.DataBits = 8;
myPort.StopBits = StopBits.One;

myPort.Open();

Data = FileBuild.SelectMany(s =>
        System.Text.Encoding.UTF8.GetBytes(s + Environment.NewLine)).ToArray();
// Data = FileBuild.SelectMany(BitConverter.GetBytes).ToArray();

myPort.Write(Data, 0, Data.Length);
myPort.Close();

The error message provided was:

System.IO.FileNotFoundException: 'Could not find file 'COM3'.'

Jack
  • 41
  • 4
  • ...how do you know your computer has a serial-port named `COM3`? – Dai Apr 24 '23 at 09:11
  • in which line is the exception thrown? – Mong Zhu Apr 24 '23 at 09:15
  • Checked in device manager, it's called COM3 and I can rename it if needed. – Jack Apr 24 '23 at 09:16
  • The exception is thrown on the 'myPort.Open();' line – Jack Apr 24 '23 at 09:17
  • 1
    it seems that they have outsourced it to [nuget](https://www.nuget.org/packages/System.IO.Ports/) as is described in this [post](https://stackoverflow.com/a/66306168/5174469). – Mong Zhu Apr 24 '23 at 09:24
  • I already had the package for Nuget that is suggested in the above comment – Jack Apr 24 '23 at 09:43
  • `FileBuild.SelectMany(s => System.Text.Encoding.UTF8.GetBytes(s + Environment.NewLine)).ToArray();` - my bet would be on this line, rather than an issue with `SerialPort`. Do you have a Stacktrace that goes along with that error message? – Fildor Apr 24 '23 at 09:44
  • 1
    BTW: It's always a good idea to disclose if it's an _actual_ serial connection or an emulated one via USB. – Fildor Apr 24 '23 at 09:46
  • 1
    Its emulated via USB – Jack Apr 24 '23 at 09:49

0 Answers0