Hi i trying to send data to nextion 2.4 display from raspberry pi , i try to do change such as t0.txt="abc" but i dont know how i do with python
i try to this code block but is not work
import serial
import time
import struct
ser = serial.Serial("/dev/ttyAMA0")
print ser
time.sleep(1)
i=1
k=struct.pack('B', 0xff)
while True:
ser.write(b"t0.txt=")
ser.write(str(i))
ser.write(k)
ser.write(k)
ser.write(k)
print " NEXT"
time.sleep(1)
i=i+1`
You're missing the quotation marks around str(i). You're sending; t0.txt=1, t0.txt=2, etc. It needs to be; t0.txt="1".
Something like this should do the trick I guess:
ser.write(b"t0.txt=")
ser.write('"')
ser.write(str(i))
ser.write('"')
ser.write(k)
ser.write(k)
ser.write(k)
Related
import json
#Load the data into a list.
filename = 'population_data.json'
with open(filename)as f:`enter code here`
pop_data = json.load(f)
enter code here
#Print the 2010 population data for each country.
for pop_dict in pop_data:`enter code here`
if pop_dict['Year'] == '2010':
country_name = pop_dict['Country Name']
population = int(float(pop_dict['Value']))
print(country_name + " : " + str(population))
Am trying to extract data from a population_data.json file, but whenever i run my code it doesn't show any result and i don't get any Errors, i have save the population data file in the same folder with the code but i still have that same problem, i don't get any result of the data in the shell. i would be glad if someone can help.Thank you .
enter code here
import json
#Load the data into a list.
filename = 'population_data.json'
with open(filename)as f:`enter code here`
pop_data = json.load(f)
enter code here
#Print the 2010 population data for each country.
for pop_dict in pop_data:`enter code here`
if pop_dict['Year'] == '2010':
country_name = pop_dict['Country Name']
population = int(float(pop_dict['Value']))
print(country_name + " : " + str(population))
I would suggest starting your script in the python debugger (pdb). You can start it either by starting your script like this:
python3 -m pdb your_script.py
or by importing the pdb module. Adding the following line into your python file (for example after import json, or any other):
import pdb; pdb.set_trace()
Once it is loaded, the debugger stops at the first instruction and shows the debugger promt (Pdb) . Continue execution line by line by typing next and hitting ENTER until you hit a line where something interesting is happening. The debugger prints always the next instruction and the script path and line.
Debugger output is added below, everything after (Pdb) you have to type and confirm with ENTER
(Pdb) next
> /path/to/your_script.py(7)<module>()
-> pop_data = json.load(f)
(Pdb) next
> /path/to/your_script.py(11)<module>()
-> for pop_dict in pop_data:
Now let the debugger print contents of a variable
(Pdb) p pop_data
{"Probably": "something", "you": "don't expect here"}
I suspect either the for loop yields 0 pop_dicts and therefore the loop body is never executed, or no pop_dict key Year has value 2010 and the if body is never executed.
Alternative to type next often (== single stepping): set a break point on a specific line (your_script.py:11), and continue execution until the breakpoint is hit
(Pdb) break your_script.py:11
Breakpoint 1 at /path/to/your_script.py:11
(Pdb) continue
> /path/to/your_script.py(11)<module>()
-> for pop_dict in pop_data:
(Pdb)
For additional debugger commands see pdb commands
I'm using Modeler 18.0 and I'm new to the tool.
I inherited 30 stream files. Each stream starts with 30 excel source nodes with file names like \.xlsx (e.g. c:\source\regl_sales_01_WI_2017Q3.xlsx). I need to update all 900 nodes for the 2017Q4 versions of the source files.
Can I do this with some type of script where I can find and replace? Would this be a stand alone script? Seems like I could use something like node.setPropertyValue("full_filename", "c:\source\regl_sales_01_WI_2017Q3.xlsx") If I can only identify the script and node.
Thank you
I think your problem can be solved using standalone scripting and the stream.findAll()-functionality.
You could do something like this:
from os import listdir
from os.path import isfile, join
session = modeler.script.session()
tasks = session.getTaskRunner()
mypath = 'C:\\yourpath'
streams = [f for f in listdir(mypath) if (isfile(join(mypath, f)) and f.endswith(".str"))]
for streamFile in streams:
print(stream.getName()+" is getting processed.")
stream = tasks.openStreamFromFile(demosDir + streamFile, True)
inputNodes = stream.findAll("excelimport", None)
for in in inputNodes:
ff = in.getPropertyValue("full_filename")
ff.replace("2017Q3.xlsx", "2017Q4.xlsx")
in.setPropertyValue("full_filename", ff)
print(stream.getName()+" is processed.")
stream.close()
I did not test this, but it shouldn't need a lot of tweaking to work.
For my classroom, I have a PN532 NFC card reader/writer hooked up via UART to a Raspberry Pi 2, and I'm using Type 2 NXP NTAG213 NFC cards to store information specifically to the text record. While weak in Python, I used the example under subheader 8.3 in the NFCPy Documentation to write to the card and used "How to redirect 'print' output to a file using python?" in order to complete the output process to a text file. For a while, the reading, writing, and outputting to my text file worked:
import nfc
import nfc.ndef
import nfc.tag
import os, sys
import subprocess
import glob
from os import path
import datetime
f = open('BankTransactions.txt', 'a')
sys.stdout = f
path = '/home/pi/BankTransactions.txt'
def connected(tag): print(tag); return False
clf = nfc.ContactlessFrontend('tty:AMA0:pn532')
clf.connect(rdwr={'on-connect': connected})
tag = clf.connect(rdwr={'on-connect': connected})
record_1 = tag.ndef.message[0]
signature = nfc.tag.tty2_nxp.NTAG213
today = datetime.date.today()
print(record_1.pretty())
if tag.ndef is not None:
print(tag.ndef.message.pretty())
if tag.ndef.is_writeable:
text_record = nfc.ndef.TextRecord("Jessica has 19 GP on card")
tag.ndef.message = nfc.ndef.Message(text_record)
print >> f, "Edited by Roman", today, record_1, signature, '\n'
f.close()
Now, however, when I use the same card for testing, it will not append the data within the text file. The data is still being written to the card, as I can read the information on the card with a simple read program.
I'm working on reading device reply using QBasic. The problem is the qbasic wait for the newline or CHR$(13) before outputting the data but my device reply don't have CHR$(13) (example: "OK") so qbasic hang waiting for newline.
How can i get the reply or read comport even without newline? is this possible?
[EDIT]
CLS
OPEN "com2:9600,n,8,1,BIN,cs,ds,rs" FOR RANDOM AS #1
param$ ="Some data"
PRINT #1, param$
DO WHILE b$ <> "*CLOSE*"
INPUT #1, b$
PRINT b$
LOOP
That is my code but in that code it can't read *CLOSE* because no newline after *CLOSE*.
And another thing the device delay 5 sec before replying.
Could you give an example of your code? I suspect you are using INPUT#n , but maybe instead you should use INPUT$(x). I found an example here, see code below
a$ = ""
DO
IF LOC(1) THEN a$ = a$ + INPUT$(1, 1)
LOOP UNTIL INSTR(a$, "OK")
This code sample demonstrates accessing modem in Basic.
REM Reset modem source:
CLS
OPEN "COM2:9600,N,8,1,BIN,CS,DS,RS" FOR RANDOM AS #1
Reset$ = "ATZ" + CHR$(13) + CHR$(10)
PRINT #1, Reset$;
Inp$ = ""
DO
IF LOC(1) THEN
Inp$ = Inp$ + INPUT$(1, 1)
IF INSTR(Inp$, "OK") THEN
PRINT "Modem reset."
EXIT DO
END IF
END IF
LOOP
END
I am running a bunch of long-running tasks with IPython's great parallelization functionality.
How can I get real-time output from the ipengines' stdout in my IPython client?
E.g., I'm running dview.map_async(fun, lots_of_args) and fun prints to stdout. I would like to see the outputs as they are happening.
I know about AsyncResult.display_output(), but it's only available after all tasks have finished.
You can see stdout in the meantime by accessing AsyncResult.stdout, which will return a list of strings, which are the stdout from each engine.
The simplest case being:
print ar.stdout
You can wrap this in a simple function that prints stdout while you wait for the AsyncResult to complete:
import sys
import time
from IPython.display import clear_output
def wait_watching_stdout(ar, dt=1, truncate=1000):
while not ar.ready():
stdouts = ar.stdout
if not any(stdouts):
continue
# clear_output doesn't do much in terminal environments
clear_output()
print '-' * 30
print "%.3fs elapsed" % ar.elapsed
print ""
for eid, stdout in zip(ar._targets, ar.stdout):
if stdout:
print "[ stdout %2i ]\n%s" % (eid, stdout[-truncate:])
sys.stdout.flush()
time.sleep(dt)
An example notebook illustrating this function.
Now, if you are using older IPython, you may see an artificial restriction on access of the stdout attribute ('Result not ready' errors).
The information is available in the metadata, so you can still get at it while the task is not done:
rc.spin()
stdout = [ rc.metadata[msg_id]['stdout'] for msg_id in ar.msg_ids ]
Which is essentially the same thing that the ar.stdout attribute access does.
just in case somebody is still struggling with
getting ordinary print-outputs of the individual kernels:
I adapted minrk's answer such that i get the output of each
kernel as if it would have been a local one by constantly checking if the stdout of each kernel changes while the program is running.
asdf = dview.map_async(function, arguments)
# initialize a stdout0 array for comparison
stdout0 = asdf.stdout
while not asdf.ready():
# check if stdout changed for any kernel
if asdf.stdout != stdout0:
for i in range(0,len(asdf.stdout)):
if asdf.stdout[i] != stdout0[i]:
# print only new stdout's without previous message and remove '\n' at the end
print('kernel ' + str(i) + ': ' + asdf.stdout[i][len(stdout0[i]):-1])
# set stdout0 to last output for new comparison
stdout0 = asdf.stdout
else:
continue
asdf.get()
outputs will then be something like:
kernel0: message 1 from kernel 0
kernel1: message 1 from kernel 1
kernel0: message 2 from kernel 0
kernel0: message 3 from kernel 0
kernel1: message 2 from kernel 0
...