Answer the question
In order to leave comments, you need to log in
Write the values of discrete channels to a binary file. C++?
There is an array of discrete channel values.
int discretCh = 6; // total 6 discrete channels
Values will always be either 0 or 1. They can generally only be 0000000 or 111111
int ChValue [6] = {000011}
the book describes how to write these values to a binary file. Example:
For a set of six status inputs (000011)
a) Write these status inputs as a binary number (110000).
b) Then pad the number out to a 16 bit number (0000 0000 0011 0000).
c) Translate this to a hexadecimal value (00 30).
d) The data is then stored in LSB/MSB format (30 00).
It is important that the size of the variable in which the value is written (30 00) depends on the number of channels.
You need to check
sizeVar = discretCh/16 round up
if (sizeVar <= 1) 2 bytes to store
if (sizeVar > 1 || sizeVar = 2 ) add 2 more bytes
if (sizeVar > 2 || sizeVar = 3 ) add 2 more bytes
I write all this to a file, the line should look like this:
05 00 00 00 9B 02 00 00 08 FD FA 04 48 00 3D 00 74 FF 0A FE 30 00
it is in the last 2 bytes that discrete values should be stored.
I understand that you need to write a sequence of values (000011) into an integer, I tried, but zeros are discarded (
Tell me, please! How to implement this? There are very few examples
Answer the question
In order to leave comments, you need to log in
Initializing an array like this:
You get in ChValue[0] = 9 - decimal 9 is octal 011. According to the rules of the language, integer numeric constants starting with 0 are octal. The remaining elements of the array will be 0.
You want to write six values into one two-byte variable (although 1 byte would be enough).
For this you need to use bitwise operations.
For example:
// ChValue - исходный массив значений по каждому каналу
int ChValue [6] = {0, 0, 0, 0, 1, 1};
uint16_t val = 0;
for(int i = 0; i < 6; ++i)
{
if(ChValue[i] == 1)
val |= 1 << i;
}
// в результате в val будет установлен в 1 бит с номером канала в котором в ChValue единица.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question