0

I have this Arduino code just for testing purpose:

int num=0;
void setup() {
    Serial.begin(9600);
}

void loop() {
    Serial.println(num);
    num+=1;
    delay(800);
}

Now it prints an integer and increments its value by one. When I open up Serial Monitor it prints as expected every 800ms.

My Arduino is connected on PORT-6

Now if I try to access the port, it says it is in use, I'm trying to access that from a .NET application. How can I do so?

c# code, collected from the internet, modified a little:

using System;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApp1
{
    class Program
    {
        static SerialPort _serialPort;
        public static void Main()
        {
            _serialPort = new SerialPort();
            _serialPort.PortName = "COM6";//Set your board COM
            _serialPort.BaudRate = 9600;
            _serialPort.Open();
            while (true)
            {
                string a = _serialPort.ReadExisting();
                Console.WriteLine(a);
            }
        }
    }
}

How can I sniff data from that serial port ? [Educational Purpose]

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • you should use serialPort event - DataReceived – Peter Ksenak Nov 11 '19 at 07:02
  • You never close it. Of course it will stay "in use". Even _if_ you close it, the OS will need some time to effectively shut it down and make it available for reopening. Use a try/finally and put the close in the finally block, so it will be closed if there is an exception or not. – Fildor Nov 11 '19 at 07:19
  • See [SerialPort.Open Remarks](https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.open?view=netframework-4.8#remarks) and [SerialPort.Close](https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.close?view=netframework-4.8) – Fildor Nov 11 '19 at 07:34
  • Ah, wait. Do you want to use your application _and_ Serial Monitor at the same time? – Fildor Nov 11 '19 at 08:31
  • @Fildor yes, that's what i'm trying to do.. – Maifee Ul Asad Nov 11 '19 at 10:27
  • That won't fly. You can only have one connection to that serial port at any given time. – Fildor Nov 11 '19 at 10:29
  • I have seen some method based on @PeterKsenak 's comment, maybe I will post an answer after I'm done – Maifee Ul Asad Nov 11 '19 at 11:45
  • @Maifee UI Asad - try it. you cannot have opened 2 conn to same port at same time.. use DataReceived event on serial port, and it will be worked.. then give me an echo.. Thanks – Peter Ksenak Nov 11 '19 at 12:23

1 Answers1

1

You can't open a serial port twice.

If what you want is to get to see what is going through the bus (sniffing), you can use virtual ports and port forwarding, see here for a complete example.

Nothing will stop you from replacing any of the tools discussed in the link with your own code (.NET or other), in case they don't suit your needs or if you have enough determination to reinvent the wheel.

Marcos G.
  • 3,371
  • 2
  • 8
  • 16