2016-08-26 9 views
2

私は、usbポートを使ってc#プログラムからarduino Unoに整数値(101〜1616の間)を送りたいと思っています。 私はarduino Unoのレジスタを使用する方法を知っており、このUSBポートを介してデータを復活させる際に割り込みがあるかどうかを知りたいと思っていました。 現在私はこれを使用していますが、動作していません。C#から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の間で大きな数字を送信することができますどのように? これらのタイプの番号を受け取るための特別な割り込みがありますか?

答えて

0

あなたがしなければならないことは、バイトごとに送信されます。複雑なソリューションがありますし、簡単なもの はコンプレックスがあります: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に変換バイトでバイトを送信し、ArduinoのではバックのIntに変換します。アルドゥイーノ、IN

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 

} 

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

コードに加えて、いくつかの説明を提供してください。 – Jan

+0

もちろん、何を理解していないのですか? – rhsn

関連する問題