Serial Communications

Go To Last Post
23 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys,

I am planning a forms based windows console application (C#) to communicate via serial coms with the UART on an ATMega. I have attached a TTL to USB interface module to the uC. When the interface module (4Dsystems) is plugged into the USB, the windows machine creates a COM port which is visible in the device manager and can be opened with serial.open() command. However, which COM port is opened is up to windows and what's available.

 

The problem is that the serial.open() commend expects the programmer to designate which COM port to open. Since the interface module is on the USB, I don't understand how the windows app knows which COM port has been opened.

So, the questions is: how does a windows application know which serial port to open to communicate with a new USB device?

 

The whole .NET C# thing is a new area for me, so please forgive my ignorance. I have googled the snot out of this without success.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

This is a windows-specific question - so you'd really be a lot better off asking in a Windows developer forum.

 

But take a look at  SerialPort.GetPortNames:

 

https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.getportnames?view=dotnet-plat-ext-3.1

 

You can use that to populate a drop-down list, so that the user can choose the required port

 

EDIT - something like this:

 

 

EDIT 2

 

jimlake wrote:
how does a windows application know which serial port to open to communicate with a new USB device?

It doesn't - it requires user input. 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. May 21, 2020 - 04:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can put the port number in the command line when you run program.exe so you dont have to hard code it in your C# program.  This is how I did it:

        string[] args = Environment.GetCommandLineArgs();
        string arg = args[1];
        serialPort1.PortName = arg;

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For the best of both worlds, give the user the option to specify it on the command line or to select it in the GUI ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is the 4D-Systems USB Bridge this Sparkfun thing? https://www.sparkfun.com/products/15009

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

N.Winterbottom wrote:
Is the 4D-Systems USB Bridge this Sparkfun thing? https://www.sparkfun.com/products/15009 

Sparkfun's picture does show the 4D name & logo on it:

 

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. May 21, 2020 - 05:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jimlake wrote:
how does a windows application know which serial port to open to communicate with a new USB device?
USB UART's serial number

AN-226_Windows_Driver_Customization_for_USB_UARTs_226ANR00.pdf (MaxLinear)

[page 7]

...

Customized Registry

...

Custom Config

...

FixCOMPortByDevID

...

FixCOMPortByLocation

...

via Documentation tab of XR21B1420 - MaxLinear

 

Similar for Atmel AVRISP mkII and its clone by Waveshare.

Can I Get a Genuine Atmel AVRISP MKII? | Blog | Kanda

[fifth paragraph]

...

... the original Atmel AVRISP MKII has a unique serial number so that more than one programmer can be connected to a PC as AVR Studio can identify the different units using it. 

...

Atmel AVRISP mkII Block Schematic | AVRISP mkII

 

WCID also has serial number though, if the USB device is on a specific port on a specific hub on a specific USB HCI, then that's another way to identify a USB device.

WCID Devices · pbatard/libwdi Wiki · GitHub

Add Microsoft WCID descriptors to Atmel's ASF USB stack · GitHub

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
For the best of both worlds, give the user the option to specify it on the command line or to select it in the GUI ..

 

Dont you have to be already connected to a COM port to be able to talk to the GUI?  I guess you could have a menu of COM ports to select from, but you wouldn't be able to type in the port number.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MarkThomas wrote:
Dont you have to be already connected to a COM port to be able to talk to the GUI?

Of course not.

 

As shown, the GUI lets the user select one of the available ports, then connect to that

 

See #2.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


When I talk to an avr app, I automatically scan the available ports & keep talking until I find the desired response.

So you just hit "connect"  & it does the work for you.

In some apps, while disconnected, I scan continuously so as soon as you plug in the cable it automatically connects--no need to do anything!

 

if these fail, there is a manual mode to force a connection to a specific port

 

   

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
 I automatically scan the available ports & keep talking until I find the desired response

That's really not a friendly thing to!

 

It can be really annoying to have an unrelated app sending unexpected, unsolicited stuff at you!

 

This is effectively what MS do in trying to detect serial mice - and it leads to all kinds of grief!

 

angry

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's really not a friendly thing to!

Oh, there's only one serial comm port that actually connects to anything in these systems so you either talk to the unit or to nothing.  Granted, that might not always be the case. 

The ping is only one character looking for a specific reply, so it prob wouldn't upset the cart too badly.

If you manually try selecting different ports you end up doing the same thing (are we connected yet....arrrg lets try comm 19, the other 18 did nothing).

Automation was put in place, since people wasted a lot of time trying all of the available ports.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Fri. May 22, 2020 - 05:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
As shown, the GUI lets the user select one of the available ports, then connect to that

Yeah, that's what I said.  I was thinking about typing in the COM port number, but you would have to be already connected to type it in.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MarkThomas wrote:
 but you would have to be already connected to type it in.

Eh?

 

I think we're talking at cross purposes here?

 

Are talking about "typing" into the thing that's at the other end of the COM port link?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If your USB device has a specific, known, VID/PID see:

 

https://www.codeproject.com/Tips/349002/Select-a-USB-Serial-Device-via-its-VID-PID

 

Of course, if you have more than one of the same device, you'll still need user input to say which particular one the user wishes to use at the particular time.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OP seems to have run away. I'm still waiting to find out whether he uses Silicon Labs USB Bridge.

 

Let's assume he does use Silicon Labs:

 

The Test Technician where I work has achieved this using Labview in the application of PCB Test. For Silicon Labs CP2102 (and others in the family no doubt) Windows will allocate a new COM port number for each unique CP2102. I believe he used this feature to recognise the PCB without any UI COM Port selector. I don't know the details I'm afraid.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

From the  Sparkfun & 4D pages, it seems that there are (at least) 2 versions of this thing - one uses SiLabs, the other has FTDI

 

For Silicon Labs CP2102 (and others in the family no doubt) Windows will allocate a new COM port number for each unique CP2102

Same for FTDI.

 

In fact, that's exactly what the image I posted in #2 is showing!

 

I believe he used this feature to recognise the PCB without any UI COM Port selector

It would need user input to know which COMx is the one of interest

 

eg, I know that COM9: is the one I want from the list shown in #2.

 

EDIT

 

It's just like when you have multiple ICEs connected - Atmel Studio can give you a list:

 

 

But the user has to know & select which one is connected to the particular Target of interest

 

EDIT 2

 

And, like Atmel Studio, you could have the app remember your previous selection - so, if you always use the same adaptor, you don't have to keep reelecting the same one every time you start the app.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. May 22, 2020 - 10:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Are talking about "typing" into the thing that's at the other end of the COM port link?

Yes.  I was thinking instead of typing the COM port number on the command line you would type the COM port number into the GUI, but you would have to already be connected to a COM port to do that.  Just me thinking in circles.  SOrry.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think I am confused.  Never mind.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Overview | How to Find Hidden COM Ports | Adafruit Learning System

[second paragraph]

You may have noticed that every time a new FTDI-based board is plugged in, you get a new COM port. You might also get new COM port assignment with adapters, etc. 

...

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gchapman wrote:
You may have noticed that every time a new FTDI-based board is plugged in, you get a new COM port. You might also get new COM port assignment with adapters, etc.

I have noticed that every time I use a new USB-serial converter I get a new COM port number.  I am up to 30 something, and I read here that it will go past 128, so there is at least one byte allocated in Windows for the port number.

 

As to typing the port number into the GUI, it is pretty obvious after thinking about it that the desired port number could be typed into a C# text box, and then clicking on a C# button could connect the form to the typed in COM port like a PuTTY window.  I should know better and think about things a little before reacting here and saying dumb stuff.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MarkThomas wrote:
I read here that it will go past 128,

See #2 !

 

Quote:
the desired port number could be typed into a C# text box,

See #2 - the GUI can easily give you a list of available ones, and you just click the one you want.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see.  I didn't follow the link to see how you get ports currently in use.  I just blundered ahead.  Thanks for method!  That makes it a good day - I learned something new.