Protocol communication help - c#

Using Visual Studio 2005
Language: c#
A new to c#
I have software, the software should communicate with gprs devices. So i want to write a protocol, TCP, UDP communication code.
Can any one give some idea for writing a source code and some sample code also.

When you say the software should communicate with GPRS devices,
I'm going to assume you mean they want to communicate with eachother using TCP/IP and the fact that the two endpoints are connected to eachother by GPRS modems should make little to no difference except for that fact that you have to keep in mind the limited bandwith and slower transfer speeds.
(Some extra work does have to be done when you are using GPRS modems which are connected to your system using RS232 or USB interfaces, I'm assuming GPRS modems with a direct RJ45 connector here)
Your first step is to make sure you understand the basics of TCP/IP and UDP/IP.
You did not mention what kind of data you want to exchange between the applications,
so that leaves us very little to go on. You need to decide what type of messages the
applications will exchange and you need to determine whether you'll use TCP/IP or UDP/IP
to exchange those messages. Long story short, you should only use UDP if you can afford to lose a few bytes along the way. (For example, it doesn't matter if you lose a few frames while streaming video).
Once you've decided what kind of messages the applications should exchange,
you need to determine how you will represent them. Olivier pointed out you should take
a look at ProtoBuf.Net, which is a .NET implementation of the Google Protocol Buffers.
You can find more information on the topic here: http://code.google.com/intl/nl-NL/apis/protocolbuffers/docs/overview.html
Basically it allows you to define messages like this:
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
Which, by means of ProtoBuf.Net, you can then use in your code.
The binary format of Google Protocol Buffers is compact enough to
be transmitted over GPRS connections.
I hope this gives you a few starting points,
first deside what you want, then look into the points we've mentioned
and feel free to post new (more specific) questions in the future

This is just a "Give me the code" question. I would say you should start your programming and ask questions about your concrete upcoming questions.
As starting point you should take a look into System.IO.Ports.SerialPort, System.Net.Sockets and Serialization (Binary, DataContract, ProtoBuf.Net)

Related

client-server data exchange format

We create client-server erp system with huge amount(in the future) of data and use c# for client and c for server. We started with xml for small requests/responses and it looks ok for now. But what is the best data exchange format for increasing amount of data per response(up to 100MB i think)?
P.S.
Highest priority is encode/decode speed.
We use Sockets to transfer data.
It really depends on what kind of data you are going to send to and receive back from the server. If “data” is some sort of a buffer with the known length and the usual operations are to put / get an object to / from the server, then I would recommend you to take a look at HTTP: it's a very simple protocol, there are many libraries and applications that support it, you can easily extend the protocol, add an encryption (HTTPS) and compression (gzip), and this protocol is easy to debug and work with.
If you want to send network packets that contain many data fields of different types, then you want to encode and decode such packet (serialize) before sending to the network. There are a plenty of open source libraries in the Internet which support both C and C# languages (you can even write your own implementation, it's not that hard). I would recommend you to take a look at XML / JSON (text-based standards for data exchange), you will find it much more easier to debug communication problem when working with a textural data.
Hope it helps !
I would suggest to take a look at JSON:
http://www.json.org/

How to fetch PLC S7-300 serial hexdata

We have Siemens PLC S7-300 with Ethernet communication port.
Which type of java or other language coding available for getting the frame data of plc.?
this PLC communicate only with step-7 software.
This frame structure data also can store in xml of some text file so we can continuous send it to internet.
Please help me
There are 3rd party libraries that allow that. At our company we are using Accon AGLink. It's a native library, but it also provides a .NET wrapper. The library is somewhat fast. You could also try this one (indi.an PLCCom) which is managed, and not so fast, but it has a free version.
Alternatively you could try setting up an OPC server and read the PLC data using an OPC client, but they are somewhat expensive.
Have a look at Snap7, which is written in C but with many wrappers and ports available, including Python, Node.js, .NET, C#, Arduino, and one in Java called Moka7, since you asked.
From the official documentation:
Snap7 is an open source, 32/64 bit, multi-platform Ethernet communication suite for interfacing natively with Siemens S7 PLCs. The
new CPUs 1200/1500, the old S7200, the small LOGO 0BA7/0BA8 and SINAMICS Drives are also partially supported.
I'm assuming that your setup consists on an S7-300 CPU with a CP 343 communication module. To read data from the CPU, the system must be running an S7 server (configurable via NetPro, inside SIMATIC Manager, as explained in the Siemens commissioning manual). The remote machine should be using the Snap7 client library.
The library includes functions to read different memory areas, such as the process inputs (I), process outputs (Q), merkers (M), timers (T), and counters (C), as well as general purpose data blocks (DBs). If the server is enabled, and the configuration is not restricted (for instance to a certain IP address) the client should have access by default to at least the I's, Q's and M's, as well as to some system properties (like the CPU model and the firmware version).
For your case in Java, have a look at the Moka7 documentation, which can be found in the same download package. There are several code examples to get started.
If successful, the ReadArea function will return hex data of the bytes you specified (i.e. the memory position to start reading from, and the number of bytes). As a remark, remember that most Siemens PLCs are big-endian, so you may have to make a conversion if you're reading memory areas that hold data in different types.

Clinical Device interface programming using ASTM protocol in .Net

How can we implement ASTM protocol in C# to read data from Clinical Device ?
Can any one suggest open source framework to write ASTM segments ?
I've been doing this since Mar of 2008 . I do not know one. Beside this not all of these diagnostic devices 100% compatible with ASTM 1394 . Especially package checksum calculation differs .
But I've infrastructures for Com port and TCP-IP level communications. Using USB directly is not commonly preferable because of the data-loss occurrences. Building an infrastructure for physical layer is logical. Because its a real standard but even for HL7 people use segments carelessly.In practice,this can happen; Lets put the patient insurance number to 11.segment ! But it could be 5 digits integer in standard. They don't care.
Have a look into this http://makecircuits.com/blog/2010-06-25-astm-protocol.html
But you should write on your own.
These guys are saying they had produced one which supports RS232, Bluetooth, HID, USB or network socket connection for ASTM
http://www.deepsoftware.com/nrcomm/astm.html It is not free and probobly won't help for the varity reasons I shared. It says HID supported. HID is Human Interface Driver for USB as you know, just a few diagnostic devices use USB directly. Most of them use USB to send data to their inner central stations and provide you over TCP/IP. But some significant devices support USB directly but not with HID , they had coded their own drivers. Such as Welch Allyn's Propaq LT,its a handy monitor for US army use.
All the best!
Be aware ASTM 1381 & 1394 protocol specs are now maintained by CLSI as LIS01-xx and LIS02-xx (https://clsi.org/standards/products/automation-and-informatics/documents/lis01/). If I am not mistaken there are nuget packages available for LIS01 & LIS02.
Don't think there is open source available.
But you might check http://www.deepsoftware.com/nrcomm/astm.html
the first step you need to understand ASTM, is that you have to work with Control Characters, , , these are not symbols, these are not string, these are not numbers.
so the first step in c# es convert these Control Characters
static string enq = char.ConvertFromUtf32(5);
static string ack = char.ConvertFromUtf32(6);

Transferring binary data using sockets?

I am thinking of making a server/client application that does this: the client(s) will connect to the server and the server will send back a list of folders (probably music or videos) that the client will be able to stream or download. I am going to make a GUI for this so that it won't be a text-only interface. I want it so that when the client connects to the server and gets the information about the media files that can be streamed/downloaded, the client will show the folders/files in an explorer-like interface (folders will show up with a folder icon, videos will show up with a video icon, etc.)
This seems really big to me but I really want to learn more about socket programming with C# and feel that getting my hands really dirty is the best way. As you can imagine, I have a lot of confusion about how to start this and what it is that I need to do to make this work.
The first part of my question is: If I want to let a client receive a list of files/folders and also have the ability to download/stream them, do I need to do something with transferring binary data using sockets or am I completely wrong on that? If I'm wrong, do I need to use something else like file readers and somehow use them over sockets?
The second part to my question is: when I transfer this information over to a client, how can I make the client show the appropriate icons for videos, folders, mp3s, etc.? I'm thinking that if I have to transfer binary data I will have to use that data to somehow populate the client GUI with the correct icons/data. I am stuck on exactly how to handle that. Are there certain methods/classes that I can use to parse that data and do what I want to do with it? Again, I don't know if I'm wrong about needing to transfer binary data but either way I am confused on what it is that's necessary to handle this.
I apologize if any of my terminology is wrong. I am obviously not an expert in C# yet ;)
Any time you are sending data between machines (or between processes), you are already talking binary data (you could argue that everything is binary data, but when working with it as an OO model it doesn't feel like it). The point here is that all you need to do is serialize your messages. There are a myriad of serialization frameworks - some aimed at convenience, some aimed to range-of-features, and som can use aimed at performance (minimum bandwidth etc).
Even the inbuilt ones would work - for example, you can use XmlSerializer etc to write to a Stream (such as the NetworkStream you typically use with a socket). But please - not BinaryFormatter (it will hurt you, honest). Personally I'd use something a bit less verbose such as protobuf-net, but I'm somewhat biased since I wrote it ;p
However, note that when working with sockets, you are typically sending multiple (separate) messages as part of the conversation. In order for each end to correctly and conveniently read the right data per-message from the stream, it is common to prefix each message with the length (number of bytes) - for example, as a little-endian int taking a fixed 4 bytes. So the reading process is:
read 4 bytes
interpret that as an int, let's say n
read n bytes
run that data through your deserializer, perhaps via MemoryStream
process the message
and the sending process might be:
serialize the message, perhaps to a MemoryStream
query the length and form the 4 byte prefix
write the prefix
write the data
You don't have to go to as low level as Socket, unless you just want to learn of course.
I suggest using WCF. It will completely meet your requirements, will be flexible and you will not have to deal with sockets directly.
WCF is a big framework, but you can quickly learn how to use it.
Introduction to WCF
Here you can find how to retrieve and show file icon in Windows.
If you client is hosted by Windows, you don't need to transfer icons, you can just retrieve image on the client by filename/extension.

Best API for modeling networked devices with arbitrary attributes to learn from?

I need to design a new API which models networked devices which have a large amount of attributes which vary quite a lot based on the device's type. The attribute set is not totally arbitrary though, it is a big set of known attributes. That said, with new devices come new attributes so the situation is never totally fixed.
The network devices themselves come and go all the time so that's a central part of the API design. Also, it would be preferable to get updates on the attributes/attribute sets via some variant of the Observer pattern.
Note: I'm not talking about network management, although this might sound like that. That said, the APIs on those systems might very well be suitable/worth looking at.
So my question is, do you know any good APIs out there in the Open Source world from which I could learn and derive some inspiration from?
The system will be Java-based so the examples would preferably be from close relative languages, e.g. Java (of course :)), C#, Scala and other similar statically typed languages.
I'm not sure what exactly your API is intended to do, but I'm guessing that since the network devices "come and go all the time" and that you wanted to use the observer pattern, you're looking for something that can get updates on the current state of stuff out on the network.
Have you looked at SLP? It's a pub/sub protocol that may do what you want: it lets network devices broadcast their presence and properties out over the network and also listen for other people. It works over TCP and UDP (unicast and multicast).
There's a couple of java implementations around (like jslp for example), but I was never entirely satisfied with them (so I ended up writing my own). As far as C# goes, I couldn't find one easily.
Not positive that I understand your question completely, but I'll give it a shot.
Are you looking for a way to allow a device to publicize its attributes when it is on a network? If so, there really isn't a good way unless you write code that understands now to interact with the device. After you have something like that, all you need to have is a class which holds attribute data in some type of data structure of your liking. For updating, you will probably need to poll the devices periodically unless they have a way of reporting when their attributes change:
Dictionary<string, Dictionary<string, string>> deviceAttributes = new
Dictionary<string, Dictionary<string, string>>();
void AddAttribute(string deviceIdentifier, string attribute, string value)
{
if(!deviceAttributes.Keys.Contains(deviceIdentifier))
deviceAttributes.Add(deviceIdentifier, new Dictionary(attribute, value);
else
deviceAttributes[deviceIdentifier].Add(attribute, value);
}
If each device has some kind of standard method for retrieving the attributes of interest, you can write code that would accept an array of whatever and add that to the data structure using a for or foreach loop. I can't really say more without more specific knowledge of what you are trying to do.
I might look at LDAP/ActiveDirectory/ADAM(User Mode Active Directory) . . .
TheEruditeTroglodyte

Categories

Resources