Send Byte command via socket to a controller in python?

1

Hello everyone, I'm trying to send commands to a Controller "Commbox", this controller has the purpose of opening and closing gates, traffic lights and other things. I am getting a command in Java but I need to do the same command in Python. Since I am a beginner in language, I am not succeeding. Here is the code in Java and Python:

package teste2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;


public class teste {

    public static void main(String[] args){
        Socket socket = new Socket();
        OutputStream out;
        InputStream in;



        try {
            socket.setSoTimeout(10000);
            socket.setTcpNoDelay(false);
            socket.connect(new InetSocketAddress("192.168.1.210", 4091), 1000);
            out = socket.getOutputStream();
            in = socket.getInputStream();
            byte[] b =  {-86, 85, -86, 85, 0, 0, 0, 1, 0, 0, 0, 18, 0, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 6, 20, 0, 0, 0, 1, 0, 0, 0, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            out.write(b);
            out.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Python:

import socket

sockt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    sockt.settimeout(10000)
    sockt.connect(('192.168.1.210', 4091))
    b = bytearray()
    lista = [42, 213, 42, 213, 128, 128, 128, 129, 128, 128, 128, 146, 128, 128, 128, 128, 127, 127, 127, 127, 128, 128, 128, 128, 128, 128, 134, 148, 128, 128, 128, 129, 128,
         128, 128, 132, 130, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128]

    for i in range(lista.__len__()):
        b.append(lista[i])

    print(b)

    sockt.send(b)
    resposta = sockt.recv(4091)
    print(resposta)

finally:
    exit()

Note: The command is different because I was not able to mount a negative byte array in python. By searching I saw that bytes in java range from -128 to 127 and Python from 0 to 256. So I tried to change the values.

    
asked by anonymous 17.02.2017 / 13:17

0 answers