locked
Serial Communication problem RRS feed

  • Question

  • Hello Buddies!

    I am a beginner i C# programming and I am trying to create an interface software between PIC Microcontroller and PC via Serial Port Communication.

    I am trying convert a received string (via Serial Port) from my microcontroller and then to convert it to int32. The data i receive is a string number like "132" or similar. The problem occur when i try to convert a received string number to integer so I can use it for calculations in my C# code. 

    Some code example would be very useful.

    Thank you in advance!

    • Moved by Mike Feng Friday, February 8, 2013 9:45 AM
    Thursday, February 7, 2013 8:05 PM

Answers

  • The Convert class may be useful, or using the parse methods on the Int32 type.

    int value = Convert.ToInt32("132");

    or preferable 

    int value;
    if (Int32.TryParse("132", out value))
    {
      // success do something with value
    }
    else
    {
      // failed to convert string, not a number?
    }


    • Proposed as answer by RenaudBancel Wednesday, February 13, 2013 10:51 PM
    • Marked as answer by Mike Feng Thursday, February 14, 2013 10:38 AM
    Thursday, February 7, 2013 8:36 PM
  • Chris, 

           Just to correct, we can read a string from serial port. it only depends on a user/developer to use any  method

    ReadByte

    ReadChar

    Read(Byte[], Int32,Int32)

    Read(Char[], Int32,Int32)

    ReadExisting

    ReadLine

    ReadTo

    Yes one must know the encoding for the data coming from a microcontroller side


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    • Proposed as answer by Mike Feng Friday, February 8, 2013 9:45 AM
    • Marked as answer by Mike Feng Thursday, February 14, 2013 10:37 AM
    Friday, February 8, 2013 8:25 AM

All replies

  • The Convert class may be useful, or using the parse methods on the Int32 type.

    int value = Convert.ToInt32("132");

    or preferable 

    int value;
    if (Int32.TryParse("132", out value))
    {
      // success do something with value
    }
    else
    {
      // failed to convert string, not a number?
    }


    • Proposed as answer by RenaudBancel Wednesday, February 13, 2013 10:51 PM
    • Marked as answer by Mike Feng Thursday, February 14, 2013 10:38 AM
    Thursday, February 7, 2013 8:36 PM
  • Unless I am mistaken (never used the Serial Port) you won't get a Strign result. You will more likely get a byte[] result.

    I fear you may have to read up on Encoding before converting to between string and byte:

    http://www.joelonsoftware.com/articles/Unicode.html

    And especially you need to know wich enconding the PIC Microcontroller is using.

    Thursday, February 7, 2013 10:31 PM
  • Chris, 

           Just to correct, we can read a string from serial port. it only depends on a user/developer to use any  method

    ReadByte

    ReadChar

    Read(Byte[], Int32,Int32)

    Read(Char[], Int32,Int32)

    ReadExisting

    ReadLine

    ReadTo

    Yes one must know the encoding for the data coming from a microcontroller side


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    • Proposed as answer by Mike Feng Friday, February 8, 2013 9:45 AM
    • Marked as answer by Mike Feng Thursday, February 14, 2013 10:37 AM
    Friday, February 8, 2013 8:25 AM