鹏程万里-----python开发中遇到的问题

1.    self.tabWidget.currentChanged.connect(self.tab_change())
TypeError: argument 1 has unexpected type 'NoneType'

解决办法:

self.search.clicked.connect(lambda:self.search_information(e))

我个人理解是,当用connect时,里面调用函数时,需要给他用lambda定义。

 

2.在用modbus tk的时候总是不知道通信什么时候结束,在关闭串口的时候会遇到正在通信中被关闭的错误

Unhandled exception in thread started by <function get_info_into_DB at 0x00000284F3E74048>
Traceback (most recent call last):
  File "E:/hypn/python/uart_tool/main.py", line 69, in get_info_into_DB
    num = matt_modbus_com.execute(1, cst.READ_HOLDING_REGISTERS, 30110, 38)
  File "C:\Program Files\Python36\lib\site-packages\modbus_tk\utils.py", line 39, in new
    raise excpt
  File "C:\Program Files\Python36\lib\site-packages\modbus_tk\utils.py", line 37, in new
    ret = fcn(*args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\modbus_tk\modbus.py", line 306, in execute
    response_pdu = query.parse_response(response)
  File "C:\Program Files\Python36\lib\site-packages\modbus_tk\modbus_rtu.py", line 60, in parse_response
    raise ModbusInvalidResponseError("Invalid CRC in response")
modbus_tk.exceptions.ModbusInvalidResponseError: Invalid CRC in response

 

3.安装matplotlib的时候用pythoncharm直接导入没成功,只能用指令执行

pip3 install matplotlib即可

 

4.python查询mongodb的数据,大于+小于应该这么写

for x in self.db_list.find({"time": {"$gt": 20190614172602, "$lte": 20190614172845}}, {"time": 1, "Output_P": 1}):
 

5.modbus指令

读取40310

01 03 9d 76 00 01 4a 7c

返回

01 03 02 01 2C B8 09

 

写40310,多寄存器模式

01 10 9d 76 00 01 02 01 01 21 9f

返回

01 10 9D 76 00 01 CF BF

 

读40343

01 03 9d 97 00 1B 9B 81

返回

01 03 36 01 2C 01 2C 01 2C 01 2C 01 2C 01 2C 01 2C 01 2C 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CF 9A

 

写40369 单寄存器

01 06 9d b1 00 01 37 81

返回

 

读40410

01 03 9d da  00 19 8a 57

 

写40434 单寄存器

01 06 9d f2 00 01 c6 55

 

读40622

01 03 9e a2 00 0d 0a 05

 

 

 

6.由于用modbus tk出现了每次写了以后就没法再接受数据,也就是出现了传说中的

Response length is invalid 0

,也没发现又什么办法可以解决,于是只能用python直接串口读写

result = matt_uart_com.write(b'\x01\x10\x9d\x76\x00\x01\x02\x01\x01\x21\x9f')
time.sleep(1)  # sleep() 与 inWaiting() 最好配对使用
rec_num = matt_uart_com.inWaiting()

if rec_num:
    try:  # 如果读取的不是十六进制数据--
        data = str(binascii.b2a_hex(matt_uart_com.read(rec_num)))[2:-1]  # 十六进制显示方法2
        print(data)
    except:  # --则将其作为字符串读取
        rev_str = matt_uart_com.read(rec_num)
        print(rev_str)

time.sleep(5)
result = matt_uart_com.write(b'\x01\x03\x9d\x76\x00\x01\x4a\x7c')
time.sleep(1)  # sleep() 与 inWaiting() 最好配对使用
rec_num = matt_uart_com.inWaiting()

if rec_num:
    try:  # 如果读取的不是十六进制数据--
        data = str(binascii.b2a_hex(matt_uart_com.read(rec_num)))[2:-1]  # 十六进制显示方法2
        print(data)
    except:  # --则将其作为字符串读取
        rev_str = matt_uart_com.read(rec_num)
        print(rev_str)

然后还遇到一个问题,就是如果获取linedit这种text的字符串如何转

self.list_tmp1 = '01 10 9e a2 00 0d 1a '

然后

Hex_str = bytes.fromhex(self.list_tmp2)就转成了上面的b'型字符串可以直接发送
matt_uart_com.write(Hex_str)

5.non-hexadecimal number found in fromhex() arg at position 1559

结果发现crc函数校验后的值是0xd,而不是正常的0xXXXX这种四位的,所以出现了一个单独的d所以无法转hex

 

6.wifistick通信

1.json通信

def wifi_post_rawdata(data):
    url = 'http://' + ip_addr + '/fdbg.cgi'
    form_data = json.dumps({'data': data})
    try:
        results = requests.post(url, data=form_data)
        #print(results.text)
    except Exception as err:
        print(err)
    return results.text
internal_information = wifi_post_rawdata('01 03 76 02 00 04 ff 81 ')
url_list = data.split(',')
url_sub_list = url_list[3].split(':')
main_status_info_struct.PV1_voltage = int_str_convert_01(url_sub_list[1])

 

2.rawdata通信

 

3.搞scrollArea_2之前失败了,但是这次却意外成功了,其实很简单,先拖一个控件到桌面,然后设定大小,然后

把这个设为空白

然后设置

然后把框缩小就可以了,也就是取消自适应后需要把窗口缩小才能让滑条使能

4.在用socket的时候,开启两个线程,发现会出现错误10054,后来改成socket不长时间链接,每次发送接收完数据都关闭然后重新链接就没有问题了

 

5.PermissionError: [WinError 5] 拒绝访问。: 'c:\\program files\\python36\\lib\\site-packages\\pip-9.0.3.dist-info\\description.rst'
You are using pip version 9.0.3, however version 20.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

到C:\Program Files\Python36>where cmd
C:\Windows\System32\cmd.exe这里用管理员权限打开这个exe然后执行python -m pip install --upgrade pip即可

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值