[Tutor] Another parsing question

Vicki Stanfield vicki at stanfield.net
Tue Mar 23 10:13:45 EST 2004


Once again, I appreciate your patience here. I finally got around to
trying what Alan posted to solve my problem of sending a hexidecimal
number out the serial port. I got an error. Here is his response to make
this more clear.

>That's because you have parsed a string - ie a series of characters.
>You want to send a series of bytes. You need to convert the 4
>byte character strings into single byte numbers.
>Try using the int() operaion:
<snip>
>>> int('12')
>12
>>> int('0x12',16)
>18
>>> print "As hex: %0X" % int('0x12',16)
>As hex: 12
>
>Now you can send that binary number down the pipe:
>
>for line in data_file.readlines():
>    if len(line)>1:
>       tokens=line.split("|")
>       for piece in tokens:
>          port.write(int(piece,16)) # convert to number
>          time.sleep(S_INTERVAL)
>
>HTH,
>
>Alan G.

Okay, when I use that code, with my data file which is formatted thusly:

0x02|0x34|0x45|......

I get this error. Just when I think I understand what is going on,
something else throws me off.

Traceback (most recent call last):
  File "F:\wxComTool10.py", line 2286, in CommandCallback
    self.SendCommand(self.selection, "")
  File "F:\wxComTool10.py", line 248, in SendCommand
    self.ProcessCommand(command, parameters)
  File "F:\wxComTool10.py", line 2078, in ProcessCommand
    port.write(int(piece,16))
  File "C:\Python23\Lib\site-packages\serial\serialwin32.py", line 202,
    err, n = win32file.WriteFile(self.hComPort, s, self._overlappedWrite
TypeError: Objects of type 'int' can not be directly written to a file

I am not writing to a file, but I am writing to a serial port. Can someone
tell me what I am doing wrong? Again, I can't control the inputfile.

Thanks so much for not sending an assassin to keep me from posting this
same topic over and over.

--vicki

"A pessimist sees the difficulty in every opportunity; an optimist sees
the opportunity in every difficulty."
  --  Winston Churchill




More information about the Tutor mailing list