We have a scenario where multiple devices are connected to one com port. Each device has an access code, which is manually defined within the device.
Can I access all the connected devices with/by access code in C#?. Does anybody have any idea as to how to do this?
In my case multiple devices are connected with single port but slave addresses are different..how can i access all the slave address in C# code for restricting that another device shudnot have same slave address?
Take a look at an article - Serial post in C# and Tutorial
Jaswant,
Where I work, we have some devices that are accessed over RS232 via a .net forms app. We have multiple devices connected via the same serial line at the same time.
As long as the devices on the end of the cable:
a) respond when spoken to (i.e. directly addressed) - and at no other time
b) use connectionless serial communications (i.e. no handshaking)
then I doubt that you will have a problem doing this with your devices.
I'm assuming you will have to 'address' your device like we do, i.e. every block of data sent down the line is in a specific format (bit like a TCP header) e.g.
byte description
---- -----------
00 command
01 address
>=02 data
All devices receive all comms, but only act upon instructions and respond if the address in the data matches the address on the device.
Related
I'm developing a weather site, with several weather stations. Ever station uses a different method to post data to the server. They all work, except one. It's a brand new station, but uses a serial port. The company that installed the station connected the station to a Moxa Serial converter. Witch essentially sends the serial data over a specific TCP port to my remote server. Using Pccomm Terminal Emilator I can receive the data on my server. But how should I get my app to listen to this port for serial data. I tried Tcplistener, but it doesn't receive any data. Data is in NMEA format.
Thanks for the suggestions!
Without more specifics I can only give you some general hints, but here goes:
Many Serial-to-Ethernet converters include server-side software that will make the data appear on a virtual serial port. If that is the case, with the software installed you will see the virtual port listed along with the real ports in your system. You can simply open and read from that port as usual.
If that is not the case, but you are receiving data on a known TCP port and can view it using Telnet or something similar, then perhaps the Moxa converter is acting as a server and expects you to be the client. In that case you would want to use the TCPClient class rather than the TCPListener class.
The Moxa converter (you didn't give the model number) has a manual which may prove helpful.
If you need more help then you will need to post specifics about exactly how you are connecting with PComm and what you see, and exactly what you tried with TCPListener, and what happened.
gl
From my understanding I can only connect to a single service of a certain UUID. I looked in the properties and found that each device had it's own comport and guessed I had to be wrong. I tested multiple connections with 32feet.net and it turns out I can connect and communicate to both at the same time (and receive data).
I read Android: How do bluetooth UUIDs work? . Which mentions I need to the UUID to connect to a serial port service which I do and this is great (https://groups.google.com/forum/#!topic/android-developers/adeBD275u30) this link has the discussion.
I had been under the impression from reading various posts about not being able to connect to multiple headphones / keyboards / mice that bluetooth didn't allow you to connect to multiple services of the same UUID on Android / Iphone. Are these restrictions made by the OS / application?
After reading wikipedia / http://people.csail.mit.edu/rudolph/Teaching/Articles/PartOfBTBook.pdf
that seems to be the case.
Did I just assume this a long time ago and forget that it was assumed from something non concrete?
Device A can connect only one at a time to service S on Device B. Device A can connect to service S on Device B, C, D and E, etc at the same time.
In details a RFCOMM service listens on port number and publishes that port number in a SDP record listed by UUID. Unlike TCP/IP which uses source and destination port numbers in its packets RFCOMM only uses one port number in its packets and therefore only one connection can be active to that port number between a pair of devices.
I only briefly started working in C# and I'm currently using RS-485 to communicate with some other devices but I don't seem being able to actually get the other devices to respond.
I was wondering if there were any way to write to a specific device using serial communication since it's all connected through COM Port 4.
I already know serial.Write(); but as far as I know it doesn't give you the option to choose which address you wish to send to.
Is there anyone who might know a answar to this question?
Best Regards
Cvr
Thanks for the responses. They helped alot :)
Kristof is correct, but to elaborate a little more:
When communicating with RS232 you simply have two endpoints, your PC and external device. You communicate with the device by sending it commands, or it may even send them regardless. It may be simple ASCII text or binary/hex codes. The way it communicates between the two devices is known as the protocol - and your application must implement this protocol to be able to 'talk' to the device.
RS485 is different than RS232, in that you can daisy chain multiple devices on the same serial port that is connected to your PC. Depending on your device it will have its own protocol that it understands which you will need to study and become familiar. This should be supplied with the devices you are connecting to.
Typically, the protocol will have (at least) the following information:
Device Address - it uses this to distinguish which device you wish to talk to, usually can be set by hardware toggle switches or the like
Command - the actual command that you wish to send to the unit
Data - Any extra data you may need to pass for specific commands
So, an example command you might send to the unit will look like (note this is only an example):
$01FF9A
Where:
01 is the module or devices unique address
FF is the command type
9A is the data
So here, the module with device address 01 will read the command and deduce 'hey you are talking to me' and then process the command information. All other devices will also receive the data, but will realise that it is not destined for itself.
Usually RS485 devices communicate using Hex data, so your application will need to send hex commands to the external devices, and handle the conversion to from for any relevant responses etc. You may need to look at Serial.Write(byte[], int,int) to send hex data to the devices.
For a more detailed explanation of .NET serial port class, refer to http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx
You can create an instance of the SerialPort class.
There you can define baudrate, portname etc.
After calling the Open() method you can read or write data to or from the port.
var port = new SerialPort();
port.PortName = "COM4";
port.Open();
I am looking for something to test my app. The app is a weight logger, built in .net 3.5 - it is connected by rs232 to an electronic weight, but the problem is that I don't have it at home.
I wanted to emulate the traffic and data with my app, but I have a problem: I can't get 2 apps on 1 port.
Please help.
These are the tools you need:
com0com: Creates two virtual COM Ports which are connected to each other
Just connect your receiver application with the first one and your mock weight application with the second one.
Serial Port Monitor: If you need to sniff into some serial port connection to find out how another application communicates with some serial device.
Update
The storing and filtering is just to specific to your concrete project, so that you wouldn't find anything "out of the box". But you have two possibilities to store the data.
Within your application implement some kind of wrapper class for your serial port. Every read and write access to the serial connection goes through this class. Then this class sees everything and can dump the whole traffic into a file or somewhere else. Additionally you could also implement some kind of filter mechanism.
Instead of putting the dump and filter logic into your program, write it as an application. This application connects to two COM ports. The first is the real serial port. The other is a virtual from the com0com driver. Your application that normally communicates with your serial device will now be connected to the second com0com driver. So you're able to dump and filter anything that flows through your serial port.
I need to Determine the serial port name connected to other machine using c#.
This is just not the way serial ports work. It is not a bus, like USB or PCI, where you can plug something in and the operating system will do the ah-ha, new hardware! discovery automatically. Serial ports are very primitive, dating from the stone age of computer hardware.
It takes a human to plug a serial port device connector. With some luck, the connector will have a label which says what COM port number is assigned to the connector. Although that luck is hard to come by these days. She'll then tell a program to establish a connection on that particular COM port. Hyperterminal is the canonical implementation of such a program on Windows.
You cannot realistically open every COM port that might be available. That prevents another program from using another COM port. You'll prevent a modem from getting used for example. Part of the stone age legacy is that only one program can open a COM port, every other program will be locked out.
So, provide your program with a UI that lets the user select the COM port(s). Save the selection in your config data, it is very likely that the device is still connected to the same port when it starts back up. You can use WMI and the Win32_SerialPort class to provide a better description for the COM port (more than just the number). Some USB serial port emulators may set the Description property to something recognizable.
SerialPort.GetPortNames() enumerates the available COM port numbers. A basic sanity test is to check the SerialPort.DsrHolding property, it should be true when the serial port device is plugged in and powered-up.
A serial port doesn't report any connection state. You can open all available serial ports on your computer (if no other application already opened it) regardless if it is connected to something or not.
To find out if a serial port is connected to another machine, you have to open up all the available port, send your initialization data and listen if something correct comes back.
Imagine you have a good old serial modem connected to your pc. To find this out you have to open up all the available ports and send a 'AT' over the wire. If a 'OK' comes back you found a modem (maybe additional tasks are necessary to check if you found the right one [maybe there is more than one device connected to your pc]).
What i just missed out: Don't forget to configure the serial port! Don't set only baudrate and stop bits. Set all settings to the values you need (even if you use default settings). Cause these settings will be saved also if you close and reopen again. All settings are still valid unless you change them. Now imagine you have some other application on your pc that also opens up a serial port and changes the settings for some uncommon feature (e.g. XOnOff). If you don't set it back on your initialization phase
you'll never be able to get a working connection!
Update
Listening to all the available ports is quite easy:
First you need a list of all com ports.
Then you create for each one a own thread (or backgroundworker)
And each thread handles its given SerialPort
That's it.
Serial communication doesn't have anything compared to that of IP which has an address and port sent with every packet. The only data that is sent over a serial cable is the bytes you send yourself.
If you control both ends you can send the port number as a part of your own protocol.