Arthur Mamou-Mani
Arthur Mamou-Mani

Reputation: 2333

How to access a device and send data to it?

I have managed to identify both drives and serial ports in visual C# express but I still cannot access a specific device (RepRap Printer). I would like to send an array of string to it but first I need to locate it, how can I do that? I am using windows 7.

To get the drives:

using System.Linq;
using System.IO;
using System;

    class Program
    {
            static void Main(string[] args)
            {
                var drives = DriveInfo.GetDrives();
               DriveInfo[] allDrives = DriveInfo.GetDrives();
                     foreach(DriveInfo dv in drives)          
                     {              
                            Console.WriteLine("drive Name:{0}", dv.Name);      
                     }    
                    Console.ReadLine();
            }
        }

to get the serial ports:

using System;
using System.IO.Ports;

namespace SerialPortExample
{
    class SerialPortExample
    {
        public static void Main()
        {
            string[] ports = SerialPort.GetPortNames();
            Console.WriteLine("The following serial ports were found:");
            foreach (string port in ports)
            {
                Console.WriteLine(port);
            }
            Console.ReadLine();
        }
    }
}

Many thanks in advance!

Upvotes: 1

Views: 877

Answers (1)

quetzalcoatl
quetzalcoatl

Reputation: 33536

I encourage you to check those two questions and answers at first:

Get the device name connected to the serial port
This one explains shortly why it is hard, but gives some clues on how to ask the Windows what does it know about the devices

Getting Serial Port Information
Here's a bunch of further code samples.

Generally, you will probably want to find the device by it's NAME that the system assigned to it - you probalby know the name and its something like "reprap#1" etc. I just guess. It may be a good idea to ask to scan all COM-device names and display it to the user so he may choose the proper one..

if you want to automaticaly detect, you can try to detect them by some lower-level details like drivername etc, but usually it is better to just leave that to the user.

Upvotes: 1

Related Questions