Re: [Minimalmodbus-list] UnicodeEncodeError in debug mode (Windows, Python 3.4)
Status: Beta
Brought to you by:
pyhys
|
From: Jonas B. <jon...@ho...> - 2015-01-12 19:47:37
|
Hi Thomas,
great!
I will include this in next release.
Thanks!
/Jonas
<-----Ursprungligt Meddelande----->
From: Thomas Speckert [tho...@om...]
Sent: 5/1/2015 5:49:58 PM
To: min...@li...
Subject: [Minimalmodbus-list] UnicodeEncodeError in debug mode
(Windows,Python 3.4)
Hi,
While debugging a Python 3.4 application on Window using MinimalModbus,
I would get a UnicodeEncodeError with debug mode enabled when I ran my
application using the Windows command prompt. Here's a partial
Traceback:
File "C:\Python34\lib\site-packages\minimalmodbus.py", line 494, in
read_registers
numberOfRegisters=numberOfRegisters, payloadformat='registers')
File "C:\Python34\lib\site-packages\minimalmodbus.py", line 693, in
_genericCommand
payloadFromSlave = self._performCommand(functioncode, payloadToSlave)
File "C:\Python34\lib\site-packages\minimalmodbus.py", line 791, in
_performCommand
response = self._communicate(message, number_of_bytes_to_read)
File "C:\Python34\lib\site-packages\minimalmodbus.py", line 848, in
_communicate
format(number_of_bytes_to_read, message))
File "C:\Python34\lib\site-packages\minimalmodbus.py", line 2329, in
_print_out
sys.stdout.write(inputstring + '\n')
File "C:\Python34\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xc8' in
position 89: character maps to <undefined>
It seems the problem was a combination of the default encoding in the
Windows shell being code page 437 (IBM437), and Python trying to
interpret the "raw" Modbus frame data as a proper string. The value 0xc8
apparently couldn't be encoded using code page 437, and so an exception
was thrown.
The simplest solution to this problem was to change the encoding of the
console with the command 'chcp 65001' to use UTF-8 encoding. But I would
suggest a solution where the raw frame data is instead converted to
string representation (i.e. the char 0xc8 is converted to the 4-char
string "\xc8") before being sent to stdout. In addition to solving the
problem, this has the nice side effect of clearer debugging output :)
My solution was to add the following function to the module:
> def _rawframe_to_hexstring(string, prefix='\\x'):
> if len(string) < 1:
> return ''
> return prefix + prefix.join('{:02x}'.format(ord(char)) for char in
string)
and then change Instrument._communicate in two places:
> if self.debug:
> _print_out('\nMinimalModbus debug mode. Writing to instrument
(expecting {} bytes back): {!r}'. \
> format(number_of_bytes_to_read, message))
becomes
> if self.debug:
> _print_out('\nMinimalModbus debug mode. Writing to instrument
(expecting {} bytes back): {}'. \
> format(number_of_bytes_to_read, _rawframe_to_hexstring(message)))
and
> if self.debug:
> template = 'MinimalModbus debug mode. Response from instrument: {!r}
({} bytes), ' + \
> 'roundtrip time: {:.1f} ms. Timeout setting: {:.1f} ms.\n'
> text = template.format(
> answer,
> ...omitted...
becomes
> if self.debug:
> template = 'MinimalModbus debug mode. Response from instrument: {} ({}
bytes), ' + \
> 'roundtrip time: {:.1f} ms. Timeout setting: {:.1f} ms.\n'
> text = template.format(
> _rawframe_to_hexstring(answer),
> ...omitted...
The debug output is then more consistent - and no UnicodeEncodingError
with the default console encoding:
MinimalModbus debug mode. Writing to instrument (expecting 9 bytes
back): \x05\x03\x03\x8f\x00\x02\xf4\x20
MinimalModbus debug mode. No sleep required before write. Time since
previous read: 9.0 ms, minimum silent period: 1.00 ms.
MinimalModbus debug mode. Response from instrument:
\x05\x03\x04\x03\x66\xc7\xc8\x0c\x0e (9 bytes), roundtrip time: 21.0 ms.
Timeout setting: 2000000.0 ms.
Best Regards,
Thomas Speckert
------------------------------------------------------------------------
------
Dive into the World of Parallel Programming! The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take
a
look and join the conversation now. http://goparallel.sourceforge.net
_______________________________________________
Minimalmodbus-list mailing list
Min...@li...
https://lists.sourceforge.net/lists/listinfo/minimalmodbus-list
.
|