Python How to create and run a python script on freebsd properly

Good day. I am not good at freebsd. I know it only a little. I need to create a program that reads some data from a usb device and writes it to files. Files will be in a folder available through FTP. It is not a problem to run FTP, there are pleny of tutorials on the internet. The problem is i have no clue how to run an app on freebsd completely. I know programming, i create some desctop apps on Windows on C++, it is not a problem to write an app in Python also, i just dont know how to import it in freebsd properly. For some reason internet sources didnt clear my vision on it. It seems i am not used "command line" operational systems.

Can i write and debugg a script on windows and then upload it to freebsd and run it? will it work ok?
Or should i create it in freebsd itself and run it? I just dont know how it would work without a IDE like in windows. What does services freebsd have to write and debugg scripts?

or both options are ok?

So as you can see, i am just a complete noob in this and just need guidance what to start from. For some reason i didnt find anything specific on the internet. It seems lile the question is too simple to explaine but for some reason i have trouble understanding this.
 
FreeBSD is not just a "command line" operating system. You can install any GUI that is available for *nix, like KDE or Gnome. I personally used to use XFCE.

Can i write and debugg a script on windows and then upload it to freebsd and run it? will it work ok?
You can write your python scripts anywhere you want and they'll work in FreeBSD, as well as in the rest of operating systems.

I just dont know how it would work without a IDE like in windows. What does services freebsd have to write and debugg scripts?
There are plenty of IDEs available in FreeBSD. I usually use Geany and Visual Studio Code, but other IDEs like NetBeans are also available.
 
There are plenty of IDEs available in FreeBSD. I usually use Geany and Visual Studio Code, but other IDEs like NetBeans are also available.
Or just use a text editor and learn more about how things are run, how to debug, etc. Instead of relying on an IDE that does all that stuff 'automagically'.

If you move Python scripts from one system to another you should only keep an eye on the Python version. Try to keep the same version everywhere. The default on FreeBSD at this time is 3.9, you should probably use that version on Windows too. Or else you might run into subtle issues. Once you're more familiar with Python those subtle differences won't matter that much, but when starting out they might add a whole bunch of additional confusion.
 
I am back. I wrote a program in Python on windows and started it in Freebsd but for some reason i get a different output. I have been searching for the source of issue for a long time but didnt find anything. Can anyone pleasw tell me where to dig? because for now i am stuck

Here is the script:

Code:
import serial
import struct

ser = serial.Serial(
    port='COM4',
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)

ser.isOpen()

packet = bytearray(b'\x7b\xff\x20\x00\x06\x00\x83\x04\x00\x00\xda\xfb\x04\x08\x0c\x00\xef\xf7')

print(packet)


ser.write(packet)

print("packet size = " + str(len(packet)))
print("sent packet")

raw_temp = bytearray()
raw_hum = bytearray()
raw_rad2 = bytearray()
raw_rad3 = bytearray()

what_we_got = bytearray()

exp_timer = 0;

for x in range(0, 70):
    c = ser.read()
    #print('{0:x}'.format(ord(c), 10), " ",)
    #print("reading, range - " + str(x))

    what_we_got += bytearray(c)

    if x in range(28, 32):
        raw_rad3 += bytearray(c)
    if x in range(32, 36):
        raw_temp += bytearray(c)
    if x in range(36, 40):
        raw_hum += bytearray(c)
    if x in range(56, 60):
        raw_rad2 += bytearray(c)
    if x == 64:
        exp_timer = ord(c);
    if x == 65:
        exp_timer = ord(c) * 256 + exp_timer;

print("packet_we_got: "+str(what_we_got))

print("temperature - " + str(struct.unpack('f', raw_temp)))

print("humidity - " + str(struct.unpack('f', raw_hum)))

print("radon current - " + str(struct.unpack('f', raw_rad2)))

print("radon max - " + str(struct.unpack('f', raw_rad3)))

it sends a bytearray to a device through com port. The device replies with bytes and some of them are number with floating point. Here is the output in windows:

Code:
bytearray(b'{\xff \x00\x06\x00\x83\x04\x00\x00\xda\xfb\x04\x08\x0c\x00\xef\xf7')
packet size = 18
sent packet
packet_we_got: bytearray(b'z\xff \x80:\x00\x83\x04\x00\x00\xa7{\x04\x08\x00\x000\x00\x00\x00\x02\x00a\x00D~\x0eH\x00\x00\x00\x00\x08\xa8\xc7AlHIB\xf0~0\xb8\x11\x00\x00\x00\x00\x00\x90A\xab\xa5\x1fA\xa3\xea\x87B\x00\x00\x00\x00F\x0c\x00\x00\x92#')
temperature - (24.957046508789062,)
humidity - (50.32072448730469,)
radon current - (67.9582748413086,)
radon max - (0.0,)

So i get temperature, humidity and radon. Numbers seems valid.

The i start this script on freebsd but only change the com port name to "/dev/ttyU0"
Here is the ouput in freebsd:

Code:
root@OST:~ # python /python_scripts/old_v.py
bytearray(b'{\xff \x00\x06\x00\x83\x04\x00\x00\xda\xfb\x04\x08\x0c\x00\xef\xf7')
packet size = 18
sent packet
packet_we_got: bytearray(b'F\x0c\x00\x00\x92#z\xff \x80:\x00\x83\x04\x00\x00\xa7{\x04\x08\x00\x000\x00\x00\x00\x02\x00a\x00D~\x0eH\x00\x00\x00\x00\x10[\xc7A\x05\xc7HB\xf0~0\xb8\x11\x00\x00\x00\x00\x00\x90A\xab\xa5\x1fA\xa3\xea\x87B\x00\x00\x00\x00')
temperature - (2.5848351472935576e-41,)
humidity - (4.053239664633446e+16,)
radon current - (-2.970819831728689e-16,)
radon max - (6.513266364189377e+37,)

Freebsd shows different numbers.
For some reason i have a different outcome. I use virtual box with freebsd 13.2-release.
On windows i use python 3.9. pip 23.2.1.
On freebsd i use 3.9.18. pip 23.3.2
 
i think the problem with ser.read() because it gives the wrong bytes. when i read bytes in windows, the device always return bytesarray starting from '7aff' but in freebsd it returns different numbers every time i try to read from it. Maybe someting is wrong with baud rate. is it because i use a virtual box to communicate with virtual com port?
 
Could be wrong baud rate. Could be some other hardware connection problem, although serial lines are usually pretty reliable and accurate over short distances.
 
Sorry. I managed to understand where the issue was. The point is when i connect my usb sensor to freebsd, the sensor somehow knows it is connected to a different OS and shifts the values to other positions in the buffer. So the temperature, humidity etc. are in different positions when it sends it to freebsd port. I know only a little about usb protocol so it seems it uses usb DESCRIPTOR or something. So everything works fine for now. Thank everyone for the help. Now i learned how to make programs in Freebsd
 
Back
Top