How to supply data via COM3? - c++

I have been tasked with testing a serial comms application.
This application listens on COM3.
How can I supply data via COM3 to test the code.
I have tried to have another application that supplies the data but as I have discovered I can only have one application accessing COM3
What is the recommended way of doing this?

The most typical way is to connect some hardware to COM3 to communicate with.
If you are lacking such hardware or want to test what the application does when it gets garbage instead of what it is expecting there are applications which provide a "virtual serial port pair" which emulate both sides and you can connect your application to one port while using a second application to communicate via it's pair. The paired ports should behave as if they were physically connected by a wire.
Also if you need a specific port number because of how the application is written you can change port numbers by choosing the device in Device Manager going to Properties and than Advanced and changing COM Port Number. If it's already used by another device you have to change that first to have a different number.

Related

Sniff LPT Traffic in windows

I need to programmatically sniff data that is being passed through the parallel port (LPT) to printer in Windows XP.
This is what I have tried in the past 3-4 days:
Firstly, I tried to read data using windows CreateFile(), WaitCommEvent() and ReadFile() functions. But they seem to block the port, so that the port access is denied to other applications.
Secondly, I have used com0com to create virtual COM port to get the printer data and then send it to LPT. This requires me to make the source user space application send the data to COM port first and then I write my code to tunnel that data to printer through LPT port. So, I basically redirect the print stream through COM port rather than sniffing it over the LPT port. So, it requires changing the settings in the application whose print data I need to capture. Hence, this is more of a hack but not something I need.
Finally, I have used PortMon and PrintFil as mentioned in this question. They work fine but I need to include PortMon's sniff functionality in my application.
So, is there any open source library or code sample which could be used to sniff the parallel port data similar to what portmon or printfil do ?

Virtual COM port to Socket communication

I have a virtual COM port and socket in my app , i want to transfer data from this Virtual COM port to a socket and vice versa.
How can i do this?
Is there some sample code or good library out there to do so? Eventually this should work on Windows CE, but initially it should work on regular Windows.
It depends what you need the service to do.
Is it bi-directional, does opening the port have to automatically setup a new network link, do you need to set serial port parameters over the network?
If you only really need to remote a serial port and don't need any command and control data then most introductions to network programming start with some sort of chat server where everything typed at the client end goes to the server as text - it should be trivial to modify this so that the source text comes from a serial port

How to create a generic network proxy using Python or C++?

I have an application which communicates over the local area network. However, I want to instead make it communicate over the internet. To do this I propose making an intermediate program which will read the network traffic generated from the application on one computer and send it to the application on another computer.
This involves:
Reading the outgoing network traffic of the application
Sending a copy of this traffic over the internet to another computer
Giving this copy to the application on the other computer
Instead of this:
Application on computer A <-LAN-> Application on computer B
I want to achieve this:
Application on A <--> My Program on A <-INTERNET-> My program on B <--> Application on B
I can accomplish (2), but with (1) and (3) my problem is that I have very little experience with networking and I do not know where to start. I can program in python but would be willing to use c++ to accomplish this.
(Hamachi does not work for this application, I do not know why.)
In response to comments
I do not intend to manipulate any data unless it is necessary to make the connection work. I have no control over the application itself and it does not provide me with any methods to configure the connection with the exception of a port number.
TCP and UDP are both used on the port 6112. The IP addresses used are first 255.255.255.255 for a generic broadcast used to discover other applications on the LAN (with UDP), then a TCP connection is established.
The term you are missing in your original question is proxy. You specifically need a transparent forwarding proxy.
Here is a link to some source code in Python that will get you started with writing a proxy.
That said, if you search around you should be able to find a transparent forwarding proxy that you don't have to write yourself.
If you want to do this the most robust way, you can do it in hardware and setup a managed router/switch/firewall to route things to where ever you need without having to write anything.
Why re-engineer the wheel? Why not just use OpenVPN, n2n or vtun etc etc.

How to acess COM port of remote system?

I Want to access the COM port present in the remote system from system. Any help would be appreciable.
I am using windows XP in both remote as well as local system.
The com0com project, and especially the com2tcp application should help you.
In conjunction with the Null-modem
emulator (com0com) the com2tcp enables
to use a COM port based applications
to communicate with the TCP/IP based
applications. It also allows
communication with a remote serial
port via the TCP/IP.
Personally, I use SerProxy, which makes that com port looks like telnet:
Serproxy is a multi-threaded proxy program for redirecting network socket connections to/from serial links, in cases where the remote end of the serial link doesn't have a TCP/IP stack (eg an embedded or microcontroller system). The proxy allows other hosts on the network to communicate with the system on the remote end of the serial link.
I also looked into com0com before, but I finally decide not to use it, as it requires a driver installation. Where as serproxy just need to be run. Another nice part is that if the com port is not being "used" remotely, I can still access it locally.
I've used Advanced Virtual COM Port to share a COM port remotely.
On the local PC, it creates a virtual COM port that mirrors the activity of a real COM port on the remote PC. The remote PC can be on a local network or on the Internet. (If on the Internet, you just have to make sure your firewalls allow the particular TCP ports through.)
I tried several programs to share a COM port remotely, but this was the only one I found that also shared the serial hardware handshaking signals. So we picked this one, and it worked great. We used it about 3 years ago, to access a Japanese CDMA modem dev board, sitting in Japan, from Australia.

Determine if device is connected/disconnected to RS232 port without opening the port

I"m working on a C++ Win32 application for which I'm trying to essentially "auto detect" if a device has been plugged into one of the RS232 ports and then if it has been disconnected.
The checking for a connected device is easy enough because my use case allows me to assume that this particular thread will be the first to try to initiate communication with the port. I'm able to scan the available ports every minute or so and once I've found a port with the device on it I flag that port has having a device, close the port, then set an event so that the process that actually will use the device knows it can now connect on that port.
The disconnect detect is where I run into trouble. When I'm scanning for connected devices I can actually send data to the port to be sure that, if there is a device, it's the specific device I'm looking for. But once it's connected, that port will already be open by the other process and I can no longer open that port from the detect thread. So I am looking for a way to either open the port in a "listen mode" or something like that so I can just see if the device is still there.
I briefly came across something about watching the DSR or DTR line or something...but couldn't find any more or how to actually do it.
Any suggestions?
Edit: Looks like I need to clarify a little more... For detecting the disconnect, I cannot send data to the RS232 port in any way. Also, I cannot assume that another application actually has the port open. The device may be physically connected, but without and open connection...but I still can't risk sending data to it. I was hoping there was a way to just check that there was still power on that port or something like that.
It depends on the connected hardware whether there will be a change in the modem state registers when you disconnect the hardware, but if there is then you could check the state of for example the CTS or DSR line using the GetCommModemStatus() function.
There is however the problem that you need a file handle to the COM port to call any API function, and this is exclusive as the documentation of CreateFile() states:
The CreateFile function can create a handle to a communications resource, such as the serial port COM1. For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, the dwShareMode parameter must be zero (exclusive access)
So you can not open the COM port to watch the line state while another process has the port opened for communication.
There are ways to do this, but they involve a driver. SysInternals has the Portmon tool, and a Google search will turn up some companies selling software for sharing COM port access between applications, but AFAIK simultaneous access is impossible using the standard API.
Sounds like it might be a good idea to have that process that will give notification of connected and disconnected events also relay the data to the other process. Have your app work in layers such that there is a process that takes control of the RS232 connection and sends your upper layer app events: connected, disconnected, data available, etc.
I have done applications like this and its not really a language specific problem (unless you have no serial port access in your language).
My preferred solution has always been to have one thread per port, according to your configuration and the thread maintains a state which is accessible from some sort of controller.
The default condition is that the thread polls the port every few seconds and while there is no answer assume there is no device connected. Once a device seems to respond, change the state to indicate this is so.
I designed an application that had a number of queues: One with disconnected threads, one with connected, but idle threads and another with connected and busy threads. The controller moved threads between queues as they changed state.