Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Netduino serial communication - sent and received bytes are not equal


  • Please log in to reply
9 replies to this topic

#1 Niko

Niko

    New Member

  • Members
  • Pip
  • 5 posts
  • LocationSlovenia

Posted 17 November 2011 - 07:38 AM

Hi,

I tried out the following simple Netduino program which is supposed to read the byte and send back the same value via serial connection:

public class Program
    {
        static SerialPort serialPort;
        public static void Main()
        {
            serialPort = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);
            serialPort.Open();
            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
            Thread.Sleep(Timeout.Infinite);
        }

        static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] bytes = new byte[1];
            serialPort.Read(bytes, 0, bytes.Length);

            serialPort.Write(bytes, 0, bytes.Length);
        }

    }

The received values on Netduino are not equal to those sent from PC hyperterminal (I used X-CTU) and then the received values on PC ae not equal to those, sent back from Netduino. Below is the list of bytes (characters) I tried to send from PC to Netduino and then from Netduino back to PC using the above program:

PC byte (character) -> Netduino byte -PC byte (character)
61 (a) -> 79 -> 58 (X)
62 (B) -> 39 -> 6C (1)
63 (c) -> 78 -> 2C (,)
64 (d) -> 19 -> 76 (v)
65 (e) -> 77 -> 59 (Y)
66 (f) -> 38 -> 36 (6)
67 (g) -> 76 -> 16 (.)
68 (h) -> 9 -> 7B ({)
69 (i) -> 75 -> 5A (z)
6A (j) -> 37 -> 6D (m)
6B (k) -> 74 -> 2D (-)
6C (l) -> 18 -> 3B (j)
6D (m) -> 73 -> 5B ([)
6E (n) -> 36 -> 1B ()
6F (o) -> 72 -> 0B ()
70 (p) -> 4 -> 1F ()
71 (q) -> 71 -> 5C (\)
72 (r) -> 35 -> 6E (n)
73 (s) -> 70 -> 2E ()

Does anyone know what could possibly cause the change of byte values on serial communication and how to solve the problem?

Thank you for your help!

Niko

#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 17 November 2011 - 09:14 AM

Hello Niko. I tried the serial on Netduino, but there's no issue pending AFAIK. I already connected a normal AT-command modem, and it worked correctly, without any problem at all. I'd ask you a couple of questions: could you describe how did you connect the Netduino board to the PC? I never liked the Hyperterminal: could you try with a trivial C# program on the PC? Cheers
Biggest fault of Netduino? It runs by electricity.

#3 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 17 November 2011 - 12:37 PM

Also, can you double-check your port settings (bps, stopbits, parity, etc.)? Chris

#4 Niko

Niko

    New Member

  • Members
  • Pip
  • 5 posts
  • LocationSlovenia

Posted 17 November 2011 - 02:05 PM

It is really strange, because I used the same hyperterminal and USB2Serial adapter while communicating with GPRS/GPS custom hardware, which I plan to connect with Netduino and it worked well. Also while trying to connect Netduino to GPRS/GPS hardware I was unable to exchange AT commands/responses between these two.

I guessed there were similar cases with Netduino but haven't found any except this one which is exactly the same, using arduino instead of netduino.

In hyperterminal the settings are the following:
Baud Rate: 9600
Flow Control: None
Data Bits: 8
Parity: None
Stop Bits: 1

which match to Netduino program (
serialPort = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);
).

One more thing I just noticed is that it seems there is some kind of buffer:
here is the sequence of sent and received signs in hyperterminal: blalaXbXbl
so first the response to 'b' is 'l' and then the response to 'a' is again 'l' at first, but the next time I tipe 'a' I get 'X' and again 'X' is the output for next sent character 'b' and so on.

But when trying to check what's going on with debugger, there isn't any 'buffer' - below is the sequence of first three bytes sent from PC to Netduino and back to PC from Netduino:

PC -> Netduino -> PC
62 (B) -> 39 -> 6C (l)
62 (B) -> 39 -> 6C (l)
61 (a) -> 79 -> 58 (X)

The USB2Serial adapter is Belkin F5U409 which worked fine with all other hardware connected serially so far.

I would really appreciate any suggestion about what to try out or check.

#5 427v8

427v8

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationMinnesota

Posted 17 November 2011 - 02:26 PM

your data is all screwed up, there is no pattern to the errors that I can see. I think you have a baud rate issue somewhere, one baud rate is not the same as the other If you were dropping bits or had a stuck bit you'd see a better pattern (convert your hex values to binary to make it more visible) HTH Keith

#6 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 17 November 2011 - 02:32 PM

Niko, I'm afraid you connected your Netduino (which works at TTL/HCMOS voltage levels) to a RS232 interface, which is totally different about voltages. If so, you may seriously damage the Netduino because the voltages used by the RS232 are both positive and negative, and over the maximum allowable limit of any HCMOS device. You should purchase a MAX232 chip, or any TTL/HCMOS to RS232 shield. Could you confirm this kind of trouble?
Biggest fault of Netduino? It runs by electricity.

#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 17 November 2011 - 02:41 PM

Really good points, Mario. [CuteDigi has an RS232 shield that works with Netduino if you need to plug into an RS232 port.]

Also, one more tip. In your DataReceived event, make sure you read in all the bytes available. So instead of:
byte[] bytes = new byte[1];
Use:
byte[] bytes = new byte[serialPort.BytesToRead];

Chris

#8 Niko

Niko

    New Member

  • Members
  • Pip
  • 5 posts
  • LocationSlovenia

Posted 17 November 2011 - 05:16 PM

Thanks for help, Mario! I will use MAX232 chip to adapt voltage levels.

#9 427v8

427v8

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationMinnesota

Posted 17 November 2011 - 06:52 PM

Hey Niko, You say you use X-Ctu, so I'm thinking you may have an X-BIB or other XBee interface board lying about. If you do, you can use the board as a temporary level shifter. just run your serial lines to the X-BIB board to where the XBee plugs in. Pin 2 is out..from the Duino Pin3 in in, to the Duino Pin 10 is ground I do this all the time, it works great. Keith

#10 kluger

kluger

    New Member

  • Members
  • Pip
  • 9 posts

Posted 24 November 2011 - 04:54 PM

Hello Niko, I made the same mistake as you made! The good news is that I got a MAX232 chip to inteface between the Netduino and the RS232 interface and now all is well. There has been no apparent damage to the Netduino Plus. I am able to Rx as well as Tx! Hurray! Thanks all for your help. -dusty




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.