繁体   English   中英

如何使用USB端口将数字从C#发送到Arduino Uno?

[英]How to send numbers from C# to Arduino Uno with a usb port?

我想使用USB端口从c#程序向arduino Uno发送整数(介于101和1616之间)。 我知道如何使用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之间发送更大的数字? 接收这些类型的号码是否有特殊的中断?

您需要做的是逐字节发送。 有复杂的解决方案,也有简单的复杂解决方案:来自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";

在Arduino中:

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

}

}
 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
                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM