Read and write over same serial port?
#1
Posted 27 October 2012 - 09:02 PM
#3
Posted 28 October 2012 - 03:22 PM
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
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
Posted 28 October 2012 - 05:54 PM
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
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
Posted 28 October 2012 - 08:26 PM
ntools
TCP Listener (Beta) · FTP Server (Alpha)
Netduino Plus Go Module · Xml Parser
http://ntools.codeplex.com/
#8
Posted 28 October 2012 - 11:01 PM
Attached Files
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users