當前位置: 首頁>>代碼示例>>Python>>正文


Python Arduino.connect方法代碼示例

本文整理匯總了Python中Arduino.Arduino.connect方法的典型用法代碼示例。如果您正苦於以下問題:Python Arduino.connect方法的具體用法?Python Arduino.connect怎麽用?Python Arduino.connect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Arduino.Arduino的用法示例。


在下文中一共展示了Arduino.connect方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: connect_arduino

# 需要導入模塊: from Arduino import Arduino [as 別名]
# 或者: from Arduino.Arduino import connect [as 別名]
def connect_arduino(P_knob, I_knob, D_knob, 
                     P_result_box, I_result_box,  D_result_box, 
                     connected_result, com_choice, connect_button, disconnect_button, set_temp_box, manual_com_choice, 
                     start_data, stop_data, save_params_button, display_params_button, params_box, set_temp_button, auto_collect, current_temp, 
                     plot, messages, **kwargs):
    global parameters
    global arduino
    arduino = Arduino()
    parameters = np.zeros(6)
    found_PID_controller = False
    found_arduino = False
    if manual_com_choice.value:
        arduino = Arduino('COM%s'%com_choice.value, 9600, 1.0)
        if arduino.connect():
            if arduino.ID == 'PID Temperature Controller':
                found_PID_controller = True
                found_arduino = True
                connected_result.value = 'Connected on COM%s'%(com_choice.value)
                messages.write('Successfully connected to Arduino on COM%s. '%com_choice.value)
            else:
                messages.write('Successfully connected to Arduino on %s but ID was not correct.\nID: %s\n' %(com_choice.value, arduino.ID))
                found_arduino = True
                arduino.disconnect()
        else:
            connected_result.value = 'Failed to connect on COM%s' %(com_choice.value)
    else:
        coms = []
        
        for x in map(str, range(1,101)): coms.append('COM'+x)
        for com in coms:
            arduino = Arduino(com, 9600, 1.0)
            if arduino.connect():
                if arduino.ID == 'PID Temperature Controller':
                    found_PID_controller = True
                    found_arduino = True
                    connected_result.value = 'Connected on %s' %(com)
                    messages.write('Successfully connected to Arduino on %s. '%com)
                    print arduino.ID
                else:
                    messages.write('Successfully connected to Arduino on %s but ID was not correct.\nID: %s\n' %(com, arduino.ID))
                    found_arduino = True
                    arduino.disconnect()
                    
            else:
                connected_result.value = 'Failed to connect on %s' %(com)
            if found_PID_controller: break
        if not found_arduino and not found_PID_controller:
            messages.write('Failed to connect to Arduino on COMs 1-100\n')
    if found_arduino:
        if found_PID_controller:
            messages.write('PID Temperature Controller found.\n')
            if arduino.is_connected and arduino.is_ready:
                time.sleep(.2)
                messages.write('Arduino is open for communication.\n')
                P_knob.enabled=True
                I_knob.enabled=True
                D_knob.enabled=True
                P_result_box.enabled=True
                I_result_box.enabled=True
                D_result_box.enabled=True
                connect_button.enabled = False
                disconnect_button.enabled = True
                save_params_button.enabled=False
                display_params_button.enabled=True
                params_box.value=''
                set_temp_button.enabled=True
                start_data.enabled=True
                stop_data.enabled=True
                auto_collect.enabled=True

                initialize_parameters(P_knob, I_knob, D_knob, 
                    P_result_box, I_result_box, 
                    D_result_box, set_temp_box, start_data, stop_data, 
                    current_temp, auto_collect, messages, plot, params_box, **kwargs)
            else:
                messages.write('Arduino connected but not yet ready for serial communication.\n')
        else:
            messages.write('Failed to find PID Temperature Controller, but Arduino boards detected.\n')
    else:
        arduino.disconnect()
        P_knob.enabled=False
        I_knob.enabled=False
        D_knob.enabled=False
        P_result_box.enabled=False
        I_result_box.enabled=False
        D_result_box.enabled=False
        disconnect_button.enabled=False
        connect_button.enabled=True
        set_temp_button.enabled=False
        display_params_button.enabled=False
        start_data.enabled=False
        stop_data.enabled=False
開發者ID:cmcpartland,項目名稱:PID-Temperature-Controller,代碼行數:94,代碼來源:PID_interface.py


注:本文中的Arduino.Arduino.connect方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。