modbus-data.c
Go to the documentation of this file.
1 /*
2  * Copyright © 2010-2011 Stéphane Raimbault <stephane.raimbault@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdlib.h>
20 #ifndef _MSC_VER
21 #include <stdint.h>
22 #else
23 #include "stdint.h"
24 #endif
25 #include <string.h>
26 #include <assert.h>
27 
28 /* Sets many bits from a single byte value (all 8 bits of the byte value are
29  set) */
30 void modbus_set_bits_from_byte(uint8_t *dest, int index, const uint8_t value)
31 {
32  int i;
33 
34  for (i=0; i<8; i++) {
35  dest[index+i] = (value & (1 << i)) ? 1 : 0;
36  }
37 }
38 
39 /* Sets many bits from a table of bytes (only the bits between index and
40  index + nb_bits are set) */
41 void modbus_set_bits_from_bytes(uint8_t *dest, int index, unsigned int nb_bits,
42  const uint8_t *tab_byte)
43 {
44  int i;
45  int shift = 0;
46 
47  for (i = index; i < index + nb_bits; i++) {
48  dest[i] = tab_byte[(i - index) / 8] & (1 << shift) ? 1 : 0;
49  /* gcc doesn't like: shift = (++shift) % 8; */
50  shift++;
51  shift %= 8;
52  }
53 }
54 
55 /* Gets the byte value from many bits.
56  To obtain a full byte, set nb_bits to 8. */
57 uint8_t modbus_get_byte_from_bits(const uint8_t *src, int index,
58  unsigned int nb_bits)
59 {
60  int i;
61  uint8_t value = 0;
62 
63  if (nb_bits > 8) {
64  /* Assert is ignored if NDEBUG is set */
65  assert(nb_bits < 8);
66  nb_bits = 8;
67  }
68 
69  for (i=0; i < nb_bits; i++) {
70  value |= (src[index+i] << i);
71  }
72 
73  return value;
74 }
75 
76 /* Get a float from 4 bytes in Modbus format */
77 float modbus_get_float(const uint16_t *src)
78 {
79  float f = 0.0f;
80  uint32_t i;
81 
82  i = (((uint32_t)src[1]) << 16) + src[0];
83  memcpy(&f, &i, sizeof(float));
84 
85  return f;
86 }
87 
88 /* Set a float to 4 bytes in Modbus format */
89 void modbus_set_float(float f, uint16_t *dest)
90 {
91  uint32_t i = 0;
92 
93  memcpy(&i, &f, sizeof(uint32_t));
94  dest[0] = (uint16_t)i;
95  dest[1] = (uint16_t)(i >> 16);
96 }
void modbus_set_float(float f, uint16_t *dest)
Definition: modbus-data.c:89
void modbus_set_bits_from_byte(uint8_t *dest, int index, const uint8_t value)
Definition: modbus-data.c:30
void modbus_set_bits_from_bytes(uint8_t *dest, int index, unsigned int nb_bits, const uint8_t *tab_byte)
Definition: modbus-data.c:41
float modbus_get_float(const uint16_t *src)
Definition: modbus-data.c:77
uint8_t modbus_get_byte_from_bits(const uint8_t *src, int index, unsigned int nb_bits)
Definition: modbus-data.c:57


libmodbus
Author(s):
autogenerated on Sat Nov 21 2020 03:17:32