-
Notifications
You must be signed in to change notification settings - Fork 846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
COM Port Access in WSL #1929
Comments
There is a feature request for that somewhere on uservoice, but sadly, accessing the windows USB/com ports is afaik not yet possible. |
Serial port access has been added in Insider build 16176. See https://blogs.msdn.microsoft.com/wsl/2017/04/14/serial-support-on-the-windows-subsystem-for-linux/ Note that |
@dhalbert: Thanks for the heads up. Just to make sure I read this right: this is not part of CU right? |
Using 16176 and it is supposed to work. It's not part of cu, it's part of the WSL, exposing serial ports as device nodes. There's a couple of missing IOCTLS that prevent minicom from working, but cu should. |
I tried using |
Thanks for trying out the serial support. Can you please try the follow: 1.) Please double check that your COM port shows up in the registry under HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM 2.) The ttyS number matches the COM port number (e.g. ttyS1->COM1, ttyS2->COM2, ...) 3.) No other process has the COM port open (Windows only allows a single open). 4.) Try running with an elevated command prompt and as root |
You have to specify the tty as the |
Appreciate the feedback, I've never used cu before. sudo cu -l /dev/ttyS4 Same stands for all of my COM ports; in the elevated bash, using sudo. |
That should get rid of the permission error. |
@dhalbert, were you able to resolve the issues that you were hitting? If not, can you give me a quick rundown of your setup? I have some serial hardware and if it matches I can try to repro. Locally I used cu to connect to a RaspPi, |
Thanks, @dhalbert - your suggestion worked, sorta. I'm able to connect to the 'line' now, but still not seeing any of the serial traffic. Probably related to what @stehufntdev mentioned about line settings, which I've tried his but to no avail... need to read up on this a bit more and make sure I have the settings correct. |
@nosajthenitram: Yes, that's the problem I see. I am trying to connect to an Adafruit M0 Feather board which is running CircuitPython (variant of MicroPython), talking to the REPL. I did this:
At that point I can ask
I can connect to the same COM4 port with Tera Term or putty without difficulty, also at 9600 baud (that probably doesn't matter since it's USB to serial). I tried your |
Thanks @dhalbert, appreciate the extra details! I was able to cu fine with a raspberry pi over a usb to serial connector which is the closest thing I have, and wanted to share out the notes. On my machine, the raspberry pi is connected on COM5. Running as a Windows administrator should only be needed if your COM port is only accessible by a Windows administrator. 1.) Here's the output: stehufntdev@stehuf-test-6:/mnt/c/Users/stehuf$ sudo chmod 666 /dev/ttyS5 Raspbian GNU/Linux 8 raspberrypi ttyAMA0 raspberrypi login: pi The programs included with the Debian GNU/Linux system are free software; Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent SSH is enabled and the default password for the 'pi' user has not been changed. pi@raspberrypi:~$ |
Not sure which of the above stty settings did it, but something pushed it to work. :) public@systemhost:/dev$ sudo cu -l /dev/ttyS4 -s 9600 Raspbian GNU/Linux Thanks for the support in getting this going. |
Great to hear, thanks for the confirmation! Not sure why some serial devices initialize with non-conventional configurations. For the most part, WSL reports back the current configuration since there isn't really a canonical serial device configuration with the proliferation of serial devices. I'll see if we can update the blog with some of the commands above since it will probably be a common question. @dhalbert, did the steps above resolve the issue you were seeing? |
@stehufntdev I tried both the The device as shown in Device Manager is just "USB Serial Device (COM4)". The driver is |
@dhalbert, thanks for the update and trying out the different scenarios! It's really strange that you can't read from the serial port with powershell, and I haven't come across a device with that behavior. I'll ask around to see if I can get something similar from our hardware lab. One thing that might be good to check is to see if Tera Term or Putty are setting different serial port settings which cause the device to function. Can you please try the following to see if you can use powershell\WSL with the settings populated by Tera Term or Putty: 1.) Load up Tera Term or Putty so the serial device is functional |
Try anything you can load CircuitPython on MicroPython on. An Arduino Zero might be more readily available. I haven't tried a BBC micro:bit but that might exhibit the same behavior when loaded with MicroPython if it has a REPL. These MS guys might be able to help: https://makecode.com/blog/one-chip-to-flash-them-all, https://github.com/Microsoft/uf2. I will test as you suggested and maybe try writing some simple test programs as well. |
@stehufntdev - No luck on killing Tera Term and then using its settings (which are not unusual: 9600 baud, 8N1, no flow control). Also tried various things from WSL using Python. I set up a program on the board to continuously output some text with numbers separated by
[hangs] I have a feeling this is due to something about WSL's interactions with the stock |
@dhalbert, thanks for all of your persistence here really appreciate the help! I borrowed a Arduino Mega 2560 which uses usbser.sys as well, and was able to go through the steps above for Since both powershell and WSL don't work on your device with reasonable configuration settings, I'm starting to wonder if your device doesn't report read available correctly. If you have time to further debug, could you try out the MSDN sample code just below the "...one oversight covered later in this section" line? That block of code waits for read available and then reads the data which is what is done in WSL. Based on your results we can rule out a read available issue with the device. Today I'll see if I can get ahold of a Adafruit M0 Feather board. If you can share out any details on your setup that might be relevant that would be helpful since I haven't worked with these devices previously :). |
@stehufntdev - Thanks for looking at this. The Mega uses an Atmel 328 microcontroller, which does not have native USB support, so the board provides USB connectivity with another small microcontroller that just manages the USB port, so it may be acting somewhat differently. The M0 boards uses an Atmel 32u4-based-board which has native USB support. I will try the MSDN code, though I have to get VS installed on the Insider machine. If you have a precompiled test for the read-available issue, I could use that, but don't spend a lot of time on building it. To test with a Feather M0:
So now the board will print numbers continuously and you can test reading in WSL or in Windows proper. There is an outdated tutorial here, but the above should duplicate what I'm doing. |
@dhalbert, thanks again! All of my serial test code was kernel mode so we couldn't reuse it here. Let me know if getting the sample setup proves to be painful and I can share out the code and a binary :). Thanks for the details, I have a lead on a Feather M0 but waiting to hear back. |
@stehufntdev - I hacked up a test program to read chars from the Feather M0 running the Python print loop shown above. I included the problematic character reading loop as described in the MSDN sample code you referenced. Here's the program (as I'm not quite sure what I'm testing here, but I hope this helps. I originally tested this on vanilla Creators Update (while waiting for VS to install on my wimpy Insider Build machine) and then 16179. When run, the programs reads and prints about 50-100 lines of increasing numbers, and then hangs. When it starts, it reads some old output that seem to have been previously buffered and then skips ahead to the latest set of consecutive numbers:
|
@dhalbert, thanks for sharing out the results. Based on the above the device reported back read available but it's concerning that it hung. Going through your sample code I realized we wanted to try out the block without the caveat :(, sorry for the oversight there. If it's not too much trouble could you re-run with the code in the block just below in the sample that has a do while loop on the read? I'm hoping to rule out the hang is a result of read available. Also, thanks for your tip on the https://github.com/Microsoft/uf2 project. I spoke with that team and was able to borrow a Feather M0 to setup a local repro. I should be able to start debugging today and will report back. |
@stehufntdev - With the do-while loop added, the sample program works just fine. It continuously reads my continuously-printing python loop above. Glad you got in contact with the uf2 people. Their bootloader is very nice. Version 0.9.6 of CircuitPython is now released. I just tested it with |
Thanks @dhalbert, appreciate all your help here. I was able to reproduce the hang with the device and tried a few different scenarios. Here's one that was particularly concerning: 1.) Reboot machine However, if I then connect with putty, putty shows the output. If I then close putty and rerun serial_read2.exe it shows the output just fine. Can you confirm if you are seeing the same behavior? Based on the above, the serial device doesn't output with standard settings and putty is setting something that makes it work. Not sure I would go as far to say that the device is buggy since it does work with putty and I'll do some more digging to see if I can narrow down the setting. |
as I think my previous comment two weeks ago was missunderstood: Accessing com ports is not possible in the Creators Update but only in a later Insider Preview. Right? |
@MikeGitb, yes serial port support is only in the latest insider preview (post Creator's update). |
@stehufntdev - I duplicated your results. Note that you don't have to reboot: pulling the USB plug and then replacing it (and waiting until the CIRCUITPY drive shows up) will reset the port. I looked at the putty source code to see what it was setting, and I've found the change that will make serial_read2.exe work! You have to set Add the
So the serial code on the M0 is expecting that the host will assert DTR (Data Terminal Ready), and does not transmit until DTR is enabled.
So maybe this all has to do with the EDIT: I found out about the |
@stehufntdev: Thanks for the clarification (disappointing as it is) |
@MikeGitb - worry not! Windows is targeting a release twice a year so users on stable builds won't have to wait long. |
Thanks @dhalbert, I narrowed it down to the DTR bit as well and also discovered the mode command :). Here's a basic repro using your test: hang: no hang: As you suspected DTR is part of the ioctl support and isn't included in termios. I started working on the fix and will report back when it is finished. Thanks again for brining this to our attention! |
@stehufntdev, great! Are you going to try to emulate how serial ports come up on Linux? Or is that not really worth it? It does appear that they start with DTR (and maybe other things) asserted. Maybe one way to do that would be that the first time As we saw |
@dhalbert, to err on the safe side we need to rely on the Windows serial driver to set the correct defaults when the serial port is opened. I thought about adding initialization code in our layer, but there are so many different serial configurations that any defaults we picked would be the wrong ones. It looks like some versions of stty support configuring DTR, but I'm not seeing that option in the version included with Xenial. If this ends up causing a lot of issues we can take another look at a default configuration but I think those changes should really be in the serial driver. |
@dhalbert, I'm hitting another strange issue with the device. If I update your sample code to not call SetCommState, the device hangs even if I set everything up through Might be worthwhile to contact the company or search around to see if anyone else is having issues when reading from the device in code. I think supporting this in WSL might be tricky since the device seems to be very sensitive to the settings. |
@stehufntdev: Could you give the the The serial code used is the standard library supplied by Atmel for the microcontroller, incorporated into CircuitPython. It's all open-source and I'm helping on a volunteer basis to debug all this, so I'll look deeper. |
@dhalbert, yes I can still read fine from the device via putty. To follow-up on the experiment above, I updated your sample code to call SetCommState without changing the values from GetCommState (i.e. set to the current). After this, the device worked fine so the board seems to have some requirement that after opening the state has to be set. |
@dhalbert, I was able to narrow down the device hang some more. SetCommState calls EscapeCommFunction to set\clear RTS and DTR so the line matches the value passed to SetCommState. When the feather M0 is opened, the DTR line is clear and needs to be set to transfer data. To double check I updated the same code to call EscapeCommFunction with SETDTR instead of SetCommstate and it worked fine. SetCommState has a comment that this is required for Win95 compatibility :), but I don't know the history of that code. If you can update the serial code on the devices, my recommendation would be to have the device come up with the correct serial settings for transfer. Also, DTR doesn't seem to have first class support everywhere (e.g. Linux) but I imagine switching over to another mechanism may be costly. Going through the serial layer in WSL, I think we can support this device but it will likely be easier to configure from Windows first since the Linux tty tools I found don't have support for DTR. Here's a quick summary of the changes I will make: 1.) Match SetCommState's behavior of updating the RTS and DTR line to match the configuration. With some quick testing I was able to open the device with |
@stehufntdev, thanks for the update. I looked at the source code for this device and also did some other research. In general, the libraries for these kinds of devices (not just Atmel) use DTR and/or RTS to determine whether they should send output. If DTR is not asserted, they assume they are not connected. Changing this might break a bunch of assumptions, so I'm leery of that. On Linux, DTR appears to always be asserted when a USB CDC device is connected, at least for the generic CDC driver. On Windows, The best thing may be for me just to have a little utility to do the One thing I stumbled on about using
I don't know if this is really a bug or is by design, but you might need to watch out for this. Also mentioned here and here. |
@dhalbert, thanks all sounds reasonable to me. I tested with a few more serial devices and submitted the changes described above. This should work on a future insider build and the test program could use mode from cmd.exe or from WSL you can use the TIOCMSET\TIOCMBIS ioctls with TIOCM_DTR. Once the changes are out I'll update the blog post with the devices and some more examples. |
I am getting
I guess this is already been taken care for future release, if not then we need to make DTR control work not sure RTS works or not |
The fix hasn't made it out to insider builds yet, but I will update the tag on the thread when it does and the blog post to include the supported ioctls. |
Hi Everyone, |
@Raphdeumax - If your USB WiFi device is getting enumerated by Windows, conceptually it should work within bash. Is the WiFi device not showing up in |
HI @sunilmut and thanks for your response. |
@Raphdeumax - Is the interface showing up on Windows, in |
@stehufntdev & @dhalbert - I have been following this issue since I tried to work with my microPython device earlier this year through my WSL setup and stumbled upon this issue. I previously got to the "Connected" message after launching cu, but no in- or output was displayed. I just installed the FCU. After uninstalling the previous "Bash on Ubuntu on Windows" system, and installing Ubuntu WSL through the Windows Store I can confirm this issue has been resolved and in- as well as output to the usb serial device works perfectly! Well done! |
Thanks @TimV81. Closing this out. 16176 and FCU. |
Hello! @TimV81 , what does FCU mean in full? I also got connected message from "cu" command, but no input or output obtained fro mthe serial port in WSL. How to solve this issue? |
In an attempt to use the com ports from WSL, I encountered the following:
public@systemhost:/dev$ ls ttyS4 -l crw-r----- 1 root root 4, 68 Apr 14 21:23 ttyS4
public@systemhost:/dev$ cu /dev/ttyS4 cu: /dev/ttyS4: System not found
public@systemhost:/dev$ sudo cu /dev/ttyS4 cu: /dev/ttyS4: System not found
This is a USB to serial adapter. I've tried to open each of the ports present on my system, but no such luck.
The text was updated successfully, but these errors were encountered: