Hi
wie löst man am besten in python folgendes Problem:
ich muss über sockets/TCPIP/UDP comamndos an mein embedded device
schicken. Die cmds sind wie folgt aufgebaut:
cmd = bytearray([
0x01, 0x02, 0x04, #protocol header 3 byte
0x00,0x20, # version (Int16)
0x00, 0x0e, # cmd
0x02, my id
0x00, # Priority (Int8))]
Also ein struct in c aber in python?
Wie wurde ich am besten Bitflder dadrin definieren? in C wären es ja
#defines.
Danke
optimaler weg ein cmd aufzubauen
- __blackjack__
- User
- Beiträge: 14428
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
@klaus1234: In C wären das nicht wirklich ``struct``\s, jedenfalls nicht wenn das portabel sein soll, denn das Endianess, Padding und Bitfelder hängen vom Compiler und vom System ab.
Das `struct`-Modul hat diese Probleme nicht, jedenfalls nicht wenn man die Endianess explizit angibt. Bitfelder muss man sich selbst mit bitweisem Oder und Bitverschiebungen basteln. Falls es sich um Flags handelt kann `enum.IntFlag` aus der Standardbibliothek nützlich sein.
Das `struct`-Modul hat diese Probleme nicht, jedenfalls nicht wenn man die Endianess explizit angibt. Bitfelder muss man sich selbst mit bitweisem Oder und Bitverschiebungen basteln. Falls es sich um Flags handelt kann `enum.IntFlag` aus der Standardbibliothek nützlich sein.
“Design patterns are kind of like sarcasm: hard to use well, not always appropriate,
and disgustingly bad when applied to problems they are not meant to solve.” — ajones in c.l.python
and disgustingly bad when applied to problems they are not meant to solve.” — ajones in c.l.python
