Skip to content

Commit

Permalink
# Adapted data size (resolved TheUnknownOnes#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin committed Jan 18, 2021
1 parent b494247 commit 0ad30f7
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/s7_phphelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class s7_phphelper
* @param $strHex string with 32 bit Hex data
* @return float
*/
public static function hexToFloat32($strHex) {
public static function hexToFloat32($strHex)
{
$v = hexdec($strHex);
$x = ($v & ((1 << 23) - 1)) + (1 << 23) * ($v >> 31 | 1);
$exp = ($v >> 23 & 0xFF) - 127;
Expand All @@ -20,7 +21,8 @@ public static function hexToFloat32($strHex) {
* @param $float float to convert to 32 bit hex string
* @return string
*/
public static function float32ToHex($float) {
public static function float32ToHex($float)
{
return strrev(unpack('h*', pack('f', $float))[1]);
}

Expand All @@ -29,33 +31,37 @@ public static function float32ToHex($float) {
* @param $start
* @return int
*/
public static function getS7_Int($data, $start) {
return hexdec(bin2hex(substr($data,$start,4)));
public static function getS7_Int($data, $start)
{
return hexdec(bin2hex(substr($data, $start, 2)));
}

/**
* @param $data
* @param $start
* @return float|int
*/
public static function getS7_DInt($data, $start) {
return hexdec(bin2hex(substr($data,$start,8)));
public static function getS7_DInt($data, $start)
{
return hexdec(bin2hex(substr($data, $start, 4)));
}

/**
* @param $data string
* @param $start int
* @return float
*/
public static function getS7_Real($data, $start) {
return self::hexToFloat32(bin2hex(substr($data,$start,4)));
public static function getS7_Real($data, $start)
{
return self::hexToFloat32(bin2hex(substr($data, $start, 4)));
}

/**
* @param $data string to prepare
* @param $lenght int lenght for the send function
*/
public static function sendData_Prepare(&$data, &$lenght) {
public static function sendData_Prepare(&$data, &$lenght)
{
$data = "";
$lenght = 0;
}
Expand All @@ -65,7 +71,8 @@ public static function sendData_Prepare(&$data, &$lenght) {
* @param $lenght int lenght for the send function
* @param $float2add float
*/
public static function sendDataAdd_S7_Real(&$data, &$lenght, $float2add) {
public static function sendDataAdd_S7_Real(&$data, &$lenght, $float2add)
{
$data .= self::float32ToHex($float2add);
$lenght += 4;
}
Expand All @@ -75,7 +82,8 @@ public static function sendDataAdd_S7_Real(&$data, &$lenght, $float2add) {
* @param $lenght int lenght for the send function
* @param $number2add int
*/
public static function sendDataAdd_S7_Int(&$data, &$lenght, $number2add) {
public static function sendDataAdd_S7_Int(&$data, &$lenght, $number2add)
{
if ($number2add > (-32767 * -1) || $number2add < -32768) {
throw new Exception("Given number '$number2add' is out of range for a 16Bit Siemens S7 INT");
} else {
Expand All @@ -89,8 +97,9 @@ public static function sendDataAdd_S7_Int(&$data, &$lenght, $number2add) {
* @param $lenght int lenght for the send function
* @param $number2add int
*/
public static function sendDataAdd_S7_DInt(&$data, &$lenght, $number2add) {
public static function sendDataAdd_S7_DInt(&$data, &$lenght, $number2add)
{
$data .= str_pad(dechex($number2add), 8, '0', STR_PAD_LEFT);
$lenght += 4;
}
}
}

0 comments on commit 0ad30f7

Please sign in to comment.