Python中文
首页
教程
问答
标签
搜索
登录
注册
python没有将bybyte写入串行端口
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>正如之前在论坛上讨论过的,阅读问题正在解决,但现在却遇到了写作问题。我想写一些十六进制地址到串行端口。地址看起来像:</p> <pre><code> 000062240 000062A4B 000062244 000062245 000062D50 00006225E 00006A25F </code></pre> <p>我想以0.01秒的延迟逐字节写入它们。我要写的是: 我需要在写作之前完美地写作。在C之后我需要写下地址。我把内部循环放在文本文件中读取地址。但是<code>ser.write(line[line1-1])</code>给出的字符串不能与int错误组合。有什么解决办法吗?在</p> ^{pr2}$ <p>书写代码:</p> <pre><code>with open('lut.txt', 'r') as f: for line in f: #print line ser.write('N') time.sleep(0.01) ser.write(' ') time.sleep(0.01) ser.write('C') time.sleep(0.01) for line1 in range(1,8): ser.write(line[line1-1]) time.sleep(0.01) line+=1 time.sleep(0.01) </code></pre> <p>参考完整代码。在</p> <pre><code>import serial, time from addresstable import * ser = serial.Serial() ser.port = "/dev/ttyUSB0" ser.baudrate = 38400 ser.bytesize = serial.EIGHTBITS ser.parity = serial.PARITY_NONE ser.stopbits = serial.STOPBITS_ONE ser.xonxoff = False ser.rtscts = False ser.dsrdtr = False number_address = 10 number_char = 9 timeout=1 #f=open('lut.txt','r') try: ser.open() except Exception, e: print "error open serial port: " + str(e) exit() if ser.isOpen(): try: ser.flushInput() ser.flushOutput() # reading max_packet = 20 lines = 0 while True: byteData = ser.read_until('\r',max_packet) newdata=str(byteData) print newdata.strip() ser.write('I') time.sleep(0.01) # writing with open('lut.txt', 'r') as f: for line in f: #print line ser.write('N') time.sleep(0.01) ser.write(' ') time.sleep(0.01) ser.write('C') time.sleep(0.01) for line1 in range(1,8): ser.write(line[line1-1]) time.sleep(0.01) line+=1 time.sleep(0.01) except Exception, e1: print "error communicating...: " + str(e1) else: print "cannot open serial port " </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>这个问题用简单得多的方法解决了。供将来参考。在</p> <pre><code>for i in line: newdata=i ser.write(newdata) time.sleep(0.01) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在python中从数据帧列中删除分类值?
1 回答
如何在python中从数据帧列表中删除引号
3 回答
如何在python中从数据帧创建列表
9 回答
如何在Python中从数据帧创建嵌套的JSON
7 回答
如何在Python中从数据帧显示wordcloud
7 回答
如何在Python中从数据帧的时间戳中删除字符
5 回答
如何在Python中从数据帧绘制简单绘图?
6 回答
如何在python中从数据帧行提取具有特定长度的范围?
9 回答
如何在python中从数据帧设置dict中的值
5 回答
如何在Python中从数据库中获得一个结果
4 回答
如何在python中从数据框中绘制分类条形图
9 回答
如何在Python中从数据框中选择特定细节?
6 回答
如何在python中从数据集中删除unicode
5 回答
如何在python中从数据集中删除某些数值?
2 回答
如何在python中从数据集中选择行
8 回答
如何在Python中从数组中删除元素
1 回答
如何在python中从数组中删除单个倒逗号?
2 回答
如何在python中从数组中删除对象?
4 回答
如何在python中从数组中删除引号
6 回答
如何在python中从数组中删除所有最小值
7 回答