久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 9175|回復: 18
打印 上一主題 下一主題
收起左側

Python串口助手 TK界面(含源代碼)

  [復制鏈接]
跳轉到指定樓層
樓主
學習兩個月的Python ,經過千辛萬苦,熬夜寫的軟件,含有源碼


單片機源程序如下:
  1. from tkinter import  *
  2. from tkinter import ttk
  3. from tkinter import messagebox
  4. import threading
  5. import serial
  6. import serial.tools.list_ports
  7. import time
  8. import sys





  9. data = None
  10. tx=None
  11. recv_end = False
  12. send_data=None
  13. send_data_hex=None
  14. txt_rece_show=None
  15. txt_send_show=None
  16. myser=None
  17. window=None
  18. th_receve=None
  19. ports=[]
  20. comvalue_port=None
  21. comvalue_baudrate=None
  22. comvalue_bytesize=None
  23. comvalue_prity=None
  24. comvalue_stopbits=None

  25. chekbuvalue_line = None
  26. chekbuvalue_time_show=None
  27. checkbuvalue_send_show=None

  28. combox_port=None
  29. combox_stopbits=None
  30. combox_prity=None
  31. combox_bytesize=None
  32. combox_baudrate=None

  33. radb_hex_ascii=None
  34. radb1_hex_ascii=None

  35. but_open_clos_port=None
  36. exit_mark=True
  37. port_list=[]


  38. def recv():
  39.     global tx
  40.     global data
  41.     global recv_end
  42.     global txt_rece_show
  43.     print('---rec-start---')

  44.     while exit_mark:

  45.         # print(myser.isOpen())
  46.         time.sleep(0.01)
  47.         data = ''
  48.         # data1=''
  49.         data = data.encode('utf-8')
  50.         try:
  51.             if myser.isOpen():
  52.                 time.sleep(0.01)
  53.                 n = myser.inWaiting()
  54.                 # print(n)
  55.                 if n:
  56.                     for i in range(n):
  57.                         data = data+myser.read()
  58.                     print(data)
  59.                     print(n)
  60.                     print(len(data))
  61.                     insert_txt(txt_rece_show)
  62.         except Exception as e:
  63.             print('--異常--',e)


  64. def find_port_list():
  65.     global ports
  66.     global port_list
  67.     global myser
  68.     port_test = None
  69.     port_list = serial.tools.list_ports.comports()
  70.     for i in range(len(port_list)):
  71.         ports.append(port_list[i][0])
  72.         try:
  73.             myser = serial.Serial(port_list[i][0])
  74.             myser.close()
  75.         except:
  76.             print(port_list[i][0],'打開失敗')


  77.     return ports

  78. def open_close_port():
  79.     global myser
  80.     global th_receve
  81.     global comvalue_port
  82.     global comvalue_baudrate
  83.     global comvalue_bytesize
  84.     global comvalue_prity
  85.     global comvalue_stopbits
  86.     global but_open_clos_port
  87.     global combox_port
  88.     global combox_stopbits
  89.     global combox_prity
  90.     global combox_bytesize
  91.     global combox_baudrate


  92.     myport = comvalue_port.get()
  93.     mybaudrate = comvalue_baudrate.get()
  94.     mybytesize = comvalue_bytesize.get()
  95.     myprity = comvalue_prity.get()
  96.     mystopbits = comvalue_stopbits.get()

  97.     myser.port=myport
  98.     myser.baudrate = mybaudrate
  99.     myser.bytesize=int(mybytesize)
  100.     myser.parity=myprity

  101.     if mystopbits == '1':
  102.         myser.stopbits=serial.STOPBITS_ONE
  103.     elif mystopbits == '1.5':
  104.         myser.stopbits = serial.STOPBITS_ONE_POINT_FIVE
  105.     elif mystopbits == '2':
  106.         myser.stopbits = serial.STOPBITS_TWO


  107.     try:
  108.         if myser.isOpen():

  109.             myser.close()
  110.             combox_port['state'] = "normal"
  111.             combox_baudrate['state'] = "normal"
  112.             combox_bytesize['state'] = "normal"
  113.             combox_prity['state'] = "normal"
  114.             combox_stopbits['state'] = "normal"

  115.             but_open_clos_port['text']='打開串口(串口已經關閉)'
  116.             print(myser.name,'串口已經關閉')
  117.         else:
  118.             myser.open()

  119.             combox_port['state']="disable"
  120.             combox_baudrate['state']="disable"
  121.             combox_bytesize['state']="disable"
  122.             combox_prity['state']="disable"
  123.             combox_stopbits['state']="disable"
  124.             but_open_clos_port['text'] = '關閉串口(串口已經打開)'
  125.             print(myser.name,'串口已經打開')
  126.     except Exception as e:
  127.         print('--串口打開異常—',e)
  128.         str_mes = myser.name + " 打開失敗"
  129.         open_port_mesbox=messagebox.showinfo('提示',str_mes)


  130. def send_mydata(serial):
  131.     global send_data
  132.     global radb1_hex_ascii
  133.     if serial.isOpen() :
  134.         if radb1_hex_ascii.get()==1:
  135.             # serial.write(send_data.encod('utf-8'))
  136.             serial.write(send_data.encode('utf-8'))
  137.         if radb1_hex_ascii.get()==2:
  138.             serial.write(send_data_hex)
  139.         #     pass


  140. def get_txt_send_show():
  141.     global send_data
  142.     global txt_send_show
  143.     global radb1_hex_ascii
  144.     global send_data_hex
  145.     if radb1_hex_ascii.get()==1:
  146.         send_data = txt_send_show.get('1.0', 'end')
  147.         print('send_data type',type(send_data))
  148.         print(send_data)
  149.         pass
  150.     if radb1_hex_ascii.get()==2:
  151.         send_data_hex = bytes.fromhex(txt_send_show.get('1.0', 'end'))
  152.         print('send_data_hex type',type(send_data_hex))
  153.         print(send_data_hex)
  154.     #     pass
  155.     # send_data = txt_send_show.get('1.0','end')

  156. def button_sent_data():
  157.     print(myser.isOpen())
  158.     get_txt_send_show()
  159.     send_mydata(myser)

  160. def insert_txt(tx):
  161.     global radb_hex_ascii
  162.     global chekbuvalue_line
  163.     global chekbuvalue_send_show
  164.     global data

  165.     # timeadd=''

  166.     if chekbuvalue_time_show.get()==1 & chekbuvalue_line.get()==1:
  167.         timeadd=str(time.strftime("[%H:%M:%S]", time.localtime()))
  168.         print('timeadd',timeadd)
  169.     else:
  170.         timeadd=''

  171.     if radb_hex_ascii.get()==1:
  172.         data = timeadd.encode('utf-8') + data
  173.         tx.insert('end', data)
  174.     if radb_hex_ascii.get()==2:
  175.         hex_str=''
  176.         for i in range(len(data)):
  177.             # print('%x'%data[i])
  178.             hex_str=hex_str+('%x'%data[i]).upper()+' '
  179.         hex_str=timeadd+hex_str
  180.         tx.insert('end', hex_str)

  181.     if chekbuvalue_line.get()==1:
  182.         print('自動換行已經選中')
  183.         tx.insert('end', '\n')

  184. def clear_all_txt():
  185.     global txt_rece_show
  186.     txt_rece_show.delete('1.0','end')

  187. def all_exit():
  188.     global window
  189.     global exit_mark
  190.     exit_mark=False
  191.     if myser.isOpen:
  192.         myser.close()
  193.     time.sleep(0.1)
  194.     sys.exit()



  195. def mygui():
  196.         global myser
  197.         global window
  198.         global th_receve
  199.         global comvalue_port
  200.         global comvalue_baudrate
  201.         global comvalue_bytesize
  202.         global comvalue_prity
  203.         global comvalue_stopbits
  204.         global chekbuvalue_line
  205.         global chekbuvalue_time_show
  206.         global checkbuvalue_send_show

  207.         global combox_port
  208.         global combox_stopbits
  209.         global combox_prity
  210.         global combox_bytesize
  211.         global combox_baudrate
  212.         global checkbu


  213.         global port_list

  214.         find_port_list()
  215.         # port_list[0][0]

  216.         try:
  217.             # myser = serial.Serial(port_list[0][0])
  218.             # myser.close()
  219.             th_receve = threading.Thread(target=recv)
  220.             th_receve.start()
  221.         except Exception as e:
  222.             print('--串口異常--',e)


  223.         window = Tk()
  224.         window.title('HaiLang serial tools')
  225.         window.geometry('700x400')
  226.         window.resizable(width=False,height=False)  # 窗口大小不可更改
  227.         window.protocol('WM_DELETE_WINDOW',all_exit)
  228.         # window.bind('<QUIT>',all_exit)


  229.         frame_set = Frame(window)   # 設置單元框架
  230.         frame_set.pack(side="left",padx=10)
  231.         frame_disply =Frame(window) # 顯示單元框架
  232.         frame_disply.pack(side='right')

  233. #  設置單元框架
  234.         # 串口設置 LableFrame
  235.         lf_port_set = LabelFrame(frame_set,text="串口設置")
  236.         lf_port_set.pack()

  237.         lf_port_set.rowconfigure([0,1,2,3,4],minsize=30)
  238.         lf_port_set.rowconfigure(5, minsize=10)
  239.         #*****************************************************
  240.         label_port = Label(lf_port_set,text="端  口:")
  241.         label_port.grid(row=0,column=0,padx=10)

  242.         global ports
  243.         comvalue_port=StringVar()
  244.         combox_port = ttk.Combobox(lf_port_set,textvariable=comvalue_port)
  245.         combox_port['values']=ports
  246.         combox_port.current(0)
  247.         combox_port.grid(row=0,column=1,padx=10)



  248.         #*****************************************************
  249.         label_baudrate = Label(lf_port_set, text="波特率:")
  250.         label_baudrate.grid(row=1,column=0)

  251.         comvalue_baudrate = StringVar()
  252.         combox_baudrate = ttk.Combobox(lf_port_set, textvariable=comvalue_baudrate)
  253.         combox_baudrate['values'] = [2400, 4800,9600, 19200,
  254.                                      38400, 57600, 115200]
  255.         combox_baudrate.current(2)
  256.         combox_baudrate.grid(row=1, column=1)
  257.         #*****************************************************

  258.         label_bytesize = Label(lf_port_set, text="數據位:")
  259.         label_bytesize.grid()

  260.         comvalue_bytesize = StringVar()
  261.         combox_bytesize = ttk.Combobox(lf_port_set,
  262.                                        textvariable=comvalue_bytesize)
  263.         combox_bytesize['values'] = [serial.FIVEBITS, serial.SIXBITS, serial.SEVENBITS, serial.EIGHTBITS]
  264.         combox_bytesize.current(3)
  265.         combox_bytesize.grid(row=2, column=1)
  266.         #*****************************************************

  267.         label_prity = Label(lf_port_set, text="校驗位:")
  268.         label_prity.grid()

  269.         comvalue_prity = StringVar()
  270.         combox_prity = ttk.Combobox(lf_port_set,
  271.                                        textvariable=comvalue_prity)
  272.         combox_prity['values'] = [serial.PARITY_EVEN, serial.PARITY_NONE, serial.PARITY_ODD]
  273.         combox_prity.current(1)
  274.         combox_prity.grid(row=3, column=1)
  275.         #*****************************************************

  276.         label_stopbits = Label(lf_port_set, text="停止位:")
  277.         label_stopbits.grid()

  278.         comvalue_stopbits = StringVar()
  279.         combox_stopbits = ttk.Combobox(lf_port_set,
  280.                                     textvariable=comvalue_stopbits)
  281.         combox_stopbits['values'] = [serial.STOPBITS_ONE,  serial.STOPBITS_TWO]
  282.         combox_stopbits.current(0)
  283.         combox_stopbits.grid(row=4, column=1)



  284.         #*****************************************************
  285.         global but_open_clos_port
  286.         but_open_clos_port =Button(frame_set,text='打開串口' ,command=open_close_port)
  287.         but_open_clos_port.pack(fill='x')

  288.         #*****************************************************

  289.         lf_receve_set =LabelFrame(frame_set,text="接收設置")
  290.         lf_receve_set.pack(fill='x')

  291.         global radb_hex_ascii
  292.         radb_hex_ascii = IntVar()
  293.         radb_hex_ascii.set(1)
  294.         radb_ascii = Radiobutton(lf_receve_set,text='ASCII',variable=radb_hex_ascii,value=1)
  295.         radb_ascii.grid(row=0,column=0)
  296.         radb_hex = Radiobutton(lf_receve_set,text='HEX',variable=radb_hex_ascii,value=2)
  297.         radb_hex.grid(row=0,column=1)
  298.         radb_hex_ascii.get()

  299.         chekbuvalue_line = IntVar()
  300.         checkbu_line = Checkbutton(lf_receve_set,text='自動換行',variable=chekbuvalue_line)
  301.         checkbu_line.grid(row=1,column=0,padx=10)

  302.         # checkbuvalue_send_show=IntVar()
  303.         # checkbu_send_show = Checkbutton(lf_receve_set, text='顯示發送',variable=checkbuvalue_send_show)
  304.         # checkbu_send_show.grid(row=2, column=0)

  305.         chekbuvalue_time_show = IntVar()
  306.         checkbu_time_show = Checkbutton(lf_receve_set, text='顯示時間',variable=chekbuvalue_time_show)
  307.         checkbu_time_show.grid(row=3, column=0)

  308.         but_clear_txt=Button(lf_receve_set,text='清空接收',command=clear_all_txt)
  309.         but_clear_txt.grid(row=0,column=2,padx=20)

  310. ……………………

  311. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:
mypythonserialtools.rar (8.13 MB, 下載次數: 191)


評分

參與人數 3黑幣 +90 收起 理由
外星人11111 + 30
王者不可阻擋 + 10 感謝大佬
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏14 分享淘帖 頂 踩
回復

使用道具 舉報

來自 2#
ID:296541 發表于 2020-4-12 22:56 | 只看該作者
修復了無可用串口時打開錯誤的BUG

hailangserialtools.rar

8.09 MB, 下載次數: 120, 下載積分: 黑幣 -5

評分

參與人數 1黑幣 +70 收起 理由
admin + 70 回帖助人的獎勵!

查看全部評分

回復

使用道具 舉報

板凳
ID:723508 發表于 2020-4-6 17:16 | 只看該作者
不錯,用python做這類東西頭次見,學習了。。。。
回復

使用道具 舉報

地板
ID:296541 發表于 2020-4-7 16:53 | 只看該作者
fgdzypf 發表于 2020-4-6 17:16
不錯,用python做這類東西頭次見,學習了。。。。

是滴 之前在網上找沒有找到 就自己做了一個
回復

使用道具 舉報

5#
ID:91165 發表于 2020-4-11 15:37 | 只看該作者
python的學習學習  
回復

使用道具 舉報

6#
ID:693413 發表于 2020-4-12 21:53 | 只看該作者
下載下來打不開,提示這個壓縮文件格式未知或者數據已損壞
回復

使用道具 舉報

7#
ID:296541 發表于 2020-4-12 22:16 | 只看該作者
jh2020 發表于 2020-4-12 21:53
下載下來打不開,提示這個壓縮文件格式未知或者數據已損壞

python 2.7 寫的  一般Windows 都可以打開 有可能你的電腦 缺少某種文件 你可以試試裝一下 Python2.7的環境,如果還是不行的話,有源代碼,可以用Python 2.7 打開源碼
回復

使用道具 舉報

8#
ID:296541 發表于 2020-4-12 22:26 | 只看該作者
jh2020 發表于 2020-4-12 21:53
下載下來打不開,提示這個壓縮文件格式未知或者數據已損壞

是用Python 3.7寫的   有BUG  你的電腦上需要有COM口 虛擬的也行才能打開
回復

使用道具 舉報

9#
ID:282431 發表于 2020-4-12 22:50 | 只看該作者
python語言學習學習
回復

使用道具 舉報

10#
ID:727812 發表于 2020-4-13 12:21 | 只看該作者
大神牛啊,學習了
回復

使用道具 舉報

11#
ID:693413 發表于 2020-5-10 16:31 | 只看該作者
壓縮包打開不了

51hei截圖_20200510163016.png (11.1 KB, 下載次數: 244)

51hei截圖_20200510163016.png
回復

使用道具 舉報

12#
ID:328014 發表于 2020-5-10 18:08 | 只看該作者
jh2020 發表于 2020-5-10 16:31
壓縮包打開不了

剛測了winrar最新版可以正常打開,,其他壓縮軟件不行的哦
回復

使用道具 舉報

13#
ID:748560 發表于 2020-5-10 19:08 | 只看該作者
不錯,用python做這類東西頭次見,之前都是C#做的
回復

使用道具 舉報

14#
ID:749484 發表于 2020-5-11 23:21 | 只看該作者
整個要支持,之前學了c#,最近正打算學python,可以參考
回復

使用道具 舉報

15#
ID:568538 發表于 2020-6-6 21:41 | 只看該作者
感謝分享,但是打不開,如圖所示~

_20200606214010.png (63.55 KB, 下載次數: 237)

_20200606214010.png
回復

使用道具 舉報

16#
ID:827841 發表于 2022-7-6 10:25 | 只看該作者
hailang0304 發表于 2020-4-12 22:56
修復了無可用串口時打開錯誤的BUG

很不錯 學習了!
回復

使用道具 舉報

17#
ID:691028 發表于 2022-7-6 13:23 | 只看該作者
非常好的入門參考資料,樓主太厲害了,短短不到一個月就已經修煉到如此程度,佩服佩服!
回復

使用道具 舉報

18#
ID:268736 發表于 2022-11-10 18:23 | 只看該作者
寫得太好了!可以取代 HyperTerminal 謝謝分享 !
回復

使用道具 舉報

19#
ID:517256 發表于 2022-11-11 15:42 | 只看該作者

python的學習學習  
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 2022国产精品| 亚洲www啪成人一区二区 | www.亚洲成人网 | 色综合久久伊人 | 91亚洲欧美 | 99久久精品一区二区成人 | 国产一区亚洲 | 成人欧美一区二区三区黑人孕妇 | 亚洲一区二区视频 | 日本三级在线 | 一区二区三区久久久 | 九九九视频在线观看 | 中文字幕精品视频 | 日韩电影一区二区三区 | 精品一二区 | 一区二区三区在线电影 | 亚洲国产精品久久久久婷婷老年 | 中文字幕乱码视频32 | 日韩高清中文字幕 | 黄色一级视频 | 成人在线免费电影 | 视频一区二区中文字幕日韩 | 国产一区二区影院 | 日本久久网 | 亚洲另类视频 | 精品久久一区 | 伊人春色成人网 | 欧美日本一区二区 | av激情在线| 亚洲资源在线 | 日韩免费看视频 | www.99热| 日日噜噜夜夜爽爽狠狠 | 色播99| 日本又色又爽又黄的大片 | 亚洲日韩中文字幕一区 | 日韩在线不卡 | av天天澡天天爽天天av | 国产精品资源在线 | 色偷偷人人澡人人爽人人模 | 日本成人在线观看网站 |