Bombalur
Bombalur

Reputation: 45

Sending standard input to Arduino C#

I am trying to take the standard input from the console and send it to my Arduino Uno as plain ASCII.

I get the Input and strip \r\n from it using this code:

String Input = Console.Read().ToString().Replace("\r",string.Empty).Replace("\n",string.Empty);

When I perform doConsole.WriteLine(Input);, it outputs "72" which is correct, but when I do serialPort.Write(Input); the Arduino returns "55", which it does for everything.

What am I doing wrong?

My code for the C# side (host/PC):

String Input = Console.Read().ToString().Replace("\r", string.Empty).Replace("\n",string.Empty);

//Console.WriteLine(Input);
//serialPort.Write(Input);

char[] InputChar = Input.ToCharArray();
serialPort.Write(InputChar,0,1);

//byte[] InputByte = Encoding.ASCII.GetBytes(Input);
//Console.WriteLine(Input);
//serialPort.WriteLine(Input);

Thread.Sleep(25);  //Wait 0.025 second.


//***************************************************************//
// Read anything from the serial port.                           //
//***************************************************************//

numBytes = serialPort.BytesToRead;
for (int i = 0; i < numBytes; i++)
    rxPacket[i] = (byte)serialPort.ReadByte();

result = new char[numBytes];
for (int i = 0; i < numBytes; i++)
    result[i] = (char)rxPacket[i];

Console.Write("Read this from Arduino:");
Console.WriteLine(result);

Console.WriteLine("press Enter to continue");
Console.ReadKey();                                   //Read nothing.

And my Arduino sketch:

const int ledPin = 13; // The pin that the LED is attached to.
int incomingByte;      // A variable to read incoming serial data into.

void setup() {
    // Initialize serial communication:
    Serial.begin(9600);
    // Initialize the LED pin as an output:
    pinMode(ledPin, OUTPUT);
}

void loop() {
    // see if there's incoming serial data:
    if (Serial.available() > 0) {
        // Read the oldest byte in the serial buffer:
        incomingByte = Serial.read();
        // If it's a capital H (ASCII 72), turn on the LED:
        if (incomingByte == 'H') {
            digitalWrite(ledPin, HIGH);
            Serial.print(incomingByte);
        }
        // If it's an L (ASCII 76), turn off the LED:
        else if (incomingByte == 'L') {
            digitalWrite(ledPin, LOW);
            Serial.print(incomingByte);
        }
        else{
            Serial.print(incomingByte);
       }
    }
}

Edit: changed the code to the following. Still no luck; I am getting same reply.

String Input = Console.Read().ToString().Replace("\r",string.Empty).Replace("\n",string.Empty);
Console.Write(Input,0,1);
//serialPort.Write(Input);
byte[] inputByte = Encoding.ASCII.GetBytes(Input);
serialPort.Write(inputByte,0,1);

Upvotes: 1

Views: 2436

Answers (2)

user613326
user613326

Reputation: 2180

Well, I looked it up... As it turns out, ASCII code 55 = 7.

7 is the first digit of 72.

Hmm, so perhaps your sending decimal numbers to the Arduino here and the Arduino sees a 7 first. May I suggest to convert your byte and send it as a byte (a byte can only contain 0..255), but it is a single ASCII code.

Maybe for the Arduino to think about, but maybe it is not related to this. Instead of

int incomingByte;  // ints are made of 2 bytes an int isn't an incomming byte

try

Byte incomingByte;

Upvotes: 2

Bombalur
Bombalur

Reputation: 45

So I managed to get it working

Basically, converting it to a byte took me a while to work out.

this is the code I ended up with

 String Input = Console.Read().ToString().Replace("\r", string.Empty).Replace("\n",string.Empty);
        Console.Write(Input,0,1);
        byte[] inputByte = new byte[1];
        inputByte[0] = Convert.ToByte(Input);
        serialPort.Write(inputByte, 0, 1);
        //byte[] inputByte = Encoding.ASCII.GetBytes(Input);
        //serialPort.Write(inputByte,0,2);
        //String num = inputByte.ToString();
        //serialPort.WriteLine(num);
       //Console.WriteLine(Input);
        //serialPort.Write(InputByte,0,1);
        Thread.Sleep(25);  //Wait 0.025 second.


        //***************************************************************//
        // Read anything from the serial port.                           //
        //***************************************************************//

        numBytes = serialPort.BytesToRead;
        for (int i = 0; i < numBytes; i++)
            rxPacket[i] = (byte)serialPort.ReadByte();

        result = new char[numBytes];
        for (int i = 0; i < numBytes; i++)
            result[i] = (char)rxPacket[i];

        Console.Write("Read this from Arduino:");
        Console.WriteLine(result);


        Console.WriteLine("press Enter to continue");
        Console.ReadKey();                                   //Read nothing.

Seems to work Perfectly now. 

Upvotes: 0

Related Questions