Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
can some1 tell me How to read single bit from com port using madbus protocol? in C#
Posted
Updated 5-Jan-13 19:42pm
v2
Comments
Ravndra22 6-Jan-13 3:33am    
In the referance you provided input reads in the form of bytes. I want to read single bit i.e high or low

Have a look at this article Simple Modbus Protocol in C# / .NET 2.0[^]
 
Share this answer
 
In Modbus RTU, you always read a byte. Naturally, in Modbus ASCII, you read characters, 8-bit characters, and each byte is a part of number represented as text:
http://en.wikipedia.org/wiki/Modbus#Frame_format[^].

So, you either read individual byte or some number and extract an individual bit from the data read. All software developers know how to extract a bit, but… do you need help?

—SA
 
Share this answer
 
Comments
Ravndra22 7-Jan-13 0:42am    
thnx all...

but problem is that I want to read 4 inputs from serial port in the form of ON/OFF. So what request messge I should write to serial port through modbus in order to read inputs?

like Function Code=2,slave address=1 and other?
Sergey Alexandrovich Kryukov 7-Jan-13 0:47am    
You did not ask about request message. It depends on your device. The format should be known from the documentation. I answered your question, it should be enough. Are you going to accept it formally (green button)?
—SA
Quote:
thnx all...

but problem is that I want to read 4 inputs from serial port in the form of ON/OFF. So what request messge I should write to serial port through modbus in order to read inputs?


You probably need reading 4 consecutive discrete inputs (or coils) from the device.
Have a look at MODBUS functions 0x02 (read discrete inputs) or 0x01 (read coils) explanations and samples in this document "MODBUS APPLICATION PROTOCOL SPECIFICATION"[^].

If you need the CRC computation routine, you may find the C source code at the end of this document: Modicon Modbus Protocol Reference Guide[^].
 
Share this answer
 
thnx CPallini. It really helped me.
 
Share this answer
 
Comments
CPallini 8-Jan-13 6:34am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900