Reputation: 23
I'm working on a project where I need to receive data from a Teltonika FMB140 device via a SIM card on my server. I've managed to establish a connection between the device and my server using CodeIgniter 4, however, the data I receive contains garbage characters alongside the IMEI of the device. Here's a snippet of the data I'm receiving:
[Sample Data]
IMEI: 123456789012345
Timestamp: 0
Priority: 0
Longitude: 0
Latitude: 0
Satellites: 0
I want to extract meaningful information such as longitude, latitude, and satellites from the received data. However, the data seems to be in a binary format, and I'm struggling to parse it correctly.
This is the code that I have connected with FMB140 and I'm trying to get data from SIM card
$port = xxxxx;
// Create a TCP/IP socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("socket_create() failed: " . socket_strerror(socket_last_error()));
}
// Bind the socket to the address and port
if (!socket_bind($socket, 'xxxxxxxx', $port)) { // Replace 'your_server_ip' with your actual server IP
die("socket_bind() failed: " . socket_strerror(socket_last_error($socket)));
}
// Listen for incoming connections
if (!socket_listen($socket, 1)) { // Limit to 1 connection
die("socket_listen() failed: " . socket_strerror(socket_last_error($socket)));
}
// Accept incoming connection
if (($clientSocket = socket_accept($socket)) === false) {
die("socket_accept() failed: " . socket_strerror(socket_last_error($socket)));
}
// Set a timeout for the socket
socket_set_option($clientSocket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 2, 'usec' => 0]);
// Print connection information
$clientAddress = '';
$clientPort = '';
socket_getpeername($clientSocket, $clientAddress, $clientPort);
echo "Connected by $clientAddress:$clientPort\n";
// Receive data until connection closes or timeout occurs
while (true) {
$data = socket_read($clientSocket, 1024);
if ($data === false || strlen($data) === 0) {
break;
}
echo '<pre>';print_r($data);
// Extract IMEI
$imeiLength = hexdec(substr($data, 0, 4));
$imei = substr($data, 4, $imeiLength);
// Send acknowledgment response
$acknowledgment = "\x01"; // Acknowledge
socket_write($clientSocket, $acknowledgment, strlen($acknowledgment));
// Parse AVL Data
$timestamp = hexdec(substr($data, 4 + $imeiLength, 8));
$priority = hexdec(substr($data, 4 + $imeiLength + 8, 2));
$longitude = hexdec(substr($data, 4 + $imeiLength + 8 + 2, 8)); // Assuming 4 bytes for longitude
$latitude = hexdec(substr($data, 4 + $imeiLength + 8 + 2 + 8, 8)); // Assuming 4 bytes for latitude
$satellites = hexdec(substr($data, 4 + $imeiLength + 8 + 2 + 8 + 8, 2));
echo "IMEI: $imei\n";
echo "Timestamp: $timestamp\n";
echo "Priority: $priority\n";
echo "Longitude: $longitude\n";
echo "Latitude: $latitude\n";
echo "Satellites: $satellites\n";
}
// Close the client socket
socket_close($clientSocket);
// Close the server socket
socket_close($socket);
Those are some garbage characters that I am receiving: h��JvHj&�?���� �+����qcQRYo��C T�UZsH HIJKSWfH d2�i&|�k{� Ϥ�DLMOGN|��m���J�j&�?����
Could someone provide guidance on how to decode this binary data and extract relevant information such as longitude, latitude, and satellites using CodeIgniter 4?
Additionally, if there are any specific configurations or libraries required for handling Teltonika FMB140 data in CodeIgniter 4, I would greatly appreciate any recommendations.
Thank you in advance for your assistance.
What I expected to happen:
I expected to successfully decode the binary data received from the Teltonika FMB140 device and extract relevant information such as longitude, latitude, and satellites. I anticipated that the parsed data would be in a readable format and would accurately reflect the GPS coordinates and satellite information transmitted by the device.
What actually resulted:
Despite successfully establishing the socket connection and receiving data packets from the Teltonika device, I encountered difficulties in parsing the binary data. While I was able to extract the IMEI from the data packets, the subsequent information appeared to be garbled and unreadable, making it challenging to interpret the longitude, latitude, and satellite data accurately.
Upvotes: 0
Views: 227
Reputation: 148
You need to "unpack" your binary data, try replace this line:
$data = socket_read($clientSocket, 1024);
For this line:
$data = bin2hex(socket_read($clientSocket, 2048, PHP_BINARY_READ));
And then you can parse your hexadecimal data according to Teltonika Codec docs.
Btw, you also need to "pack" your response to binary when sending a message to device with hex2bin($data)
function.
You can check how I communicate whit my devices in CI4
Upvotes: 0