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

Read and write over same serial port?


  • Please log in to reply
7 replies to this topic

#1 bu2002

bu2002

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationDenver

Posted 27 October 2012 - 09:02 PM

Anyone tried to read and write over the serial port from one Netduino to another? I have seen the example where he uses the XBee to write over the serial but not turn and read the slave devices values over same serial? Thanks, Brian

#2 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 28 October 2012 - 09:11 AM

i dit that. from netduino to anohter netmf device.
its the same procedure than read/write serial to/from pc -> netduino

that can be of help:
Read Me

#3 bu2002

bu2002

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationDenver

Posted 28 October 2012 - 03:22 PM

I get an exception on the return message in the Encoding Line. It gives system exception when trying to GetChars().

Master Recieve

static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
           
            int numberOfBytesRecieved = serialPort.BytesToRead;
            byte[] buffer = new byte[numberOfBytesRecieved];
            serialPort.Read(buffer, 0, buffer.Length);

            for (int i = 0; i < buffer.Length; i++)
            {
                byte aByte = buffer[i];
                switch (aByte)
                {
                    case 10:
                      break;
                    case 13:
                        try
                        {
                            string cmd = new string(System.Text.Encoding.UTF8.GetChars(commandString));
                            Debug.Print("Poll Count: " + cmd);
                        }
                        catch(Exception ex)
                        {
                            Debug.Print(ex.Message);
                        }
                      
                            for (commandStringIndex = 0; commandStringIndex < MAX_COMMAND_LENGTH; commandStringIndex++)
                            {
                                commandString[commandStringIndex] = 0;
                            }
                        
                        commandStringIndex = 0;
                        break;
                    default:
                        if (commandStringIndex < MAX_COMMAND_LENGTH)
                            commandString[commandStringIndex++] = aByte;
                        break;

                }
            }




        }


Slave Recieve:
static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e )
        {
            serialPolls++;
            int numberOfBytesRecieved = serialPort.BytesToRead;
            byte[] buffer = new byte[numberOfBytesRecieved];
            serialPort.Read(buffer, 0, buffer.Length);

            for (int i = 0; i < buffer.Length; i++)
            {
                byte aByte = buffer[i];
                switch (aByte)
                {
                    case 10:
                        break;
                    case 13:
                        string cmd = new string(Encoding.UTF8.GetChars(commandString));
                        Debug.Print("Poll Count: " + cmd);
                        for (commandStringIndex = 0; commandStringIndex < MAX_COMMAND_LENGTH; commandStringIndex++)
                        {
                            commandString[commandStringIndex] = 0;
                        }
                        commandStringIndex = 0;
                        break;
                    default:
                        if (commandStringIndex < MAX_COMMAND_LENGTH)
                            commandString[commandStringIndex++] = aByte;
                        break;

                }
            }

            serialPort.Write(buffer, 0, buffer.Length);     
            

        }

Attached Files



#4 Arron Chapman

Arron Chapman

    Advanced Member

  • Members
  • PipPipPip
  • 289 posts
  • LocationOregon, USA

Posted 28 October 2012 - 05:46 PM

I get an exception on the return message in the Encoding Line. It gives system exception when trying to GetChars().


I think we're going to need to see more of your code to make any determination as to what's wrong. I looked through what you posted but there is too much missing to say for sure. If the code is rather long, you can post it to http://pastebin.com or you can zip the solution and attach it here.

As a side note, try to avoid creating variables inside loops like you have with
string cmd = new string(System.Text.Encoding.UTF8.GetChars(commandString));
doings so makes a mess of your memory and can cause you problems down the road.

When you talk EE use small words, I'm just a Software Developer :)
My Blog/Site and Everything Else

If my post helped you please consider pressing the "Like This" button in the bottom right-hand corner.

 

Oh my. So many things, so little money!!

 


#5 bu2002

bu2002

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationDenver

Posted 28 October 2012 - 05:54 PM

As a side note, try to avoid creating variables inside loops like you have with
string cmd = new string(System.Text.Encoding.UTF8.GetChars(commandString));
doings so makes a mess of your memory and can cause you problems down the road.
[/quote]

Yes i see that as an issue. If you notice, this is the code from the example. I got it to talk both ways now after some tinkering. I went away from the example project and found some results. the end goal is to have 1 master and several slaves using the XBee radio plus. However, i feel i am a great distance away from that happening. What i am seeing is the Read() function returns bytes almost constantly which makes a straight poll difficult. I have is setup to poll every 10 seconds and the slave responds immediately upon BytesToRead trigger being fired. This leads to pieces of the commandString being sent back to the master and not all together. So i have used a string and a split method to hack together the response. Very clunky and not stable IMO.

Attached Files



#6 Arron Chapman

Arron Chapman

    Advanced Member

  • Members
  • PipPipPip
  • 289 posts
  • LocationOregon, USA

Posted 28 October 2012 - 06:39 PM

Yes i see that as an issue. If you notice, this is the code from the example. I got it to talk both ways now after some tinkering. I went away from the example project and found some results. the end goal is to have 1 master and several slaves using the XBee radio plus. However, i feel i am a great distance away from that happening. What i am seeing is the Read() function returns bytes almost constantly which makes a straight poll difficult. I have is setup to poll every 10 seconds and the slave responds immediately upon BytesToRead trigger being fired. This leads to pieces of the commandString being sent back to the master and not all together. So i have used a string and a split method to hack together the response. Very clunky and not stable IMO.


I havn't looked at your code yet, but a good solution might be to transmit the length of the data first, I usualy do it as 2 bytes (65,535 maximum length) so that all you have to do is read 2 bytes until you get a size then ack the response and read in that many bytes. The code would look something like this
byte[] buffer = new byte[2];
serialPort.Read(buffer);



int length = (buffer[0] << 8 | buffer[1]);

if(length <= 0)
{
	//Do Nothing
}

//0x06 is the ASCII ACK which is Acknowledge
serialPort.Write(new byte[] { 0x06 }); //Tell client to start sending data

buffer = new byte[length];

serialPort.Read(buffer);

ParseData(buffer);

When you talk EE use small words, I'm just a Software Developer :)
My Blog/Site and Everything Else

If my post helped you please consider pressing the "Like This" button in the bottom right-hand corner.

 

Oh my. So many things, so little money!!

 


#7 Nicky

Nicky

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationDenmark

Posted 28 October 2012 - 08:26 PM

I tried doing the same thing, but got some random exceptions when trying to convert the bits to chars! I threw my code away, and began to use the go!bus protocol, posted by Chris somewhere on the forums. I then connected my Go and Plus as shown on this picture: http://fabienroyer.f...-schematics.png (Thanks Fabien) + I connected the UART pins on D2+D3 on the Plus (COM2). I'll bet you can connect to regular Netduinos the same way. The reason that I recommend this solution, is that the protocol already includes framing, acks, naks and crc. It does also seem to be working way faster, than just connection the two UART pins, but it might be my original code which was slow, since I think that I still communicate with throug UART - but I really don't know to be honest. But it's might worth a try :)

ntools
TCP Listener (Beta) · FTP Server (Alpha)
Netduino Plus Go Module · Xml Parser
http://ntools.codeplex.com/


#8 bu2002

bu2002

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationDenver

Posted 28 October 2012 - 11:01 PM

Here is a working example with the Web server availible and modbus TCP for the master device. my next step is to build in the Modbus TCP for the slave. Then, hook up the XBee radios. Then, I will begin to persue the multiple slaves.

Attached Files






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.