Related
I have a nested JSON object and i want to save the values into a txt file.
This is the nested Json object
"objectJSON":{
"alt":"136.22",
"lat":"46.7484",
"lng":"33.0685"
}
I use the following PHP code to save the values into a txt.file
<?php
header("Content-type: application/json");
$json = file_get_contents("php://input");
$obj = json_decode($json);
$decoded = base64_decode(json_encode($obj->data));
$encoded = json_encode($obj->objectJSON);
$fp = #fopen("TMP.txt", "a"); //Open and write into TMP.txt
fwrite($fp, $encoded);
fwrite($fp,"\r\n");
fclose($fp);
?>
And here are the values into the TMP.txt file
"{\"alt\":136.22,\"lat\":46.7484,\"lng\":33.0685}"
And here is the problem:
What do I have to change/add to save only the "alt" value in the TMP.txt file?
HERE ARE MORE INFORMATION ABOUT MY PROJECT.
I receive the data via a lora node to a lora gateway which send them to my lora server. Here is a part from the lora node code that has been uploaded on it:
void GPSWrite()
{
/*Convert GPS data to format*/
datastring1 +=dtostrf(flat, 0, 6, gps_lat);
datastring2 +=dtostrf(flon, 0, 6, gps_lon);
//datastring3 +=dtostrf(falt, 0, 2, gps_alt);
if(flon!=1000.000000)
{
strcat(gps_lon,",");
strcat(gps_lon,gps_lat);
//strcat(gps_lon,",");
//strcat(gps_lon,gps_alt);
int i = 0;
for(i = 0; i < 2; i++)
{
//datasend.toFloat();
atof(gps_lon);
//Serial.println((char*)datasend);
Serial.println("Testing converted data:");
Serial.println(gps_lon);
// atof(gps_alt);
// Serial.print(gps_alt);
}
strcpy(datasend,gps_lon); //the format of datasend is longtitude,latitude,altitude
Serial.print("########### ");
Serial.print("NO.");
Serial.print(count);
Serial.println(" ###########");
Serial.println("The longtitude and latitude are:");
Serial.print("[");
Serial.print((char*)datasend);
Serial.print("]");
Serial.print("");
/*
for(int k = 0; k < 20;k++)
{
Serial.print("[");
Serial.print(datasend[k], HEX);
Serial.print("]");
}
Serial.println("");
Serial.println("");*/
count++;
}
int32_t lat = flat * 10000;
int32_t lng = flon * 10000;
datasend[0] = lng;
datasend[1] = lng >> 8;
datasend[2] = lng >> 16;
datasend[3] = lat;
datasend[4] = lat >> 8;
datasend[5] = lat >> 16;
smartdelay(1000);
}
static void smartdelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
{
gps.encode(ss.read());
}
} while (millis() - start < ms);
}
void loop() {
os_runloop_once();
}
The application server decodes the data with the following function:
function Decode(fPort, bytes) {
var decoded = {};
var hexString=bin2HexStr(bytes);
return rakSensorDataDecode(hexString);
}
// convert array of bytes to hex string.
// e.g: 0188053797109D5900DC140802017A0768580673256D0267011D040214AF0371FFFFFFDDFC2E
function bin2HexStr(bytesArr) {
var str = "";
for(var i=0; i<bytesArr.length; i++) {
var tmp = (bytesArr[i] & 0xff).toString(16);
if(tmp.length == 1) {
tmp = "0" + tmp;
}
str += tmp;
}
return str;
}
// convert string to short integer
function parseShort(str, base) {
var n = parseInt(str, base);
return (n << 16) >> 16;
}
// convert string to triple bytes integer
function parseTriple(str, base) {
var n = parseInt(str, base);
return (n << 8) >> 8;
}
// decode Hex sensor string data to object
function rakSensorDataDecode(hexStr) {
var str = hexStr;
var myObj = {};
while (str.length > 4) {
var flag = parseInt(str.substring(0, 4), 16);
{
myObj.lat = (bytes[3] | bytes[4]<<8 | bytes[5]<<16 | (bytes[5] & 0x80 ? 0xFF<<24 : 0)) / 10000;
myObj.lng = (bytes[0] | bytes[1]<<8 | bytes[2]<<16 | (bytes[2] & 0x80 ? 0xFF<<24 : 0)) / 10000;
myObj.alt = ((bytes[6] << 8) + bytes[7])/100;
str = str.substring(22);
}
}
return myObj;
}
so I get through my application server the following results:
received data
Then I use the php script to save data on txt, file. As I have told you, the problem is that I can save all data but can't save for example only the altitude.
The only thing you have to do is decode and then encode and write the proper one:
$json = file_get_contents("php://input");
$obj = json_decode($json);
file_put_contents("TMP.txt", json_encode($obj->objectJSON->alt));
If you want to append then:
file_put_contents("TMP.txt",
file_get_contents("TMP.txt") . json_encode($obj->objectJSON->alt));
But that won't be valid JSON in the file.
You are using the encoded data, instead use decoded data if you want to access the 'alt' attribute, here is an example :
<?php
$obj = (object) [
"objectJSON" => [
'alt' => '136.22',
'lat' => '46.7484',
'lng' => '33.0685'
]
];
$encoded = json_encode($obj->objectJSON);
$decoded = json_decode($encoded);
$fp = #fopen("TMP.txt", "a"); //Open and write into TMP.txt
fwrite($fp, $decoded->alt);
fwrite($fp,"\r\n");
fclose($fp);
?>
You just need to extract the value from the object:
$fp = #fopen("TMP.txt", "a"); //Open and write into TMP.txt
fwrite($fp, $decoded->alt);
// ^^^^^^^
fwrite($fp,"\r\n");
fclose($fp);
Finally, I can save my data in a txt file and I can insert my data into a MySQL database at the same time. Thank you all for your help. Here is the PHP script
<?php
header("Content-type: application/json");
$json = file_get_contents("php://input");
$obj = json_decode($json);
$decoded = base64_decode(json_encode($obj->data));
$fp = #fopen("TMP.txt", "a"); //Open and write into TMP.txt
fwrite($fp,$decoded);
fwrite($fp,"\r\n");
fclose($fp);
$server = "#######";
$username = "#######";
$password = "#######";
$DB = "#######";
$conn = new mysqli($server, $username, $password, $DB);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$insert = "INSERT INTO gpssensor(longitude, latitude) VALUES ($decoded)";
$conn->query($insert);
$conn->close();
?>
I need help in calculating CRC X25 variation, i have a Java tool and also an online tool that give the correct conversion, but i haven't been able to reproduce it myself.
Online Tool
Github for online tool
String: 40402900033231334C323031373030313839360000000000009001FFFFFFFF0000D656B759
Input type: Hex -> Calc CRC16
Result: CRC-16/X-25
0x6C49 <-- this is the value im trying to get in php from that string above.
My Php code so far
private static $CRC16_Table = array
(0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD,
0x6536, 0x74BF, 0x8C48, 0x9DC1, 0xAF5A, 0xBED3,
0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, 0x1081, 0x0108,
0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64,
0xF9FF, 0xE876, 0x2102, 0x308B, 0x0210, 0x1399,
0x6726, 0x76AF, 0x4434, 0x55BD, 0xAD4A, 0xBCC3,
0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5,
0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E,
0x54B5, 0x453C, 0xBDCB, 0xAC42, 0x9ED9, 0x8F50,
0xFBEF, 0xEA66, 0xD8FD, 0xC974, 0x4204, 0x538D,
0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB,
0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1,
0xAB7A, 0xBAF3, 0x5285, 0x430C, 0x7197, 0x601E,
0x14A1, 0x0528, 0x37B3, 0x263A, 0xDECD, 0xCF44,
0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72,
0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB,
0x0630, 0x17B9, 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5,
0xA96A, 0xB8E3, 0x8A78, 0x9BF1, 0x7387, 0x620E,
0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738,
0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862,
0x9AF9, 0x8B70, 0x8408, 0x9581, 0xA71A, 0xB693,
0xC22C, 0xD3A5, 0xE13E, 0xF0B7, 0x0840, 0x19C9,
0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324,
0xF1BF, 0xE036, 0x18C1, 0x0948, 0x3BD3, 0x2A5A,
0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E, 0xA50A, 0xB483,
0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5,
0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF,
0x4C74, 0x5DFD, 0xB58B, 0xA402, 0x9699, 0x8710,
0xF3AF, 0xE226, 0xD0BD, 0xC134, 0x39C3, 0x284A,
0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C,
0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1,
0xA33A, 0xB2B3, 0x4A44, 0x5BCD, 0x6956, 0x78DF,
0x0C60, 0x1DE9, 0x2F72, 0x3EFB, 0xD68D, 0xC704,
0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232,
0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68,
0x3FF3, 0x2E7A, 0xE70E, 0xF687, 0xC41C, 0xD595,
0xA12A, 0xB0A3, 0x8238, 0x93B1, 0x6B46, 0x7ACF,
0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9,
0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022,
0x92B9, 0x8330, 0x7BC7, 0x6A4E, 0x58D5, 0x495C,
0x3DE3, 0x2C6A, 0x1EF1, 0x0F78,
);
public static function calculate($buffer) {
$length = strlen($buffer);
$crc = 0;
$i = 0;
while ($length--) {
$crc = (( $crc >> 8) & 0xff) ^ (self::$CRC16_Table[(ord($buffer[$i++]) ^ $crc) & 0xFF]);
}
return (($crc & 0xFFFF) ^ 0x8000) - 0x8000;
}
public static function ComputeCrc($data) {
$crc = 0xFFFF;
foreach ($data as $d) {
$crc = self::$CRC16_Table[($d ^ $crc) & 0xFF] ^ ($crc >> 8 & 0xFF);
}
$crc = $crc ^ 0xFFFF;
$crc = $crc & 0xFFFF;
return $crc;
}
This gives me results as follows
ComputeCrc(str_split("40402900033231334C323031373030313839360000000000009001FFFFFFFF0000D656B759", 2));
Result = ec71 <-- decHex result
calculate("40402900033231334C323031373030313839360000000000009001FFFFFFFF0000D656B759");
Result = 48d7; <-- decHex result
Any help will be very appreciated
Solved it applying hexdec before the calculation, may be useful in the future.
public static function ComputeCrc($data) {
$crc = 0xFFFF;
foreach ($data as $d) {
$d = hexdec($d); <-- This did the trick
$crc = self::$CRC16_Table[($d ^ $crc) & 0xFF] ^ ($crc >> 8 & 0xFF);
}
$crc = $crc ^ 0xFFFF;
$crc = $crc & 0xFFFF;
return $crc;
}
I am writing a program which needs different value of gas viscosity at different temperature for respective gases. Data which i have is given below. I am new in programming in php . can anyone please give me the logic behind it. so that i can get viscosity at any required tempertaure through the program. thanks
Data for viscosity calculation (dyn. visc. [Ns/m² = kg/ms]; Source: VDI Wärmeatlas
Temp. [°C] 0 100 200 300 400 500
CO2 1,37E-05 1,82E-05 2,22E-05 2,59E-05 2,93E-05 3,24E-05
O2 1,92E-05 2,43E-05 2,88E-05 3,29E-05 3,67E-05 4,03E-05
H2O 9,00E-06 1,25E-05 1,61E-05 1,97E-05 2,33E-05 2,69E-05
N2 1,66E-05 2,09E-05 2,47E-05 2,82E-05 3,14E-05 3,42E-05
You can easily fit your data in Excel. Here's a plot that shows the result, along with the 2nd order polynomial:
Finally I got the right answer. In case if anyone need it, I am posting it out here:
class baseViscosityCalc {
public $arViscosity = array(
array(0, 1.37e-05, 1.92e-05, 9.00e-06, 1.66e-05),
array(100, 1.82e-05, 2.43e-05, 1.25e-05, 2.09e-05),
array(200, 2.22e-05, 2.88e-05, 1.61e-05, 2.47e-05),
array(300, 2.59e-05, 3.29e-05, 1.97e-05, 2.82e-05),
array(400, 2.93e-05, 3.67e-05, 2.33e-05, 3.14e-05),
array(500, 3.24e-05, 4.03e-05, 2.69e-05, 3.42e-05)
);
public function getViscosityData($DCat) {
$arValue = array(
'Temperature' => 0.0,
'CO2' => 0.0,
'O2' => 0.0,
'H2O' => 0.0,
'N2' => 0.0
);
for ($i = 0; $i < count($this->arViscosity); $i++) {
$arValue['Temperature'] = $DCat;
if ($DCat < $this->arViscosity[0][0]) {
$arValue['CO2'] = $this->arViscosity[0][1];
$arValue['O2'] = $this->arViscosity[0][2];
$arValue['H2O'] = $this->arViscosity[0][3];
$arValue['N2'] = $this->arViscosity[0][4];
break;
}
if ($DCat > $this->arViscosity[count($this->arViscosity) - 1][0]) {
$arValue['CO2'] = $this->arViscosity[count($this->arViscosity) - 1][1];
$arValue['O2'] = $this->arViscosity[count($this->arViscosity) - 1][2];
$arValue['H2O'] = $this->arViscosity[count($this->arViscosity) - 1][3];
$arValue['N2'] = $this->arViscosity[count($this->arViscosity) - 1][4];
breaK;
}
if ($DCat > $this->arViscosity[$i][0] && $DCat < $this->arViscosity[$i + 1][0]) {
$factor = ($DCat - $this->arViscosity[$i][0]) / ($this->arViscosity[$i + 1][0] - $this->arViscosity[$i][0]);
$arValue['CO2'] = $factor * ($this->arViscosity[$i + 1][1] - $this->arViscosity[$i][1]) + $this->arViscosity[$i][1];
$arValue['O2'] = $factor * ($this->arViscosity[$i + 1][2] - $this->arViscosity[$i][2]) + $this->arViscosity[$i][2];
$arValue['H2O'] = $factor * ($this->arViscosity[$i + 1][3] - $this->arViscosity[$i][3]) + $this->arViscosity[$i][3];
$arValue['N2'] = $factor * ($this->arViscosity[$i + 1][4] - $this->arViscosity[$i][4]) + $this->arViscosity[$i][4];
break;
}
}
return $arValue;
}
}
I am having a lot of trouble on generating a modbus CRC16 code using PHP. I have found a lot of different codes over the internet but i have tried them and for some reason i didnt get right results. I have found a PHP code for generating CRC16-CCITT. I have chenge the look up table to the modbus CRC corresponding table but the result is still not the right one.
The code is bellow. What do i need to do more in order to transform a CRC16-CCITT code into CRC16-MODBUS code.
<?php
/*************************************************************************
* phpCrc16 v1.1 -- CRC16/CCITT implementation
*
* By Matteo Beccati <matteo#beccati.com>
*
* Original code by:
* Ashley Roll
* Digital Nemesis Pty Ltd
* www.digitalnemesis.com
* ash#digitalnemesis.com
*
* Test Vector: "123456789" (character string, no quotes)
* Generated CRC: 0x29B1
*
*************************************************************************/
/*
* Returns CRC16 of a string as int value
*/
function CRC16($str)
{
static $CRC16_Lookup = array(
0X0000, 0XC0C1, 0XC181, 0X0140, 0XC301, 0X03C0, 0X0280, 0XC241,
0XC601, 0X06C0, 0X0780, 0XC741, 0X0500, 0XC5C1, 0XC481, 0X0440,
0XCC01, 0X0CC0, 0X0D80, 0XCD41, 0X0F00, 0XCFC1, 0XCE81, 0X0E40,
0X0A00, 0XCAC1, 0XCB81, 0X0B40, 0XC901, 0X09C0, 0X0880, 0XC841,
0XD801, 0X18C0, 0X1980, 0XD941, 0X1B00, 0XDBC1, 0XDA81, 0X1A40,
0X1E00, 0XDEC1, 0XDF81, 0X1F40, 0XDD01, 0X1DC0, 0X1C80, 0XDC41,
0X1400, 0XD4C1, 0XD581, 0X1540, 0XD701, 0X17C0, 0X1680, 0XD641,
0XD201, 0X12C0, 0X1380, 0XD341, 0X1100, 0XD1C1, 0XD081, 0X1040,
0XF001, 0X30C0, 0X3180, 0XF141, 0X3300, 0XF3C1, 0XF281, 0X3240,
0X3600, 0XF6C1, 0XF781, 0X3740, 0XF501, 0X35C0, 0X3480, 0XF441,
0X3C00, 0XFCC1, 0XFD81, 0X3D40, 0XFF01, 0X3FC0, 0X3E80, 0XFE41,
0XFA01, 0X3AC0, 0X3B80, 0XFB41, 0X3900, 0XF9C1, 0XF881, 0X3840,
0X2800, 0XE8C1, 0XE981, 0X2940, 0XEB01, 0X2BC0, 0X2A80, 0XEA41,
0XEE01, 0X2EC0, 0X2F80, 0XEF41, 0X2D00, 0XEDC1, 0XEC81, 0X2C40,
0XE401, 0X24C0, 0X2580, 0XE541, 0X2700, 0XE7C1, 0XE681, 0X2640,
0X2200, 0XE2C1, 0XE381, 0X2340, 0XE101, 0X21C0, 0X2080, 0XE041,
0XA001, 0X60C0, 0X6180, 0XA141, 0X6300, 0XA3C1, 0XA281, 0X6240,
0X6600, 0XA6C1, 0XA781, 0X6740, 0XA501, 0X65C0, 0X6480, 0XA441,
0X6C00, 0XACC1, 0XAD81, 0X6D40, 0XAF01, 0X6FC0, 0X6E80, 0XAE41,
0XAA01, 0X6AC0, 0X6B80, 0XAB41, 0X6900, 0XA9C1, 0XA881, 0X6840,
0X7800, 0XB8C1, 0XB981, 0X7940, 0XBB01, 0X7BC0, 0X7A80, 0XBA41,
0XBE01, 0X7EC0, 0X7F80, 0XBF41, 0X7D00, 0XBDC1, 0XBC81, 0X7C40,
0XB401, 0X74C0, 0X7580, 0XB541, 0X7700, 0XB7C1, 0XB681, 0X7640,
0X7200, 0XB2C1, 0XB381, 0X7340, 0XB101, 0X71C0, 0X7080, 0XB041,
0X5000, 0X90C1, 0X9181, 0X5140, 0X9301, 0X53C0, 0X5280, 0X9241,
0X9601, 0X56C0, 0X5780, 0X9741, 0X5500, 0X95C1, 0X9481, 0X5440,
0X9C01, 0X5CC0, 0X5D80, 0X9D41, 0X5F00, 0X9FC1, 0X9E81, 0X5E40,
0X5A00, 0X9AC1, 0X9B81, 0X5B40, 0X9901, 0X59C0, 0X5880, 0X9841,
0X8801, 0X48C0, 0X4980, 0X8941, 0X4B00, 0X8BC1, 0X8A81, 0X4A40,
0X4E00, 0X8EC1, 0X8F81, 0X4F40, 0X8D01, 0X4DC0, 0X4C80, 0X8C41,
0X4400, 0X84C1, 0X8581, 0X4540, 0X8701, 0X47C0, 0X4680, 0X8641,
0X8201, 0X42C0, 0X4380, 0X8341, 0X4100, 0X81C1, 0X8081, 0X4040
);
$crc16 = 0xFFFF; // the CRC
$len = strlen($str);
for($i = 0; $i < $len; $i++ )
{
$t = ($crc16 >> 8) ^ ord($str[$i]); // High byte Xor Message Byte to get index
$crc16 = (($crc16 << 8) & 0xffff) ^ $CRC16_Lookup[$t]; // Update the CRC from table
}
// crc16 now contains the CRC value
return $crc16;
}
/*
* Returns CRC16 of a string as hexadecimal string
*/
function CRC16HexDigest($str)
{
return sprintf('%04X', crc16($str));
}
echo CRC16HexDigest('123456789');
?>
This Pascal snippet is MODBUS RTU (ASCII is different) protocol online calculation without any table:
function mb_CalcCRC16(ptr: pointer to byte; ByteCount: byte): word;
var
crc: word;
b, i, n: byte;
begin
crc := $FFFF;
for i := 0 to ByteCount do
if i = 0 then // device id is 1st byte in message, and it is not in the buffer
b := mb_GetMessageID; // so we have to calculate it and put it as 1st crc byte
else
b := ptr^;
Inc(ptr);
endif;
crc := crc xor word(b);
for n := 1 to 8 do
if (crc and 1) = 1 then
crc := (crc shr 1) xor $A001;
else
crc := crc shr 1;
endif;
endfor;
endfor;
Return(crc);
end;
function mb_CalcCRC: word; // Calculate CRC for message in mb_pdu
begin // this message can be one that is just received, or in a reply we have just composed
Return(mb_CalcCRC16(#mb_pdu[1], mb_GetEndOfData));
end;
That's a quote from a working embedded AVR device with implemented MODBUS RTU slave protocol.
This is faster than lookup table
function crc16($data)
{
$crc = 0xFFFF;
for ($i = 0; $i < strlen($data); $i++)
{
$crc ^=ord($data[$i]);
for ($j = 8; $j !=0; $j--)
{
if (($crc & 0x0001) !=0)
{
$crc >>= 1;
$crc ^= 0xA001;
}
else
$crc >>= 1;
}
}
return $crc;
}
years after, your answers are very usefull for me. I combine your answers in a non lookup table solution:
function crc16_modbus($msg)
{
$data = pack('H*',$msg);
$crc = 0xFFFF;
for ($i = 0; $i < strlen($data); $i++)
{
$crc ^=ord($data[$i]);
for ($j = 8; $j !=0; $j--)
{
if (($crc & 0x0001) !=0)
{
$crc >>= 1;
$crc ^= 0xA001;
}
else $crc >>= 1;
}
}
return sprintf('%04X', $crc);
}
The results I am waiting for are consistent with online tools like:
lammertbies
tahapaksu
Thans for your response. I was able to perfome the Modbus crc check in PHP with the following code:
<?php
//Calcula o CRC para uma mensagem modbus
function modbus_crc($modbus_msg){
$crctab16 = [0X0000, 0XC0C1, 0XC181, 0X0140, 0XC301, 0X03C0, 0X0280, 0XC241,
0XC601, 0X06C0, 0X0780, 0XC741, 0X0500, 0XC5C1, 0XC481, 0X0440,
0XCC01, 0X0CC0, 0X0D80, 0XCD41, 0X0F00, 0XCFC1, 0XCE81, 0X0E40,
0X0A00, 0XCAC1, 0XCB81, 0X0B40, 0XC901, 0X09C0, 0X0880, 0XC841,
0XD801, 0X18C0, 0X1980, 0XD941, 0X1B00, 0XDBC1, 0XDA81, 0X1A40,
0X1E00, 0XDEC1, 0XDF81, 0X1F40, 0XDD01, 0X1DC0, 0X1C80, 0XDC41,
0X1400, 0XD4C1, 0XD581, 0X1540, 0XD701, 0X17C0, 0X1680, 0XD641,
0XD201, 0X12C0, 0X1380, 0XD341, 0X1100, 0XD1C1, 0XD081, 0X1040,
0XF001, 0X30C0, 0X3180, 0XF141, 0X3300, 0XF3C1, 0XF281, 0X3240,
0X3600, 0XF6C1, 0XF781, 0X3740, 0XF501, 0X35C0, 0X3480, 0XF441,
0X3C00, 0XFCC1, 0XFD81, 0X3D40, 0XFF01, 0X3FC0, 0X3E80, 0XFE41,
0XFA01, 0X3AC0, 0X3B80, 0XFB41, 0X3900, 0XF9C1, 0XF881, 0X3840,
0X2800, 0XE8C1, 0XE981, 0X2940, 0XEB01, 0X2BC0, 0X2A80, 0XEA41,
0XEE01, 0X2EC0, 0X2F80, 0XEF41, 0X2D00, 0XEDC1, 0XEC81, 0X2C40,
0XE401, 0X24C0, 0X2580, 0XE541, 0X2700, 0XE7C1, 0XE681, 0X2640,
0X2200, 0XE2C1, 0XE381, 0X2340, 0XE101, 0X21C0, 0X2080, 0XE041,
0XA001, 0X60C0, 0X6180, 0XA141, 0X6300, 0XA3C1, 0XA281, 0X6240,
0X6600, 0XA6C1, 0XA781, 0X6740, 0XA501, 0X65C0, 0X6480, 0XA441,
0X6C00, 0XACC1, 0XAD81, 0X6D40, 0XAF01, 0X6FC0, 0X6E80, 0XAE41,
0XAA01, 0X6AC0, 0X6B80, 0XAB41, 0X6900, 0XA9C1, 0XA881, 0X6840,
0X7800, 0XB8C1, 0XB981, 0X7940, 0XBB01, 0X7BC0, 0X7A80, 0XBA41,
0XBE01, 0X7EC0, 0X7F80, 0XBF41, 0X7D00, 0XBDC1, 0XBC81, 0X7C40,
0XB401, 0X74C0, 0X7580, 0XB541, 0X7700, 0XB7C1, 0XB681, 0X7640,
0X7200, 0XB2C1, 0XB381, 0X7340, 0XB101, 0X71C0, 0X7080, 0XB041,
0X5000, 0X90C1, 0X9181, 0X5140, 0X9301, 0X53C0, 0X5280, 0X9241,
0X9601, 0X56C0, 0X5780, 0X9741, 0X5500, 0X95C1, 0X9481, 0X5440,
0X9C01, 0X5CC0, 0X5D80, 0X9D41, 0X5F00, 0X9FC1, 0X9E81, 0X5E40,
0X5A00, 0X9AC1, 0X9B81, 0X5B40, 0X9901, 0X59C0, 0X5880, 0X9841,
0X8801, 0X48C0, 0X4980, 0X8941, 0X4B00, 0X8BC1, 0X8A81, 0X4A40,
0X4E00, 0X8EC1, 0X8F81, 0X4F40, 0X8D01, 0X4DC0, 0X4C80, 0X8C41,
0X4400, 0X84C1, 0X8581, 0X4540, 0X8701, 0X47C0, 0X4680, 0X8641,
0X8201, 0X42C0, 0X4380, 0X8341, 0X4100, 0X81C1, 0X8081, 0X4040];
$hexdata = pack('H*',$modbus_msg);
$nLength = strlen($hexdata);
$fcs = 0xFFFF;
$pos = 0;
while($nLength > 0)
{
$fcs = ($fcs >> 8) ^ $crctab16[($fcs ^ ord($hexdata[$pos])) & 0xFF];
$nLength--;
$pos++;
}
$crc_semi_inverted = sprintf('%04X', $fcs);//modbus crc invert the hight and low bit so we need to put the last two letter in the begining
$crc_modbus = substr($crc_semi_inverted,2,2).substr($crc_semi_inverted,0,2);
return $crc_modbus;
}
?>
You should enter a string with a message in hexadecimal:
$modbus_msg='01040000002';
$modbus_msg_crc=modbus_crc($modbus_msg);
This is a compact version of CRC16-ANSI (IBM) algorithm used for ModBus RTU.
function crc16_ansi($data) {
$crc = 0xFFFF;
for ($i = 0; $i < strlen($data); $i++){
$crc ^= ord($data[$i]);
for($j = 8; $j--;)
$crc = $crc & 0x0001 ? $crc = ($crc >> 1) ^ 0xA001 : $crc >> 1;
}
return $crc;
}
I need help in converting CRC code written in Ojective C to PHP. The following is the Objective C code
static UInt16 CRC16_Table[] =
{ 0x0000, 0x2110, 0x4220, 0x6330, 0x8440, 0xa550, 0xc660, 0xe770,
0x0881, 0x2991, 0x4aa1, 0x6bb1, 0x8cc1, 0xadd1, 0xcee1, 0xeff1,
0x3112, 0x1002, 0x7332, 0x5222, 0xb552, 0x9442, 0xf772, 0xd662,
0x3993, 0x1883, 0x7bb3, 0x5aa3, 0xbdd3, 0x9cc3, 0xfff3, 0xdee3,
0x6224, 0x4334, 0x2004, 0x0114, 0xe664, 0xc774, 0xa444, 0x8554,
0x6aa5, 0x4bb5, 0x2885, 0x0995, 0xeee5, 0xcff5, 0xacc5, 0x8dd5,
0x5336, 0x7226, 0x1116, 0x3006, 0xd776, 0xf666, 0x9556, 0xb446,
0x5bb7, 0x7aa7, 0x1997, 0x3887, 0xdff7, 0xfee7, 0x9dd7, 0xbcc7,
0xc448, 0xe558, 0x8668, 0xa778, 0x4008, 0x6118, 0x0228, 0x2338,
0xccc9, 0xedd9, 0x8ee9, 0xaff9, 0x4889, 0x6999, 0x0aa9, 0x2bb9,
0xf55a, 0xd44a, 0xb77a, 0x966a, 0x711a, 0x500a, 0x333a, 0x122a,
0xfddb, 0xdccb, 0xbffb, 0x9eeb, 0x799b, 0x588b, 0x3bbb, 0x1aab,
0xa66c, 0x877c, 0xe44c, 0xc55c, 0x222c, 0x033c, 0x600c, 0x411c,
0xaeed, 0x8ffd, 0xeccd, 0xcddd, 0x2aad, 0x0bbd, 0x688d, 0x499d,
0x977e, 0xb66e, 0xd55e, 0xf44e, 0x133e, 0x322e, 0x511e, 0x700e,
0x9fff, 0xbeef, 0xdddf, 0xfccf, 0x1bbf, 0x3aaf, 0x599f, 0x788f,
0x8891, 0xa981, 0xcab1, 0xeba1, 0x0cd1, 0x2dc1, 0x4ef1, 0x6fe1,
0x8010, 0xa100, 0xc230, 0xe320, 0x0450, 0x2540, 0x4670, 0x6760,
0xb983, 0x9893, 0xfba3, 0xdab3, 0x3dc3, 0x1cd3, 0x7fe3, 0x5ef3,
0xb102, 0x9012, 0xf322, 0xd232, 0x3542, 0x1452, 0x7762, 0x5672,
0xeab5, 0xcba5, 0xa895, 0x8985, 0x6ef5, 0x4fe5, 0x2cd5, 0x0dc5,
0xe234, 0xc324, 0xa014, 0x8104, 0x6674, 0x4764, 0x2454, 0x0544,
0xdba7, 0xfab7, 0x9987, 0xb897, 0x5fe7, 0x7ef7, 0x1dc7, 0x3cd7,
0xd326, 0xf236, 0x9106, 0xb016, 0x5766, 0x7676, 0x1546, 0x3456,
0x4cd9, 0x6dc9, 0x0ef9, 0x2fe9, 0xc899, 0xe989, 0x8ab9, 0xaba9,
0x4458, 0x6548, 0x0678, 0x2768, 0xc018, 0xe108, 0x8238, 0xa328,
0x7dcb, 0x5cdb, 0x3feb, 0x1efb, 0xf98b, 0xd89b, 0xbbab, 0x9abb,
0x754a, 0x545a, 0x376a, 0x167a, 0xf10a, 0xd01a, 0xb32a, 0x923a,
0x2efd, 0x0fed, 0x6cdd, 0x4dcd, 0xaabd, 0x8bad, 0xe89d, 0xc98d,
0x267c, 0x076c, 0x645c, 0x454c, 0xa23c, 0x832c, 0xe01c, 0xc10c,
0x1fef, 0x3eff, 0x5dcf, 0x7cdf, 0x9baf, 0xbabf, 0xd98f, 0xf89f,
0x176e, 0x367e, 0x554e, 0x745e, 0x932e, 0xb23e, 0xd10e, 0xf01e
};
+(SInt16) CalculateCRC16 : (Byte*)buffer : (UInt16) length
{
UInt32 crc=0;
while (length--)
crc = ((crc>>8) & 0xff) ^ *(CRC16_Table + ((*buffer++ ^ crc) & 0xff));
return((SInt16)crc);
}
and the following is the code I've written so far in php
$CRC16_Table = array
( 0x0000, 0x2110, 0x4220, 0x6330, 0x8440, 0xa550, 0xc660, 0xe770,
0x0881, 0x2991, 0x4aa1, 0x6bb1, 0x8cc1, 0xadd1, 0xcee1, 0xeff1,
0x3112, 0x1002, 0x7332, 0x5222, 0xb552, 0x9442, 0xf772, 0xd662,
0x3993, 0x1883, 0x7bb3, 0x5aa3, 0xbdd3, 0x9cc3, 0xfff3, 0xdee3,
0x6224, 0x4334, 0x2004, 0x0114, 0xe664, 0xc774, 0xa444, 0x8554,
0x6aa5, 0x4bb5, 0x2885, 0x0995, 0xeee5, 0xcff5, 0xacc5, 0x8dd5,
0x5336, 0x7226, 0x1116, 0x3006, 0xd776, 0xf666, 0x9556, 0xb446,
0x5bb7, 0x7aa7, 0x1997, 0x3887, 0xdff7, 0xfee7, 0x9dd7, 0xbcc7,
0xc448, 0xe558, 0x8668, 0xa778, 0x4008, 0x6118, 0x0228, 0x2338,
0xccc9, 0xedd9, 0x8ee9, 0xaff9, 0x4889, 0x6999, 0x0aa9, 0x2bb9,
0xf55a, 0xd44a, 0xb77a, 0x966a, 0x711a, 0x500a, 0x333a, 0x122a,
0xfddb, 0xdccb, 0xbffb, 0x9eeb, 0x799b, 0x588b, 0x3bbb, 0x1aab,
0xa66c, 0x877c, 0xe44c, 0xc55c, 0x222c, 0x033c, 0x600c, 0x411c,
0xaeed, 0x8ffd, 0xeccd, 0xcddd, 0x2aad, 0x0bbd, 0x688d, 0x499d,
0x977e, 0xb66e, 0xd55e, 0xf44e, 0x133e, 0x322e, 0x511e, 0x700e,
0x9fff, 0xbeef, 0xdddf, 0xfccf, 0x1bbf, 0x3aaf, 0x599f, 0x788f,
0x8891, 0xa981, 0xcab1, 0xeba1, 0x0cd1, 0x2dc1, 0x4ef1, 0x6fe1,
0x8010, 0xa100, 0xc230, 0xe320, 0x0450, 0x2540, 0x4670, 0x6760,
0xb983, 0x9893, 0xfba3, 0xdab3, 0x3dc3, 0x1cd3, 0x7fe3, 0x5ef3,
0xb102, 0x9012, 0xf322, 0xd232, 0x3542, 0x1452, 0x7762, 0x5672,
0xeab5, 0xcba5, 0xa895, 0x8985, 0x6ef5, 0x4fe5, 0x2cd5, 0x0dc5,
0xe234, 0xc324, 0xa014, 0x8104, 0x6674, 0x4764, 0x2454, 0x0544,
0xdba7, 0xfab7, 0x9987, 0xb897, 0x5fe7, 0x7ef7, 0x1dc7, 0x3cd7,
0xd326, 0xf236, 0x9106, 0xb016, 0x5766, 0x7676, 0x1546, 0x3456,
0x4cd9, 0x6dc9, 0x0ef9, 0x2fe9, 0xc899, 0xe989, 0x8ab9, 0xaba9,
0x4458, 0x6548, 0x0678, 0x2768, 0xc018, 0xe108, 0x8238, 0xa328,
0x7dcb, 0x5cdb, 0x3feb, 0x1efb, 0xf98b, 0xd89b, 0xbbab, 0x9abb,
0x754a, 0x545a, 0x376a, 0x167a, 0xf10a, 0xd01a, 0xb32a, 0x923a,
0x2efd, 0x0fed, 0x6cdd, 0x4dcd, 0xaabd, 0x8bad, 0xe89d, 0xc98d,
0x267c, 0x076c, 0x645c, 0x454c, 0xa23c, 0x832c, 0xe01c, 0xc10c,
0x1fef, 0x3eff, 0x5dcf, 0x7cdf, 0x9baf, 0xbabf, 0xd98f, 0xf89f,
0x176e, 0x367e, 0x554e, 0x745e, 0x932e, 0xb23e, 0xd10e, 0xf01e
);
function CalculateCRC16($strMessage)
{
$hexdata = pack('H*',$strMessage);
$nLength = strlen($hexdata);
$crc = 0xFFFF;
$pos = 0;
while($nLength > 0)
{
$crc = ($crc >> 8) ^ $CRC16_Table[($crc ^ ord($hexdata[$pos])) & 0xFF];
$nLength--;
$pos++;
}
return ~$crc;
}
Where $strMessage contains packed values, but it does not give the correct result.
Any help will be highly appreciated.
This seems to work, tested with several values against this website for CRC-16-CCITT
Note that it uses PHP strings as direct byte buffers, because it's PHP's closest equivalent to byte[]
<?php
class CRC16 {
private static $CRC16_Table = array
( 0x0000, 0x2110, 0x4220, 0x6330, 0x8440, 0xa550, 0xc660, 0xe770,
0x0881, 0x2991, 0x4aa1, 0x6bb1, 0x8cc1, 0xadd1, 0xcee1, 0xeff1,
0x3112, 0x1002, 0x7332, 0x5222, 0xb552, 0x9442, 0xf772, 0xd662,
0x3993, 0x1883, 0x7bb3, 0x5aa3, 0xbdd3, 0x9cc3, 0xfff3, 0xdee3,
0x6224, 0x4334, 0x2004, 0x0114, 0xe664, 0xc774, 0xa444, 0x8554,
0x6aa5, 0x4bb5, 0x2885, 0x0995, 0xeee5, 0xcff5, 0xacc5, 0x8dd5,
0x5336, 0x7226, 0x1116, 0x3006, 0xd776, 0xf666, 0x9556, 0xb446,
0x5bb7, 0x7aa7, 0x1997, 0x3887, 0xdff7, 0xfee7, 0x9dd7, 0xbcc7,
0xc448, 0xe558, 0x8668, 0xa778, 0x4008, 0x6118, 0x0228, 0x2338,
0xccc9, 0xedd9, 0x8ee9, 0xaff9, 0x4889, 0x6999, 0x0aa9, 0x2bb9,
0xf55a, 0xd44a, 0xb77a, 0x966a, 0x711a, 0x500a, 0x333a, 0x122a,
0xfddb, 0xdccb, 0xbffb, 0x9eeb, 0x799b, 0x588b, 0x3bbb, 0x1aab,
0xa66c, 0x877c, 0xe44c, 0xc55c, 0x222c, 0x033c, 0x600c, 0x411c,
0xaeed, 0x8ffd, 0xeccd, 0xcddd, 0x2aad, 0x0bbd, 0x688d, 0x499d,
0x977e, 0xb66e, 0xd55e, 0xf44e, 0x133e, 0x322e, 0x511e, 0x700e,
0x9fff, 0xbeef, 0xdddf, 0xfccf, 0x1bbf, 0x3aaf, 0x599f, 0x788f,
0x8891, 0xa981, 0xcab1, 0xeba1, 0x0cd1, 0x2dc1, 0x4ef1, 0x6fe1,
0x8010, 0xa100, 0xc230, 0xe320, 0x0450, 0x2540, 0x4670, 0x6760,
0xb983, 0x9893, 0xfba3, 0xdab3, 0x3dc3, 0x1cd3, 0x7fe3, 0x5ef3,
0xb102, 0x9012, 0xf322, 0xd232, 0x3542, 0x1452, 0x7762, 0x5672,
0xeab5, 0xcba5, 0xa895, 0x8985, 0x6ef5, 0x4fe5, 0x2cd5, 0x0dc5,
0xe234, 0xc324, 0xa014, 0x8104, 0x6674, 0x4764, 0x2454, 0x0544,
0xdba7, 0xfab7, 0x9987, 0xb897, 0x5fe7, 0x7ef7, 0x1dc7, 0x3cd7,
0xd326, 0xf236, 0x9106, 0xb016, 0x5766, 0x7676, 0x1546, 0x3456,
0x4cd9, 0x6dc9, 0x0ef9, 0x2fe9, 0xc899, 0xe989, 0x8ab9, 0xaba9,
0x4458, 0x6548, 0x0678, 0x2768, 0xc018, 0xe108, 0x8238, 0xa328,
0x7dcb, 0x5cdb, 0x3feb, 0x1efb, 0xf98b, 0xd89b, 0xbbab, 0x9abb,
0x754a, 0x545a, 0x376a, 0x167a, 0xf10a, 0xd01a, 0xb32a, 0x923a,
0x2efd, 0x0fed, 0x6cdd, 0x4dcd, 0xaabd, 0x8bad, 0xe89d, 0xc98d,
0x267c, 0x076c, 0x645c, 0x454c, 0xa23c, 0x832c, 0xe01c, 0xc10c,
0x1fef, 0x3eff, 0x5dcf, 0x7cdf, 0x9baf, 0xbabf, 0xd98f, 0xf89f,
0x176e, 0x367e, 0x554e, 0x745e, 0x932e, 0xb23e, 0xd10e, 0xf01e
);
public static function calculate( $buffer ) {
$length = strlen($buffer);
$crc = 0;
$i = 0;
while( $length-- ) {
$crc = (( $crc >> 8) & 0xff) ^ (self::$CRC16_Table[(ord($buffer[$i++]) ^ $crc) & 0xFF]);
}
return (($crc & 0xFFFF) ^ 0x8000) - 0x8000;
}
}
echo CRC16::calculate( "\x74\x65\x73\x74" );
It is CRC-16 CCITT, may be it'll help you
<?php
function crc16($data)
{
$crc = 0xFFFF;
for ($i = 0; $i < strlen($data); $i++)
{
$x = (($crc >> 8) ^ ord($data[$i])) & 0xFF;
$x ^= $x >> 4;
$crc = (($crc << 8) ^ ($x << 12) ^ ($x << 5) ^ $x) & 0xFFFF;
}
return $crc;
}
?>