Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Send raw string via socket with out any changes
#11
(Jul-24-2019, 11:05 AM)Gribouillis Wrote: What I see in the code is that you're sending the output of con_dat() through the socket
labeler_datasend = con_dat(aa_labeler_frame)
...
labeler_socket.send(labeler_datasend)
I don't understand what con_dat() does but I see that it returns the python type str and not a type bytes because there are statements such as rawconvertdata = '' (instead of = b'') or like rawhexstring = str(rawdata), etc. So the first thing to do seems to clean up this function so that it returns the binary string that the labeller is expecting.

con_dat - it will be another def, i don't have it yet


For example: its my string:
data = '\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\x4f\x52\x44\x45\x52\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x09\x31\x38\x39\x00\x64\x75\x70\x6b\x61\x5f\x69\x77\x6f\x6e\x6b\x69\x00\x31\x35\x39\x30\x30\x34\x37\x37\x30\x30\x32\x32\x38\x32\x38\x35\x38\x36\x00\x30\x35\x39\x30\x30\x34\x37\x37\x30\x30\x32\x36\x32\x38\x00\x38\x31\x00\x32\x30\x31\x32\x30\x30\x00\x34\x31\x30\x31\x38\x39\x00\x73\x6f\x63\x31\x31\x32\x30\x36\x31\x39\x00\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x00\xe7\xdc'

and my string in TCP frame must look like below:
İmage
Quote
#12
Again, you need a bytes type, so if you hard-code the string, dont use
data = '\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...'
but use
data = b'\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...'
Now I don't know exactly what the question is. If the labeler doesn't work with your data, you could prehaps print the data before you send it to the labeler and see if it's the correct data. For example use
print('SENDING:', repr(labeler_datasend))
labeler_socket.send(labeler_datasend)
Quote
#13
I solved my problem:
def rawbytes(s):
    """Convert a string to raw bytes without encoding"""
    outlist = []
    for cp in s:
        num = ord(cp)
        if num < 255:
            outlist.append(struct.pack('B', num))
        elif num < 65535:
            outlist.append(struct.pack('>H', num))
        else:
            b = (num & 0xFF0000) >> 16
            H = num & 0xFFFF
            outlist.append(struct.pack('>bH', b, H))
    return b''.join(outlist)
Quote
#14
I'm glad it works, but I still believe you're doing something that's not needed. First I don't see why you need to convert a string to bytes in the first place. If the labeler needs bytes, you should be able to generate this data without intermediary data of type str. Why would one use a string if a bytes is needed?
Quote
#15
(Jul-25-2019, 08:06 AM)Gribouillis Wrote: I'm glad it works, but I still believe you're doing something that's not needed. First I don't see why you need to convert a string to bytes in the first place. If the labeler needs bytes, you should be able to generate this data without intermediary data of type str. Why would one use a string if a bytes is needed?

I know, but i start write program and just later see problem string-->byte and know i haven't any time,

but thank You for Your help.
Quote
#16
data in the source code needs to be put in quotes. use a type of quote different than any quotes that are part of the data. make the string be bytes type by putting the b just before the starting quote. do not put r in front because that kind of raw will make your \x stuff not work.

for what you are doing you need binary. that is what the bytes type is. your use of the term "raw" for this is not the same meaning as python's meaning of "raw". you do not need python's raw. so don't even think of trying to use it. just use bytes. and bytes literal looks like b'this is bytes'.

are you using python version 3? you need to be. bytes are not real bytes in version 2.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#17
(Jul-27-2019, 12:53 AM)Skaperen Wrote: data in the source code needs to be put in quotes. use a type of quote different than any quotes that are part of the data. make the string be bytes type by putting the b just before the starting quote. do not put r in front because that kind of raw will make your \x stuff not work.

for what you are doing you need binary. that is what the bytes type is. your use of the term "raw" for this is not the same meaning as python's meaning of "raw". you do not need python's raw. so don't even think of trying to use it. just use bytes. and bytes literal looks like b'this is bytes'.

are you using python version 3? you need to be. bytes are not real bytes in version 2.
yes, python3.7.2
Quote
#18
good, version 3. so you started writing some parts before you realized the need to use bytes and the quick workaround is to convert strings to bytes. i've had a similar issue before and did the conversion like this:
  my_bytes = bytes([ord(x) for x in my_string])
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#19
(Jul-30-2019, 08:04 PM)Skaperen Wrote: good, version 3. so you started writing some parts before you realized the need to use bytes and the quick workaround is to convert strings to bytes. i've had a similar issue before and did the conversion like this:
  my_bytes = bytes([ord(x) for x in my_string])

It's working, Thank You!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Server and Network (socket) [WinError 10053] Timxxx 1 159 Aug-23-2019, 10:03 AM
Last Post: iMuny
  Send smtp message barry 1 118 Aug-12-2019, 03:47 PM
Last Post: barry
  socket.gaierror: [Errno 11001] Johnygo 3 152 Aug-07-2019, 02:49 PM
Last Post: Johnygo
  Soft Access Point & Socket Data Streaming groger57 1 143 Aug-01-2019, 02:53 PM
Last Post: groger57
  Question concerning function of a a socket pkm 4 1,144 Jun-04-2019, 07:48 AM
Last Post: DeaD_EyE
  socket.gaierror: [Errno -2] Name or service not known seokangwoo99 5 514 May-25-2019, 02:36 PM
Last Post: DeaD_EyE
  socket programming ConnectionRefusedError error srm 3 373 May-16-2019, 08:07 PM
Last Post: LavaCreeperKing
  Socket reading script Will86 4 416 Apr-17-2019, 06:57 AM
Last Post: Will86
  Server and Network (socket) [WinError 10053] SheeppOSU 2 594 Apr-13-2019, 09:23 PM
Last Post: SheeppOSU
  Send Pressure sensor data to IoT platform using WebSockets in Python barry76 3 519 Mar-12-2019, 09:48 AM
Last Post: barry76

Forum Jump:


Users browsing this thread: 1 Guest(s)