chris227
chris227

Reputation: 607

Teltonika TST100 GPRS command not sending via TCP

i have a teltonika tcp server that parses data it works ok but i'm not able to send gprs command and i don't know why or what i do wrong. Here is from their website documentation:

First, the Teltonika device opens the GPRS session and sends AVL data to the server (refer device protocols). Once all records are sent and correct sent data array acknowledgment is received by device then GPRS commands in Hex can be sent to the device. The ACK (acknowledge of IMEI from server) is a one-byte constant 0x01. The acknowledgment of each data array send from the device is four bytes integer – a number of received records.

this is the getinfo hex example that should work 1'st example: Sending getinfo SMS command via GPRS Codec12

Server request in hexadecimal stream: 000000000000000F0C010500000007676574696E666F0100004312

here is my nodejs tcp server

const net = require('net');
const Parser = require('teltonika-parser-ex');
const binutils = require('binutils64');
const { parse } = require('path');


let server = net.createServer((c) => {
    console.log("client connected");
    c.on('end', () => {
        console.log("client disconnected");
    });

    c.on('data', (data) => {

   
        let buffer = data;
        console.log(buffer);
      
        let parser = new Parser(buffer);     

        if (parser.isImei) {
            c.write(Buffer.alloc(1, 1)); // send ACK for IMEI
        } else {


            let avl = parser.getAvl();
          
             console.log("parseRec", avl?.records?.map(({ gps, timestamp }) => {
                  return { gps, timestamp }
              }
              )
              )

            let writer = new binutils.BinaryWriter();
            writer.WriteInt32(avl.number_of_data);


            let response = writer.ByteBuffer;

           c.write(response); // send ACK for AVL DATA
            // console.log(test);
            
        c.write("000000000000000F0C010500000007676574696E666F0100004312"); // SEND GETINFO command that is not working
        }
 
    });

});

server.listen(1574, '0.0.0.0', () => {
    console.log("Server started");
});

the last c.write("000000000000000F0C010500000007676574696E666F0100004312"); should send the getinfo command and i should get an answer but i don't get nothing back.

Upvotes: 0

Views: 1723

Answers (2)

Aslam Shekh
Aslam Shekh

Reputation: 716

It would be best if you will send the HEX value of "no_of_avl_data". Check the full code below.

server.on("connection", (socket) => {

socket.setEncoding("hex");

socket.on("data", (data) => {   
        
    if (data.length == 34) {            
        let imeiAck = Buffer.from('01', 'hex');
        socket.write(imeiAck);  // send IMEI ack to device          
    } else {                    
    
        //parse data            
        let parsed = new ProtocolParser(data)
        const avlData = JSON.stringify(parsed.Content.AVL_Datas);  
        const avlDataParse = JSON.parse(avlData)
        var avlLength = avlDataParse.length;

        let numData = avlLength; // Replace with the actual number of data received
        let hexString = numData.toString(16).padStart(8, '0');
        let packetAck = Buffer.from(hexString, 'hex');

        socket.write(packetAck); // send packet ack to device back  
        
        console.log('play with data');
                            
    }
            
});
socket.on("close", () => {});
socket.on("error", (err) => {
    //console.log(err.message);
});
});

Upvotes: 4

chris227
chris227

Reputation: 607

i managed to make it work, i had to convert it to a buffer in hex. It seems i was sending a string. Here is what i had to modify in case someone else has my problem:

c.write(Buffer.from('000000000000000F0C010500000007676574696E666F0100004312', 'hex'));

Upvotes: 3

Related Questions