1

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);
3
  • I'd use a terminal program like putty for this exact purpose (communicating with an Arduino on a serial port)... Commented Oct 21, 2012 at 22:06
  • May I suggest you provide more code so people can see your doConsole? There is also a very active forum where you would get an answer within a day. Most likely your Arduino is returning something because to code running on it is not how you would like it to behave, but it's hard to tell without code. I'll advice you to visit and join that forum. You will be helped within a day and find many people like you. I am an Arduino fan too there :) Commented Oct 21, 2012 at 22:09
  • Yes but I need to run it through c# as I'm creating a distributed system to control a robotic arm... Commented Oct 21, 2012 at 22:12

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

so I changed to this 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); an it didnt help ,still output 55.
0

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. 

1 Comment

nice its fun what you can do with an arduino

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.