Peek data in Serial port windows - windows

How to know the data bytes availability at serail port in windows?
I mean, I just want to check whether serail port empty or not?
NOTE:
there was function in Linux "ioctl(fd,FIONREAD,&availableBytes)", I need to implement similar functionality in windows.
~ Johnnie

This solved my problem:
Call the ClearCommError function and check the return in lpStat->cbInQue.
& I think it should solve yours too.
Refer windows api documentation for more clarification on ClearCommError() function.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363180%28v=vs.85%29.aspx

In .Net the SerialPort class has a BytesToRead property.

ClearCommError is used to obtain error and stats info.
http://msdn.microsoft.com/en-us/library/aa363180%28v=vs.85%29.aspx

Related

How to check channel state from MAC layer

In Omnet++ 5.5.1 with INET 4 framework, from MAC layer, I am trying to check if the physical channel is idle.
Would anyone please suggest me the code for that?
UPDATE
I'm using the following interface.
# wireless interface
**.wlan[*].typename = "WirelessInterface"
**.wlan[*].radio.typename = "ApskScalarRadio"
Thanking you.
Usually radios implement the IRadio interface, and the medium state can be queried through various methods on that interface. Check the CsmaCaMac.cc file for examples. Here is the actual implementation you are looking for:
https://github.com/inet-framework/inet/blob/master/src/inet/linklayer/csmaca/CsmaCaMac.cc#L593

Windows - ways to communicate with device driver

As far as I can see - the only entrypoint to communicate with DeviceDriver - is using CreateFile.
Q1 - Is there any other way to communicate with device driver other than using CreateFile
Q2 - When using pseudo file access approach - what are common ways people use ReadFile and WriteFile? Only for passing data in-out or is there any other special purpose to use these?
Any link to appropriate article would be VERY appreciated.
These articles may be very useful for you
http://www.osronline.com/article.cfm?id=24
http://www.osronline.com/article.cfm?article=39
https://learn.microsoft.com/en-us/windows-hardware/drivers/wdf/working-with-usb-pipes
A1: Yes, there is few like Pipes but afair it uses CreateFileA
A2: This is not a pseudofile (kernel objects are not quite files, you can mainly read/write data from them), but read/writefile function are designed to pass data
If your driver is a minifilter driver (file system filter driver) working with filter manager you can use CommunicationPorts.
See FltCreateCommunicationPort.

How to get the status of a serial COM port

What I mean is that, when I code a project, I need to communicate with the serial port like COM1, COM2... but sometimes there is no device connected and I also can use the function CreateFile to get the COM port handle.
When I use the WriteFile function to send a string to the COM port the software blocks.
After I dig into the problem I find another function GetCommModemStatus which can get status of the COM port but when I use the usb-rs232 transition line, the second parameter always returns 0.
How can I get the COM port status so that I can check if is there some devices connected to the computer?
If I understand correctly, you want to detect if a device is connected to your COM port and ready to accept packets. If that's the case, you need to check control signals (DTR/DSR and CTS/RTS) before sending data, assuming your device is aware of them and sets the appropriate PIN on your DB-9 or DB-25 connector. Some devices rely on software handshaking instead (XON/XOFF) and do not set control signals. Your best bet would be to consult documentation of your device.
I have been using ComPort Library version 4.10 by Dejan Crnila. It does support both hardware and software handshakings, so you can focus on your own code instead of reinventing the wheel.
As several people have already pointed out, it is not a good idea to try to "re-invent the wheel." Except for "quick and dirty" testing, your code will have to handle the com port in a separate thread and the available solutions all make this much easier.
BUT, if you Really want to do it, I'll give you some pointers.
If you are using "WriteFile" then you have probably already figured out the "CreateFile" part of the procedure and how complicated things can get depends upon what kind of IO you specified in that procedure, Overlapped or not. Overlapped IO is more complicated but does let the OS handle some of the burden.
You mentioned that your call to "WriteFile" hangs. You should look up the "SetCommTimeOuts" function. Setting both the WriteTotalTimeoutMultiplier and WriteTotalTimeoutConstant members of TCommTimeouts to zero will tell the OS to return immediately. You may also need to "SetCommMask" if your target uses handshaking.
What happens next really depends on what your target is supposed to do. The GetCommMask function can be used to check the status of the handshake lines.

GetAddressByName and Windows 7

I've taken over maintenance of a Windows Service that was written many years ago.
My first task is to get the service running on Windows 7.
WinDbg shows that the service is erroring out on a call to GetAddressByName. (GetAddressByName returns 0 and the service shuts down.)
According to MSDN (https://msdn.microsoft.com/en-us/library/windows/desktop/ms738517%28v=vs.85%29.aspx), GetAddressByName is not available for use with Winsock v. 2.
Does the above mean that GetAddressByName will not work with Windows 7? (Does Winsock 1.x work on Windows 7?)
(Any recommendations for a function to use instead of GetAddressByName? )
I'm looking to see if anyone else has done this before.
Alternatively (GetAddressByName is supported on Win 7), any suggestions for debugging a call to GetAddressByName that's returning 0?
Best regards,
Mitch
GetAddressByName() is a legacy function from WinSock 1.x. It was removed in WinSock 2.0. Apps using WinSock 2.x should use getaddrinfo(), if not gethostbyname() (which is still available).
In any case, the documentation for GetAddressByName() says:
If the function succeeds, the return value is the number of CSADDR_INFO data structures written to the buffer pointed to by lpCsaddrBuffer.
If the function fails, the return value is SOCKET_ERROR( –1). To get extended error information, call GetLastError
Since GetAddressByName() is returning 0, it is succeeding but no addresses are being returned. Make sure the service code is handling that fact and not trying to access invalid buffer data.

How can I use sys_sendto() under kernel mode?

I'm writing a DDoS firewall by netfilter, I want to send a socket packet to another computer under kernel mode. But the compiler warned me that the symbol "sys_sendto" was undefined. So how can I use these sys_socketcalls in my module? Thank you for your help.
Instead of calling the sys_sendto system call, you might be able to use the sock_sendmsg function, which is how sys_sendto is implemented.

Resources