Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Serial Communication with multiple devices on one port

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 :)

like image 682
Cvr Avatar asked Apr 03 '12 09:04

Cvr


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

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

like image 123
Simon Avatar answered Oct 03 '22 07:10

Simon