Utf-8 codec can't decode byte 0x81

I am trying to send font commands to a Videojet printer using scripts.
It works up until I try to send a Data Font command.

Here is the script as it worked before using a single line print

	#Select the 5x HQ dual Line Matrix for use when creating a message
	command = "1B".decode('hex') + "04".decode('hex') + "05".decode('hex')
	#Create Message to print
	message = line1 + TAB  + line2 + CRETURN
	command = command + message + CRETURN

	reply = socketReqWithReply(host,command)
	reply = reply.encode('hex')
	return reply

Now what I am trying that is failing. I believe it is due to the “81”.decode(‘hex’) portion as if I change that number it stops giving me the error

	#Select the 7x9 Tri Line Matrix for use when creating a message
	command = "1B".decode('hex') + "04".decode('hex') + "07".decode('hex')
	command2 = "1B".decode('hex') + '81'.decode('hex') + "20".decode('hex')
	command3 = "1B".decode('hex') + '81'.decode('hex') + "20".decode('hex')

	
	command = command + command2 + line1 + TAB + command3 + line2 + CRETURN

	reply = socketReqWithReply(host,command)
	reply = reply.encode('hex')

The error I get
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0x81 in position 4: unexpected code byte

0x81 is not valid in utf-8. You are trying to use characters as bytes, and that is not reliable in python2 or jython. Use jython’s jarray module to create actual byte arrays, and use java networking instead of python’s socket (to not re-corrupt the bytes).

1 Like

I found a link to jarray module but how would I format the command [1B][81][20] in the array?

And where would I find good documentation on java networking?

# Java bytes are signed, so bias anything >=0x80 by 256
cmd = jarray.array([0x1b, 0x81-256, 0x20], 'b')

Heh. Stack exchange? Or just search this forum for java.net and/or java.nio.