1

I'm using this code to send a Xbee-Xbee packet through API mode to print (in a real printer) on the end node. I'm reading a .txt file, saving some lines in a var and sending a API packet. On that file there's also some ESC/POS commands. When I try to group some lines in another variable using "data +=eval(line)" I got an error. Why? If I don't use eval, the printer is going to print the commands instead of doing it....

lines = f.readlines()
    for line in lines[:-1]:
    data += eval(line)
    if len(data.encode('utf-8')) >= 220:
        xbee.tx(frame_id = b'\x01', dest_addr = b'\x77\x71', dest_addr_long = ENDERECO, data = data)
        print data
        data = ''

I've tried to use exec but didn't work either. Nor using the Eval() in the xbee.tx function after grouping it... Any suggestions?

Error when I use data+=eval(line):

    Traceback (most recent call last):
    SyntaxError: unexpected EOF while parsing
Paiusco
  • 305
  • 1
  • 14
  • 5
    `eval`/`exec` interpret strings as *Python* code - they are UTTERLY unrelated to anything you're trying to do here. The ESC/POS commands presumably start with a control character or escape sequence that keep them from being printed literally - have you even tried just sending them as-is? – jasonharper May 25 '17 at 12:38
  • 2
    And when you get an error you need to post it. It's not very helpful to say you got an error, then not say what the error is. And ya, you should almost definitely not be using `eval` here. Why not just `data += line`? – Carcigenicate May 25 '17 at 12:41
  • 1
    @jasonharper Yes, I did. When I send it as it is, the printer just print it as text... – Paiusco May 25 '17 at 17:05
  • 1
    @Carcigenicate I'm gonna edit the post, sorry, first time here. I also tried to use just `data += line` but it'd just send as a text. Or if I send `eval(data)` I got the error above in the edited post. – Paiusco May 25 '17 at 17:08
  • I'd be interested to know what led you to believe "If I don't use eval, the printer is just going to print the commands..." Did you read about some kind of "eval" command in the documentation for the printer instruction set? If so, then that's a lead we could pursue. In any case, as jasonharper has pointed out, it's not Python's role to `eval()` or `exec()` anything here. Python's job is just to pass on data---data that contain coded instructions that the *printer* will evaluate/execute. – jez May 25 '17 at 17:26
  • You're going to need to post an example of the contents of your file. I took a quick look at the ESC/POS commands: they always start with a control character, that would be a bit tricky to insert into a text file, so I suspect that you don't actually have valid commands in the file. – jasonharper May 25 '17 at 17:40

0 Answers0