Can you tell me please how can I establish/tear down an existing ethernet (LAN) connection using C#?
In Windows UI it's quite simple, you're clicking right mouse button on the connection and choosing "Connect"/"Disconnect" command from the context menu. Is there any programmatic analogue of that action? It seemed rather simple to me but eventually I've found nothing on this matter. I found how to check a connection status, how to enumerate all the network interfaces, how to dial a 3G modem connection, how to enable/disable a network card, how to monitor availability of the net, etc, anything but this! Generally I need a software switcher that will be able to turn on and off an ethernet connection (not a network card!).
Isn't there a simple method something like GetConnection(connectionName).Connect()?
Make some searches on TCP/IP Client/Server modeling in c#, you actually can start by some already existing sample codes like this
Ethernet doesn't have a concept of "connected" or "disconnected". It is really a multiple random access medium. Also, I'm not sure what UI element you are talking about. On my machine, I can "connect/disconnect" wireless adapters, but not Ethernet adapters. This is because most wireless protocols have an actual concept of being "connected" to some access point. If you are really talking about a wireless adapter, I'm sure there are windows API's that can control them, but I don't know off hand where to point you. Otherwise, I think you might need to take a step back and explain the larger context of what you want to accomplish.
Related
I want to auto detect devices connected to the COM ports of my computer. Being able to use the SerialPort class allows me to get the list of available COM ports easily.
I want to iterate through them and poll(send a command) to each port and wait for a response. However it seems that most tutorials suggested to use the DataReceived event. I am lost at how to do a serial send followed by waiting for xx amount of seconds till I receive a response from the device.
The DataReceived event is very nice when you need to talk to devices that can send something at a very unpredictable time. You don't have to burn up a thread that just blocks and waits for the device to send something.
But that's completely the opposite of what you're trying achieve, you do expect to receive something. So don't use DataReceived, use Read() with the ReadTimeout property set to a suitably low value. Now it is simple. Also consider using the DsrHolding property. It is true when there's a device attached to the port and it is powered-up.
I should note that doing this is rather dangerous. You have no idea what kind of devices are attached to the machine, it is rather tricky to send them something they were not designed to process. If whatever "are you there?" command you send just happens to mean to a robot controller "go home with 1G acceleration" then somebody is going to get hurt easily. You cannot do this in any kind of industrial setting.
By far the best way is to make it somebody else's responsibility to get this right. Add an option menu that lets the user set the port name. And settings, they matter a lot too and there's no way to auto-detect them. The option dialog could have a "help me find the device" button, now it is safe and useful.
I believe WMI have feature to query devices on COM, there is also Linq to WMI library that could be helpful
Hope this helps
I have been reading up on the web and cannot find any information on working with a RS485 MultiDrop connection in c#
To give a bit of insight. I have written an application to communicate with a Serial device using the MODBUS RTU protocol. Now the client has informed me there devices may be hooked up using multidrop communications links. Being a novice when it comes to working with serial devices I am a bit lost here.
My question is simply: Where do I start? a Google search has thus far only produced hardware converters and wikipedia entries for different Serial Communication standards.
Thanks!
RS485 is a standard that defines the electrical characteristics of a particular multi-drop networking arrangement. I once used it as the internal bus for an instrument - the main control board drove various pumps which were arranged on an RS485 network.
You can get half-duplex and full duplex arrangements (half means that one device can talk at a time - full means send and receive can happen at the same time).
Really using it is not a whole lot different from using an RS232 or serial port, and as you've seen you can get serial to RS485 converters. You can use the serial port drivers in C# to use it.
SerialPort Class
Your main problem is that RS485 doesn't really address how it should be used - its a fairly low level electrical spec, it doesn't define how you should use it to make communication happen.
The main issue you need to consider is how you're going to coordinate all this. With RS232 - there are two things connected, which makes it easy - usually a computer and some device. With RS485, there are many things connected. So you need some way of addressing each device. You don't give any details about the 'device' referred to here - but if they are intended to be connected on RS485 - then there will be a way of addressing them. There are however several ways this could work - so I can't help you on specifics without more detail. With the system I developed, all communication was initiated by the 'master' device (i.e. my control board - or your c# application for example) and each message sent had the receiver's address on it - so the right pump knew that the instruction was intended for it.
I hope this is of some help. Really its not that complicated, but you need to think about what these devices do, how they are addressed, and think about the messages that you need to send back and forth. You can use the C# SerialPort classes to actually do the work.
The book referred to in the other answer looks great by the way. I really would consider buying it if this is all new to you. It covers serial port communications, and has a chapter on RS485.
I have not worked with RS485 but this book can be a help in understanding Serial port and USBs.
here is another link which discuss specs and here is another. I dont think they will be readily implemented in C# but could be.
I'm rewriting a legacy app that sends pager messages via TAP and modems. So I'm having to write a new TAP client in C#. Using SerialPort is trivial but I'm not sure about how to handle conditions such as:
excessive ringing with no answer
no dialtone
busy signal detection (I understand that this isn't enabled in every modem so this is optional)
remote modem answers
Then also how do I read received data in a synchronous manner? TAP is synchronous so I will be issuing commands and then expecting some type of response from the remote machine. I think my biggest issue is figuring out how data will be terminated. Is it standard for modem data to be terminated by a CR or maybe CR-LF?
Some pointers would be great and any references that I could go read myself is wonderful too.
Is this a question regarding Serial Communication or the TAP? I've never had to work with TAP, but I have had to work with serial communication hardware (albeit quite a few years ago.)
I just found this PDF specification document and I searched for "terminat". It brings up 8 different words with that series of text, 7 of which appear to be in reference to your request.
I'm not sure of how much experience you have with serial communication or legacy communication systems & hardware but you may find HyperTerminal to be immensely useful and it is part of Windows. There is other serial/terminal software out there, but HyperTerm is free and simple to use.
You can use HyperTerm to send and receive codes to your devices and inspect the data that is sent/received. Then, you can use the communication dumps to reverse-engineer the TAP communication.
-- EDIT --
In regards to interacting with the modem, I'd research a bit about the Hayes command set. Hayes was a company that made modems in the early 80's. According to what I've read their modem commands have become pretty standard ever since then. Glancing at the available modem commands, I think there is a solution for your questions but you may need to do a bit of experimentation.
I'll be honest, I've never had to work directly with modem hardware, so the information that I am providing is based off of the research I've found this afternoon. These tips are what I would assume and attempt if I were in your shoes. For more information, google "Hayes Command Set" or check this article.
Hope that helps.
We are looking to design a security application that does the following on laptops:
If the ethernet adapter is used (cable plugged in) disable/block all other network connections (wireless WIFI, mobile broadband (PPP), virtual VPN adapters etc)
When ethernet adapter is not being used again, all connections allowed.
Does anyone have any good suggestion on how to accomplish this?
We have looked in the WMI a lot but there are no good ways of doing this. Only disabling the network connection is not secure enough because most mobile broadband applications try to re-establish the connection. This should be an application that works on all laptop vendors without any user interaction (such as choosing interfaces etc..).
So any suggestions on how to accomplish this would be much appreciated.
The simplest method for doing this is by disabling the adapter. You say this won't work for you, but I suspect it will. You can detect if something tries to re-enable it and act appropriately.
If that isn't going to work for you, then the next easiest thing to do is to remove the device itself. I believe you will need to used some unmanaged calls to get this done. There is some sample code on codeproject.com that will point you in the right direction.
Keep in mind, if the user runs a check for devices, it will show up again. You can monitor for DBT_DEVICEARRIVAL to detect when this happens, and again act appropriately.
You might also try simply disabling the device. Usually though I have seen that when you disable a network connection, this is exactly what it does. It might depend on the card and OS. I haven't experimented with it.
I suggest you reconsider simply disabling the network interface rather than going to the device level. It is a much cleaner way of doing this, and you can always detect if the interface comes back up. Anything else you do is going to be a bit hackish.
The only other method I can think of would be to block traffic using the Windows Firewall API. Just keep in mind that not all network traffic is over IP.
Well, this might not be the optimal solution but... You could use the route command to disable the interfaces ability to reach any network. It will probably require a lot of tweaking and constant monitoring of the routing table, but would effectively prevent the interfaces ability to communicate with any other device.
There are different ways of doing this. As stated by others in this question it needs to be done on a lower level than what the WMI allows. There are some C++ examples around that addresses this issue. Check out the library NETCONLib by Microsoft.
I am working on an application that needs to control another device.
This control should be using Wi-Fi.
How can this be done in C#?
Important to know that the other device I want to make it.
I can afford the part of making and design but how to make it connect to PC using Wi-Fi?
I don't know about it.
I just need a key to start searching or some thing similar.
Connecting over Wi-Fi could be as easy as opening a socket on the server, and another on the client, and start streaming data. Of course this if both devices are compatible and has Wi-Fi receivers. Just think of them as two computers connected with a wire, or without a wire they will just behave the same.
The connection protocol will care about doing the magic of converting what you write on the socket, into RF signals received from the other device and converted back to bytes.
But if you are building your own antenna/receiver/protocol ... then things will be much more complicated.
If you have to ask then you're probably going to want a single board computer. Popular choices are:
gumstix
Zii EGG
nwg100 by Atmel
(XBee) --not too sure about these, I haven't used.
You can install a network stack and everything on them.