【问题标题】:How to send numbers from C# to Arduino Uno with a usb port?如何使用 USB 端口将数字从 C# 发送到 Arduino Uno?
【发布时间】:2016-08-26 18:04:13
【问题描述】:

我想使用 USB 端口将整数(101 到 1616 之间)从 c# 程序发送到 arduino Uno。 我知道如何使用 arduino Uno 的寄存器,并且想知道通过这个 USB 端口恢复数据时是否存在中断。 目前我正在使用它,但它不起作用。

if(Serial.available()!=0) { input= (unsigned char)Serial.read(); }

在 C# 程序中,我正在使用以下代码:

 byte[] b = BitConverter.GetBytes(MyIntx);
                SerialPort.Write(b, 0, 4);

如何在 c# 程序和 arduino uno 之间发送更大的数字? 接收这些类型的数字是否有特殊的中断?

【问题讨论】:

    标签: c# arduino serial-port arduino-uno


    【解决方案1】:

    您需要做的是逐字节发送。有复杂的解决方案,也有简单的解决方案 复杂:来自https://www.baldengineer.com/arduino-multi-digit-integers.html

     void setup() {
      Serial.begin(9600);
    }
    
    unsigned int integerValue=0;  // Max value is 65535
    char incomingByte;
    
    void loop() {
      if (Serial.available() > 0) {   // something came across serial
        integerValue = 0;         // throw away previous integerValue
        while(1) {            // force into a loop until 'n' is received
          incomingByte = Serial.read();
          if (incomingByte == '\n') break;   // exit the while(1), we're done receiving
          if (incomingByte == -1) continue;  // if no characters are in the buffer read() returns -1
          integerValue *= 10;  // shift left 1 decimal place
          // convert ASCII to integer, add, and shift left 1 decimal place
          integerValue = ((incomingByte - 48) + integerValue);
        }
        Serial.println(integerValue);   // Do something with the value
      }
    }
    

    我的版本: 将 Int 转换为 Char,逐字节发送并在 Arduino 中将其转换回 Int。

        int x=150;
        string x_char = x.ToString();
    for(int i=0;i<x_char.length();i++)
    {
    //Send Each Character to Arduino  "c_char[i]".
    }
    //Send a new line or any special character to mark the break. For Instance "\n";
    

    在阿杜诺:

    String a;
    
    void setup() {
    
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
    
    }
    
    void loop() {
    
    while(Serial.available()) {
    
    a= Serial.readStringUntil('\n'); //Read until new line 
    x = Serial.parseInt(); //This is your Integer value
    
    }
    
    }
    

    【讨论】:

      【解决方案2】:
       Connect();
                  if (serialPort1.IsOpen)
                  {
      
                      int MyInt = ToInt32(lblMixerCase.Text);
                      byte[] b = GetBytes(MyInt);
                      serialPort1.Write(b, 0, 1);
      
                      int MyInt2 = ToInt32(txtRPM.Text);
                      if (MyInt2<=255)
                      {
                          byte[] z = GetBytes(MyInt2);
                          serialPort1.Write(z, 0, 1); //For first 1 byte numbers
                      }
                      else if (MyInt2<=510)
                      {
                          byte[] r = GetBytes(MyInt2);
                          serialPort1.Write(r, 0, 2); for 2 byte numbers
                      }
                      else if (MyInt2<=765)
                      {
                          byte[] a = GetBytes(MyInt2);
                          serialPort1.Write(a, 0, 3); //for 3 byte numbers
                      }
                      else if (MyInt2<=1020)
                      {
                          byte[] q = GetBytes(MyInt2);
                          serialPort1.Write(q, 0, 4); //for 4 byte numbers
                      }
      

      【讨论】:

      • 请在代码之外提供一些解释。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      相关资源
      最近更新 更多