Click here to Skip to main content
15,314,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import logging
import socket
import random
from time import sleep
HOST = ""
PORT = 8750

def send_udp_command(command):
    COUNTER = "0s2a" + str(random.randint(10, 99))
    COMMAND = COUNTER + " " + command + " \r\n"
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.sendto( bytes(COMMAND, "utf-8"), (HOST, PORT))
    received = str(sock.recv(1024), "utf-8")

What I have tried:

Nothing, don't know python, don't know Tried googling for a translator but no joy.
Updated 7-Aug-20 19:57pm
Patrice T 7-Aug-20 21:04pm
Why not just run the code in Python ?
Member 14909491 7-Aug-20 22:13pm
Want to use it with home automation software that uses either C# or
Dave Kreskowiak 7-Aug-20 21:25pm
You're not going to find a python to VB.NET converter. The languages and runtime environments are vastly different.

If you don't know either language, you're going to have a very difficult time writing the new VB.NET code, and further yet, understanding it so you can debug and support it.
Member 14909491 7-Aug-20 22:13pm
I usually can get by with small scripts/code, etc. For example, I'm sure I could google and cobble together some of this like the random number, concentrating the string for the command, etc. Working with sockets, UDP, bytes, utf-8, etc. is a bit beyond my skills. This part would be a lot of trial and error to get the right command sent in the right format with newline characters, etc.
Dave Kreskowiak 7-Aug-20 23:03pm
Don't even think about this being a line-for-line conversion. .NET does things a bit different.
Richard MacCutchan 8-Aug-20 4:06am
"don't know python, don't know"
That is not a very good starting point. What ever it is you want this code to do, you would be better off paying someone to write it for you.

Step 1: Learn Python.
Step 2: Learn VB
Step 3: Use the Python code as a specification for a new VB app.

Never convert languages, it doesn't produce good code in the target language because it uses a totally different framework, and the data structures that make something work well in the source language are normally very different in the target language.

This is not a code conversion service: we are not here to provide a complete project you can just hand in. If you are short on time, then I'd strongly suggest that you start writing a new app quickly: you have the experience of writing this one, so you know what you need to do as far as algorithms, structures, and so forth are concerned - so it should take you a lot less time to produce you VB version than it did to produce that Python code.

Good luck!
Member 14909491 8-Aug-20 16:02pm
Wow, thanks. BTW, did you read the instructions for posting a solution...

When answering a question please:
1. Read the question carefully.
2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.

Let's work to help developers, not make them feel stupid.
Very roughly, and I say VERY, I doubt this would even compile, but you could do something like this (very raw, could be well improved).. oh, and its C#, not VB.Net, obviously

using System.Net.Sockets;
using System.Text;

// Needs HOST, PORT, command

Socket s = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp );
IPAddress ipAddress = IPAddress.Parse(HOST);

Random random = new Random();
int randomNumber = random.Next(10, 100); // NB : 100 NOT included so 10..99
String message = "0s2a" + randomNumber.ToString() + command + @"\r\n";

byte[] msg = Encoding.UTF8.GetBytes(message);
byte[] bytes = new byte[256];

  // Connect to server
  s.Connect( new IPEndPoint( ipAddress, PORT ) );

  // Send Message (Blocking) 
  int i = s.Send(msg);
  Console.WriteLine("Sent {0} bytes.", i);
  // Get reply from the server.
  i = s.Receive(bytes);
  Console.WriteLine("Received (0} bytes = {1}", i, Encoding.UTF8.GetString(bytes));
catch (SocketException e)
  Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900