Posts: 14
Threads: 2
Joined: Jul 2019
Jul-21-2019, 08:18 PM
(This post was last modified: Jul-21-2019, 08:18 PM by john36.)
Hi,
I have problem with sending raw string data via socket. I made communication between PC(python script) and labeler Markem Imaje. Labeler has RS232 and converter Ethernet/RS232 and i have to send raw string data to converter with out any changes for example:
\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...
if I do something like this is working and labeler take mesage:
if labeler_connect_status:
try:
labeler_socket.send(b'\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...')
print('DATA_SEND')
except:
print('ERROR_SEND')
labeler_connect_status = False but I have many different message and i want put raw string to variable:
if labeler_connect_status:
variable = \x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...
try:
labeler_socket.send(b'variable')#I know it doesn't work, but I want something like that
print('DATA_SEND')
except:
print('ERROR_SEND')
labeler_connect_status = False I tried many options but it doesn't work because python try to encodes my data to UTF-8, ASCII...
for example when I want change string to bytes
\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\... python send:
b'\x01\xc2\x81\xc2\xaav\x00]D\x00\x00\x00\x00WG_
My question is, how can I send raw data string as bytes with out any changes?
PLS help
P.S. I tried make variable string with b' - variable = b'\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\' - but it didn't work
Posts: 4,529
Threads: 69
Joined: Jan 2018
Jul-21-2019, 08:28 PM
(This post was last modified: Jul-21-2019, 08:28 PM by Gribouillis.)
Why don't you write
variable = b'\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...'
labeler_socket.send(variable) What happens if you do this?
Posts: 14
Threads: 2
Joined: Jul 2019
Jul-21-2019, 08:40 PM
(This post was last modified: Jul-21-2019, 08:40 PM by john36.)
(Jul-21-2019, 08:26 PM)Gribouillis Wrote: Why don't you write
variable = b'\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...' What happens if you do this?
I tried:
raw_data = '\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\'
variable = "b'" + raw_data
and when i send this variable socket.send(variable) it did't work, when i tried socket.send(variable.encode()) it's worked but python send: \x01\xc2\x81\xc2\xaav\x00]D\x00\x00\x00\x00WG_
I tried just now
variable = b'\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...' and:
_test\convert_data_4.py "
b'\x01\x81\xaav\x00]D\x00\x00\x00\x00WG_'
Posts: 4,529
Threads: 69
Joined: Jan 2018
You don't seem to understand the meaning of the b which is a purely syntactic indicator to the python compiler. Why do you write
raw_data = '\x01\x81\xaa\x76\...' instead of
raw_data = b'\x01\x81\xaa\x76\...'
Posts: 14
Threads: 2
Joined: Jul 2019
(Jul-21-2019, 08:28 PM)Gribouillis Wrote: Why don't you write
variable = b'\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...'
labeler_socket.send(variable) What happens if you do this?
thanks for Your help, i will try Your advise in Tuesday, because now I'm in home and this labeler machine is far away.
Posts: 14
Threads: 2
Joined: Jul 2019
(Jul-21-2019, 08:55 PM)john36 Wrote: (Jul-21-2019, 08:28 PM)Gribouillis Wrote: Why don't you write
variable = b'\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...'
labeler_socket.send(variable) What happens if you do this?
thanks for Your help, i will try Your advise in Tuesday, because now I'm in home and this labeler machine is far away.
ok, it's working . I watched before only how see it python, not what he's send via socket.
Posts: 14
Threads: 2
Joined: Jul 2019
Jul-24-2019, 09:08 AM
(This post was last modified: Jul-24-2019, 09:29 AM by john36.)
Sry but you don't understand my question:
I have def routine, this routine return different string for example variable = '\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...'
And in the next step i want sent this variable as binary data I cant do how did you write variable = b'\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...' becose i didn't know yet how will be look like this variable, I will be have many different data \xab\xcd\xef\xgh....
(Jul-23-2019, 06:30 AM)john36 Wrote: (Jul-21-2019, 08:55 PM)john36 Wrote: thanks for Your help, i will try Your advise in Tuesday, because now I'm in home and this labeler machine is far away.
ok, it's working . I watched before only how see it python, not what he's send via socket. I think, I too quickly found the problem solved
I need to making my received variables to binary data and then send. But my sequence looks like below:
1. I get raw data from data base,
2. I transform this raw db data to string db_data = '\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...'
3. I want transform this db_data string to binary without encoding(and any different change) --> binary_data_ready_to_sent
4. In last step I want sent this via socket
Posts: 4,529
Threads: 69
Joined: Jan 2018
Jul-24-2019, 09:50 AM
(This post was last modified: Jul-24-2019, 09:52 AM by Gribouillis.)
john36 Wrote:becose i didn't know yet how will be look like this variable, I will be have many different data \xab\xcd\xef\xgh... I understand that you can't hard code the variable in your program because its content will vary in time. However, this contents must be coming from somewhere, either read in a file or created by some other python function etc. Can you send more complete code that shows how you want to generate the data before sending it throw the socket?
The important thing that you need to understand is that there are two types of strings in python: unicode strings, which type is str, and which is mainly used to transmit human readable messages, and byte strings, which type is bytes used for binary data and obviously this is what the labeller is expecting. Besides these two types, there is a syntactic difference to allow you to hard code both types of strings in code and this is the 'b', so when you write
foo = 'foo'
bar = b'bar' the value of foo has type str (unicode string, for humans) and the value of bar has type bytes (binary data, for machines).
Posts: 14
Threads: 2
Joined: Jul 2019
Jul-24-2019, 10:59 AM
(This post was last modified: Jul-24-2019, 10:59 AM by john36.)
(Jul-24-2019, 09:50 AM)Gribouillis Wrote: john36 Wrote:becose i didn't know yet how will be look like this variable, I will be have many different data \xab\xcd\xef\xgh... I understand that you can't hard code the variable in your program because its content will vary in time. However, this contents must be coming from somewhere, either read in a file or created by some other python function etc. Can you send more complete code that shows how you want to generate the data before sending it throw the socket?
The important thing that you need to understand is that there are two types of strings in python: unicode strings, which type is str, and which is mainly used to transmit human readable messages, and byte strings, which type is bytes used for binary data and obviously this is what the labeller is expecting. Besides these two types, there is a syntactic difference to allow you to hard code both types of strings in code and this is the 'b', so when you write
foo = 'foo'
bar = b'bar' the value of foo has type str (unicode string, for humans) and the value of bar has type bytes (binary data, for machines).
honestly, it's my first steps in python, Im PLC Programmer but want do something more:
I try run the production line where I have MS Database, PLC Siemens 1500 and Labeller, to connect this in one system i chose python.
I have 3 main python program, one is for MS DB and it's finish and working well, the another is for change data between PLC Siemens and PC(i'm steel working) and the last one is for change data between labeler and PC:
Here code (code the last one main program) (but is under construction), I tring small piece of code and then put too program.
import socket
import binascii
import operator
import time
#INITIALIZATION OF VARIABLE
labeler_connect_status = False
pc_connect_status = False
#*************************************************ROUTINES*********************************************
#ascii to bytes
def a2b(data):
binarydata = binascii.a2b_hex(data)
return binarydata
#bytes to ascii
def b2a(data):
hexdata = bytes.hex(data)
return hexdata
#string to hex
def s2h(data):
hexdata = []
for i in data:
hexdata.append((hex(ord(i)))[2:])
rawhexstring = str(hexdata)
for char in rawhexstring:
if char == '[':
hexstring =rawhexstring.replace(char,'')
rawhexstring = hexstring
for char in hexstring:
if char == ']':
hexstring = rawhexstring.replace(char,'')
rawhexstring = hexstring
for char in hexstring:
if char == "'":
hexstring = rawhexstring.replace(char,'')
rawhexstring = hexstring
for char in hexstring:
if char == ',':
hexstring = rawhexstring.replace(char,'')
rawhexstring = hexstring
for char in hexstring:
if char == ' ':
hexstring = rawhexstring.replace(char,'')
return hexstring
#sub command
def sub_comm(line_nr):
auto_print = '0'
auto_acknowledge = '1'
not_used = '0'
bbe_offset = '0'
allocation_lock = '0'
if line_nr == 0 or not line_nr:
line_selection = '000'
if line_nr == 1:
line_selection = '100'
if line_nr == 2:
line_selection = '010'
if line_nr == 3:
line_selection = '110'
if line_nr == 4:
line_selection = '001'
if line_nr == 5:
line_selection = '101'
if line_nr == 6:
line_selection = '011'
if line_nr == 7:
line_selection = '111'
sub_command_binary = auto_print + auto_acknowledge + not_used + bbe_offset + allocation_lock + line_selection
sub_command = hex(int(sub_command_binary, 2))[2:]
return sub_command
#product code
def prod_cod(laber_name):
laber_name_hex = s2h(laber_name)
laber_name_lenght = len(laber_name_hex)
how_many_zeros_is_missing = 44 - laber_name_lenght
product_code = laber_name_hex + how_many_zeros_is_missing * '0'
return product_code
#crc data
def dat_check(data):
sum = 0xffff
crc_gen = 0xA001
data_lenght = len(data)
i=0
hexintdata = []
while i < data_lenght:
j = i + 2
hexintdata.append(hex(int(data[i:j], 16)))
i += 2
hexidata_lenght = len(hexintdata)
i=0
for i in range(hexidata_lenght):
j = int(hexintdata[i], 16)
sum = sum ^ j
for k in range(8):
if ((sum&1) == 0):
sum = sum >> 1
else:
sum = (sum >> 1) ^ crc_gen
str_sum_normal = str(hex(sum)[2:])
str_sum_inverse = str_sum_normal[2:] + str_sum_normal[:2]
data_checksum = str_sum_inverse
return data_checksum
#data lenght
def dat_len(data_measurement):
number_of_bytes = (len(data_measurement) // 2)
hex_number_of_bytes = hex(number_of_bytes)
hex_number_of_bytes_without_0x = hex_number_of_bytes[2:]
hex_lenght = len(hex_number_of_bytes_without_0x)
how_many_zeros_is_missing = 4 - hex_lenght
hex_number_of_bytes_without_0x = '0' * how_many_zeros_is_missing + hex_number_of_bytes_without_0x
number_of_bytes_inverse = hex_number_of_bytes_without_0x[2:] + hex_number_of_bytes_without_0x[:2]
data_lenght = number_of_bytes_inverse
return data_lenght
#header checksum
def head_check(frame_header):
frame_header_lenght = len(frame_header)
i=0
hexintdata = []
while i < frame_header_lenght:
j = i + 2
hexintdata.append(hex(int(frame_header[i:j], 16)))
i += 2
sum = 0
for j in hexintdata:
k = int(j, 16)
sum = sum + k
ffvalue = 0xff
checksum = ffvalue - sum
checksum_decoded = hex((checksum + (1<<32))%(1<<32))
header_checksum = checksum_decoded[-2:]
return header_checksum
#string to byte
def con_dat(data):
data_lenght = len(data)
i=0
d=r'\x'
rawdata = []
while i < data_lenght:
rawdata.append(d)
rawdata.append(data[i:(i+2)])
i += 2
rawhexstring = str(rawdata)
print(rawdata)
print(rawhexstring)
for char in rawhexstring:
if char == '[':
hexstring =rawhexstring.replace(char,'')
rawhexstring = hexstring
for char in hexstring:
if char == ']':
hexstring = rawhexstring.replace(char,'')
rawhexstring = hexstring
for char in hexstring:
if char == "'":
hexstring = rawhexstring.replace(char,'')
rawhexstring = hexstring
for char in hexstring:
if char == ',':
hexstring = rawhexstring.replace(char,'')
rawhexstring = hexstring
for char in hexstring:
if char == ',':
hexstring = rawhexstring.replace(char,'')
rawhexstring = hexstring
for char in hexstring:
if char == ' ':
hexstring = rawhexstring.replace(char,'')
convert_data = str(hexstring)
data_lenght = len(convert_data)
rawconvertdata = ''
rawconvertdata2 = ''
i=1
while i < data_lenght:
rawconvertdata = rawconvertdata2 + (convert_data[i:(i+4)])
rawconvertdata2 = rawconvertdata
i += 5
convert_data = rawconvertdata
return convert_data
#******************************************************************************************************
#######################################################################################################
#************************************************PROGRAM LOOP******************************************
while True:
#------------------------------------------connect to labeler
if not labeler_connect_status:
try:
address_labeler = "192.168.0.214"
port_labeler = 4001
labeler_socket = socket.socket()
labeler_socket.connect((address_labeler, port_labeler))
labeler_connect_status = True
except:
print('ERROR_LOG_LABELER')
labeler_connect_status = False
#------------------------------------------connect to PC
if not pc_connect_status:
try:
address_pc = socket.gethostname()
port_pc = 7000
pc_socket = socket.socket()
pc_socket.connect((address_pc, port_pc))
server_connect_status = True
except:
print('ERROR_LOG_PC')
pc_connect_status = False
#------------------------------------------send data to PC
if pc_connect_status:
#read data from pc
if pc_connect_status:
try:
pc_datarecive = pc_socket.recv(1024)
except:
print('ERROR_LOG')
#send data to pc
if pc_connect_status:
try:
pc_socket.send(pc_datasend)
except:
print('ERROR_LOG')
pc_connect_status = False
#------------------------------------------data to labeler
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\the frame
#PRoduct select AA
#01\81\aa\Lenght LSB\Lenght MSB\Header chechsum\Sub Command\Allocations\Product Code\Number of fileds\Data1\data...n\Data chechsum\
aa_labeler_frame = ''
aa_header = '0181aa'#ready to send
aa_sub_command = sub_comm(1)#1 is mean Line selection bit 0 is True
aa_allocations = '00000000'#ready to send - but need to check -2 (feffffff) or -1 (ffffffff),You cannot send a new product select until the allocation is complete UNLESS you resend a product
#select with an allocation set to –1 or –2. (No information after the allocation number needs to be included
#in the ‘Product Select’ message, as it will be ignored).
labeler_name = 'WG_ORDER' #labeler name willbe taken form db
aa_product_code = prod_cod(labeler_name)#the product code will be taken from database
aa_nr_of_fields = nr_of_fiel(labeler_name)#the nr will be chosen depending on lebeler name
aa_data = lab_dat(db_data)#db_data will be transform to binary data
data_checksum = aa_sub_command + aa_allocations + aa_product_code + aa_nr_of_fields + aa_data
aa_data_checksum = dat_check(data_checksum)
data_measurement = aa_sub_command + aa_allocations + aa_product_code + aa_nr_of_fields + aa_data
aa_data_lenght = dat_len(data_measurement)
frame_header = aa_header + aa_data_lenght
aa_header_checksum = head_check(frame_header)
aa_labeler_frame = aa_header + aa_data_lenght + aa_header_checksum + aa_sub_command + aa_allocations + aa_product_code + aa_nr_of_fields + aa_data + aa_data_checksum
print(aa_labeler_frame)
labeler_datasend = con_dat(aa_labeler_frame)
print(labeler_datasend)
try:
address_labeler = "192.168.0.214"
port_labeler = 4001
labeler_socket = socket.socket()
labeler_socket.connect((address_labeler, port_labeler))
labeler_connect_status = True
print("CONNECT_TO_LABER")
except:
print('ERROR_CONNECT_TO_LABER')
labeler_connect_status = False
#send data to labeler
if labeler_connect_status:
try:
labeler_socket.send(labeler_datasend)
print('DATA_SEND')
except:
print('ERROR_SEND')
labeler_connect_status = False
#read data from labeler
labeler_datarecive = ''
if labeler_connect_status:
while True:
try:
labeler_datarecive = labeler_socket.recv(1024)
if labeler_datarecive:
break
except:
print('error_recive')
time.sleep(1)
if labeler_datarecive:
print('data_recive =', labeler_datarecive)
else:
print('ERROR_RECIVE')
Posts: 4,529
Threads: 69
Joined: Jan 2018
Jul-24-2019, 11:05 AM
(This post was last modified: Jul-24-2019, 11:07 AM by Gribouillis.)
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.
|