How to print Arabic text using network printer - python-3.x

I have tried to implement an Arabic language by using network printer. But When I am printing through a local printer directly connected to the Windows workstation, I am printing Arabic successfully since it is using the Windows drivers through print driver.
But, When I am printing through a network printer directly from the printer ip, I am unable to print Arabic.
from escpos.printer import Network
kitchen = Network("192.168.1.111")
kitchen.text(" اطبع هذا النص")
kitchen.cut()
also, I have tried to solve this issue using encode("utf-8"). Here is the code,
from escpos.printer import Network
kitchen = Network("192.168.1.111")
kitchen.text(" اطبع هذا النص".encode("utf-8"))
kitchen.cut()

Related

Jetson AGX Orin: tty device usable only once before failing

I am using a barcode scanner as part of a project, everything works correctly until I exit the program then I can't communicate anymore with the barcode scanner. This holds true for whatever program I'm running, be it one of my own or just using screen to monitor the transmissions. As soon as I exit, the only way to make the scanner work again is to unplug and replug.
The scanner (this one) is always mounted correctly (usually at /dev/ttyACM0) and communicates by SSI over USB CDC.
I’ve tried monitoring with pyserial’s miniterm and with screen /dev/ttyACM0 9600 but the same problem arises (f.e. screen just says [screen is terminating])
Mind you, everything works well on another computer so I believe it might be an issue with the Jetson rather than the scanner.
In the project I’m trying to run, I use pyserial to interact with the device. Here is an extract of the code to give you an idea of how I use it:
import serial
serial_port = "/dev/ttyACM0"
baud_rate = 9600
with serial.Serial(serial_port, baud_rate, timeout=0.1) as device_serial:
device_serial.flush()
while True:
try:
# read a line from the serial port
barcode_byte_string = device_serial.readline()
if len(barcode_byte_string) > 0:
# convert the byte string to a string and strip the newline character
barcode = barcode_byte_string.decode("utf-8").rstrip()
# publish the barcode to the topic
self.publish_barcode(barcode, serial_port)
except serial.SerialException as e:
# exit with error code 1. This will cause the application to be restarted.
sys.exit(1)
except Exception as e:
break

Python: Access photos on iPhone for Exif Data

I am looking to access all photos on my iPhone using Python.
I want to be able to exract the Exif data to load into Leaflet / Folium.
It would seem that I cannot access the path for the photos because the phone is connected using a MTP Connection.
I found a script and managed to modify it to allow me to list all photos on the phone in the DCIM directory.
for pidl in folder.EnumObjects(0, shellcon.SHCONTF_NONFOLDERS):
name = folder.GetDisplayNameOf(pidl, shellcon.SHGDN_FORADDRESSBAR)
dirname = os.path.dirname(name)
basename, ext = os.path.splitext(os.path.basename(name))
#print (name)
#print (dirname)
#print (basename)
if ext.endswith("JPG") and i == 1:
photo_dict[dirname].append(name)
i =+ 1
print (photo_dict[dirname][0])
I am access the path to the image, but when I try and open the image using PIL, I receive an error that the path does not exist. Which I am guessing is due to the MTP connection and the way that interacts with the File Explorer.
How can I process imagery using the path provided by my script, is there a better method?
I want to keep the files on the phone, so copying off is not an option.
I am using Python 3.5 and Windows 10.

setting default printer custom page size with python

At the organization I work for, different printers are set up at various locations. All are mainly used to print A4-sized documents, so the defaults are set up accordingly.
We are also using a bunch of custom-sized forms which people have up to now been filling in by hand.
Recently, I was tasked with setting up print-automation onto the said forms from our central database.
I'm using reportlab to create temporary pdf files which I am then trying to send to the default printer. All is relatively simple, save for getting the printers to register a custom paper size.
I got as far as the following code snippet, but I'm really stuck.
import tempfile
import win32api
import win32print
pdf_file = tempfile.mktemp(".pdf")
#CREATION OF PDF FILE WITH REPORTLAB
printer = win32print.GetDefaultPrinter()
PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}
pHandle = win32print.OpenPrinter(printer, PRINTER_DEFAULTS)
level = 2
properties = win32print.GetPrinter(pHandle, level)
pDevModeObj = properties["pDevMode"]
pDevModeObj.PaperSize = 0
pDevModeObj.PaperLength = 2200 #SIZE IN 1/10 mm
pDevModeObj.PaperWidth = 1000 #SIZE IN 1/10 mm
properties["pDevMode"]=pDevModeObj
win32print.SetPrinter(pHandle,level,properties,0)
#OPTION ONE
#win32api.ShellExecute(0, "print", pdf_file, None, ".", 0)
#OPTION TWO
win32api.ShellExecute (0,"printto",pdf_file,'"%s"' % printer,".",0)
win32print.ClosePrinter(pHandle)
It just does not work. Printers do not report a "paper size mismatch", like they should when a non-A4 document is being sent to them. And when I try printing to a PDF printer, it also defaults to A4.
When calling
print(pDevModeObj.PaperSize)
print(pDevModeObj.PaperLength)
print(pDevModeObj.PaperWidth)
everything seems to be in order, so I'm guessing I don't know how to send those paper size values back to the printer settings.
Here is a list of all the resources I checked out (examples not all in python, and a few are not using the win32api), and couldn't get the thing to work properly:
Programmatically Print a PDF File - Specifying Printer
Python's win32api only printing to default printer
https://mail.python.org/pipermail/python-win32/2005-August/003683.html
https://learn.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-setprinter-api
Print PDF file in duplex mode via Python
https://www.thinbug.com/q/39249360
Saving / Restoring Printer DevModes - wxPython / win32print
pywin32: how do I get a pyDEVMODE object?
https://learn.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-documentproperties
How to change printer preference settings using python
Print file to continuous paper using win32print Python
python win32print can't set custom page size
http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html
https://newcenturycomputers.net/projects/pythonicwindowsprinting.html
Printing a file and configure printer settings
Change printer default paper size
https://grokbase.com/t/python/python-win32/085x5hdbtd/how-to-change-paper-size-while-printing
openpyxl - set custom paper size for printing
Python win32print changing advanced printer options
Printing PDF files with Python
Python silent print PDF to specific printer
https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printerconfiguration
Printing PDF's using Python,win32api, and Acrobat Reader 9
Python print pdf file with win32print
How to chose Paper Format when printing a PDF File with Python?
Access denied when attempting to remove printer
https://www.programcreek.com/python/example/24860/win32api.ShellExecute
https://opensource.gonnerman.org/?p=192
Python27 - on windows 10 how can i tell printing paper size is 50.8mm x 25.4mm?
https://mail.python.org/pipermail/python-win32/2008-May/007640.html
http://timgolden.me.uk/python/win32_how_do_i/print.html
ShellExecute is using the default printing parameters. If you need to use the reset DevMode for printing, you can use CreateDC.
Refer: GDI Print API
If you use SetPrinter to modify the default DEVMODE structure for a
printer (globally setting the printer defaults), you must first call
the DocumentProperties function to validate the DEVMODE structure.
Refer:
SetPrinter Remarks
Modify printer settings by using the SetPrinter function
You can also directly use DocumentProperties to modify printer initialization information.
Then pass pDevModeObj to CreateDC, and use StartDoc and StartPage to print.
Similar case: Change printer tray with pywin32

Win32api for serial communication in excel VBA

I have data acquisition devices I would like to pull information from. I've started a small project in Python 3.5 using pyserial to communicate to a device. I can send commands and receive data.
import serial
ser = serial.Serial()
ser.port = 'COM1'
ser.baudrate = 9600
ser.parity = PARITY_NONE
ser.timeout=.5
ser.open()
ser.write(b'#02\r')
print(ser.readline())
ser.close()
This sends a command to retrieve data in the buffer, and when I use the readline command, I pull in data.
b'>-999999-999999-999999-999999 -999999\r'
I've created an excel sheet to host data tables and test criteria which I am judging performance of our machines on. This was initially for manual user input, but I decided I'd try and see if I can automate this directly in excel. I've poured over several webpages, found several companies that would ask for payment for code- etc. I've finally settled on work done by The Scarms which uses the WIN32API to deal with serial I/O vs. the original mscomm32.ocx driver.
I've been able to bring his files into my project, and used the sample code to start. I can send a message, and visually verify it from the device I'm communicating through, but I don't get any reply from my end data acquisition device.
strData = "#02\r"
lngSize = Len(strData)
lngStatus = CommWrite(intPortID, strData)
The variable strData is a string. When sending a message using pyserial, it's prefaced by "b" which (to my knowledge) signals it to be encoded to bits before sent through the serial port.
I've been trying to look through the modCOMM code that gets added to VBA from the code provided by the link above, but I can't seem to get an input at all. Am I sending the information incorrectly using the WIN32API?
How do I send this command over the bus properly in order to get a response from the end device?
The end device in question is an Advantech ADAM 4017+.

Can not pass OSC data using IMU manufacturer's python2.7 example script

I am working with a high refresh rate IMU (x-IO technologies NGIMU) which outputs all data in osc format. The manufacturer provides the following python script to serve the data on linux platforms ( I am running Ubuntu 16.04)
'''
NGIMU Demo python v2.7 script written by Tom Mitchell (teamxe.co.uk) 2016
Requires pyOSC https://trac.v2.nl/wiki/pyOSC
'''
import socket, OSC, threading, time
# Change this to the NGIMU IP address
send_address = '192.168.1.1', 9000
# Set the NGIMU to send to this machine's IP address
c = OSC.OSCClient()
c.connect(send_address)
msg = OSC.OSCMessage()
msg.setAddress('/wifi/send/ip')
msg.append(str(socket.gethostbyname(socket.gethostname())))
c.send(msg)
c.close()
# Set up receiver
receive_address = '192.168.1.2', 8000
s = OSC.OSCServer(receive_address)
s.addDefaultHandlers()
def sensorsHandler(add, tags, args, source):
print add + str(args)
def quaternionHandler(add, tags, args, source):
print add + str(args)
def batteryHandler(add, tags, args, source):
print add + str(args)
# Add OSC handlers
s.addMsgHandler("/sensors", sensorsHandler)
s.addMsgHandler("/quaternion", quaternionHandler)
s.addMsgHandler("/battery", batteryHandler)
# Start OSCServer
print "\nUse ctrl-C to quit."
st = threading.Thread(target = s.serve_forever)
st.start()
# Loop while threads are running
try :
while 1 :
time.sleep(10)
except KeyboardInterrupt :
print "\nClosing OSCServer."
s.close()
print "Waiting for Server-thread to finish"
st.join()
print "Done"
The IMU hosts its own network which I connect to with the computer that is to receieve the data.
I have installed pyOSC from the location referenced in the script.
When I run the script, no data is delivered, only the message "Use ctrl-C to quit".
All connections seem to take place properly. When the script is running, I can see the udp connection at the correct ip and port using the Ubuntu firewall configuration gui. I have tried disabling the firewall but that had no effect.
Separately, I have used another computer to send udp packets to that ip and port and confirmed their receipt.
To say that I am a coding novice is far too generous. Nonetheless, I need to get this script running. Any help you can offer is greatly appreciated.
The problem is that
socket.gethostbyname(socket.gethostname())
is not setting the correct IP. You should change to
msg.setAddress('/wifi/send/ip')
msg.append('192.168.1.2')

Resources