【问题标题】:Plug n' Play usb switch即插即用 USB 开关
【发布时间】:2013-08-01 14:11:14
【问题描述】:

我目前正在开发一个硬件电子应用程序,我希望能够使用 USB 电缆轻松切换我的电路。我想要做的就是拥有一个 arduino 或其他设备,我可以将其插入任何计算机,运行一个小型应用程序并将一根电线切换到 5v。这对于许多小型应用程序来说非常实用。

问题:

-我想要一个可以插入任何计算机并通过小型应用程序切换 5v 线来控制电子设备的小型设备。我该怎么做?

【问题讨论】:

    标签: arduino usb toggle


    【解决方案1】:

    如果您的 MCU 没有 USB 前端,您可以使用汇编程序,例如。 http://www.cesko.host.sk/IgorPlugUSB/IgorPlug-USB%20%28AVR%29_eng.htm 这允许实现简单的 USB1.1 低速设备。如果您需要高速,您的 MCU 应该内置 USB 引擎,然后您可能会从 MCU 制造商那里找到合适的 USB 应用说明。

    请注意,这取决于您是否需要编写自己的驱动程序。您可以使用 HID、串行或 CDCACM 类。终于可以直接使用FTDI的桥接芯片http://www.ftdichip.com/

    【讨论】:

      【解决方案2】:

      我这里有一些代码。我使用了一个简单的晶体管IRFU5305,我将一根引线连接到 arduino 的一个引脚,另外 2 根连接到 USB 的电源线。

      根据您使用的是 n 沟道晶体管还是 p 沟道晶体管,您可能需要调整代码。

      Arduino代码:

      String inputString = "";         // a string to hold incoming data
      boolean stringComplete = false;  // whether the string is complete
      
      void setup() 
      {                
        pinMode(8, OUTPUT);
        Serial.begin(9600);
        // reserve 20 bytes for the inputString (input is max 20 chars long)
        inputString.reserve(20);
        Serial.print("waitingforinput");
        digitalWrite(8,true);
      }
      
      void loop() 
      {
        if (stringComplete) 
        {
          Serial.println(inputString);
          inputString.trim();
          if(inputString=="F8")    
          {
            digitalWrite(8,true);
          }
          else if(inputString=="T8")
          {
            digitalWrite(8,false);
          }
          inputString = "";
          stringComplete = false;
        }
        delay(500);
      }
      
      void serialEvent() 
      {
        while (Serial.available()) 
        {
          Serial.println("inputreceived");
          char inChar = (char)Serial.read();
          inputString += inChar;
          // if the incoming character is a newline, set a flag
          // so the main loop can do something about it:
          if (inChar == '\n') 
          {
            stringComplete = true;
            Serial.println(inputString);
          }    
        }
      }
      

      Python代码:

      import serial
      import time
      import os
      
      ARDUINOPORT = os.getenv('ARDUINOPORT', None)
      
      def send_string_to_arduino(cmd):
          """sends a certain command string to the arduino over Serial"""
          print ARDUINOPORT
          ser = serial.Serial()
          ser.port = ARDUINOPORT
          ser.baudrate = 9600
          ser.parity = serial.PARITY_NONE
          ser.bytesize = serial.EIGHTBITS
          ser.stopbits = serial.STOPBITS_ONE
          ser.timeout = 10
          ser.xonxoff = False
          ser.rtscts = False
          ser.dsrdtr = False
          ser.open()
          time.sleep(2)
          ser.readline()
          ser.write(cmd+"\n")
          ser.close()
      
      def switch_pin_on(pinnumber):
          """switches the pin with pinnumber(int) of the connected Arduino on"""
          cmd = "T"+str(pinnumber)
          print cmd
          send_string_to_arduino(cmd)
          return
      
      def switch_pin_off(pinnumber):
          """switches the pin with pinnumber(int) of the connected Arduino off"""
          cmd = "F"+str(pinnumber)
          print cmd
          send_string_to_arduino(cmd)
          return
      
      def set_arduino_port(comport):
          """sets to which comport the arduino is connected to, comport should be a string ("COM2")"""
          global ARDUINOPORT
          ARDUINOPORT = comport
          return
      
      if __name__ == '__main__':
          set_arduino_port("COM17")
          switch_pin_off(8)
          switch_pin_on(8)
      

      【讨论】:

        猜你喜欢
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2012-02-11
        • 2012-05-27
        • 1970-01-01
        • 1970-01-01
        • 2016-05-07
        • 2011-02-04
        相关资源
        最近更新 更多