Accessing COM-port with Windows 10

白昼怎懂夜的黑 提交于 2019-12-24 02:57:19

问题


I have some stupid code to access a Windows COM-port (just some pseudo-code of the most relevant functions):

// open device:
COMMTIMEOUTS timeouts;
DCB          ComSettings;

memset(&ComSettings,0,sizeof(ComSettings));
ComSettings.DCBlength=sizeof(DCB);
*fd=CreateFile(serialParams>port,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
GetCommState(*fd,&ComSettings);
ComSettings.BaudRate=9600;
ComSettings.ByteSize=8;
ComSettings.Parity=NOPARITY;
ComSettings.fParity=0;
ComSettings.StopBits=ONESTOPBIT;
ComSettings.fOutX=FALSE;
ComSettings.fInX=FALSE;
ComSettings.fBinary=TRUE;
ComSettings.fDtrControl=DTR_CONTROL_DISABLE;
ComSettings.fRtsControl=RTS_CONTROL_DISABLE;
ComSettings.fAbortOnError=FALSE;

SetCommState(*fd,&ComSettings);

timeouts.ReadIntervalTimeout        =MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier =0;
timeouts.ReadTotalTimeoutConstant   =0;
timeouts.WriteTotalTimeoutMultiplier=0;
timeouts.WriteTotalTimeoutConstant  =250;

SetCommTimeouts(*fd, &timeouts);

Reading of data from serial port is done with

ReadFile()

and writing with

WriteFile()

In my particular case the device is a USB gadget which connects as serial interface and where speed-settings and other things do not really matter.

This code is working fine with all Windows variants including 8.1 but for Windows 10 some users report there is no communication possible with the device. Unfortunately I do not have the related Win10 version for testing.

So my question: are there some issues/important changes in Windows 10 which could cause such a behaviour or require some changes in serial port communications?

Thanks!

****** Update ******************************************************************

Just as update to this: function SetCommState() fails with an error 87 (ERROR_INVALID_PARAMETER). Here it does not matter if I set own parameters, if I use the data received from GetCommState() or if I completely initialise and configure the DCB-structure. So...is this a known bug in SetCommState() of Win 10?

Just to note: it fails only in 4 of 5 cases, so it is not a systematic error, but a random one!


回答1:


Finally: it seems it is a Windows problem, Microsoft has trashed the usbser.sys driver. There are a lot of threads regarding this problem out there:

https://answers.microsoft.com/en-us/windows/forum/windows_10-hardware-winpc/usb-serial-interface-problems-with-windows-10/562943cb-9a65-4900-98ef-03ba453d2742

https://answers.microsoft.com/en-us/insider/forum/insider_wintp-insider_devices/windows-10-serial-usb-problems/438de66f-7294-4c06-b4fb-89b45d005ca0

The second one is the most interesting one: it ends with "we still have the problem" - and then it is closed by MS staff.

I personally found an old Windows 10 installation where usbser.sys worked fine - and after applying all the available Windows patches (including an update of usbser.sys) the problem appeared.




回答2:


We also had problems with ReadFile on COM ports not returning anything while RealTerm worked on the very same ports. As it turned out Windows wasn't properly activated which caused those issues.



来源:https://stackoverflow.com/questions/42203808/accessing-com-port-with-windows-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!