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

String to byte array for serial output


Best Answer NooM, 10 March 2013 - 11:59 PM

Thread.Sleep(-1); will sleep forever, so your write never gets called. just remove it.

 

 

port.Write(teststring, 1, testing.Length); - why start with 1 here? you start with 0.

 

 

public static void Main()

{
initComs(); //opens com port and awaits data
Thread.Sleep(500);//added this in case the com port wasn't ready fast enough
//importData(); //imports data from SD card--for later
string teststring= "Powerisoff";
byte[] data = System.Text.Encoding.UTF8.GetBytes(teststring);
port.Write(data, 0, data.Length);
Thread.Sleep(-1);
}
 
static void initComs()
{
port = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
port.DataReceived += new SerialDataReceivedEventHandler(RecievedData);
port.Open();
}
Go to the full post


  • Please log in to reply
2 replies to this topic

#1 jschaud

jschaud

    New Member

  • Members
  • Pip
  • 8 posts

Posted 10 March 2013 - 09:06 PM

I have an rs232 shield and traffic is working fine sending and receiving on the com port. What am I missing for going from a string to a byte array to output on the com port?      
 
  public static void Main()
        {
            initComs();     //opens com port and awaits data
            Thread.Sleep(500);//added this in case the com port wasn't ready fast enough
            //importData();   //imports data from SD card--for later
            string testing = "Powerisoff";
            byte[] teststring = System.Text.Encoding.UTF8.GetBytes(testing);
            port.Write(teststring, 1, testing.Length);
        }
 
        static void initComs()
        {
            port = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
            port.DataReceived += new SerialDataReceivedEventHandler(RecievedData);
            port.Open();
            Thread.Sleep(-1);
        }


#2 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 10 March 2013 - 11:59 PM   Best Answer

Thread.Sleep(-1); will sleep forever, so your write never gets called. just remove it.

 

 

port.Write(teststring, 1, testing.Length); - why start with 1 here? you start with 0.

 

 

public static void Main()

{
initComs(); //opens com port and awaits data
Thread.Sleep(500);//added this in case the com port wasn't ready fast enough
//importData(); //imports data from SD card--for later
string teststring= "Powerisoff";
byte[] data = System.Text.Encoding.UTF8.GetBytes(teststring);
port.Write(data, 0, data.Length);
Thread.Sleep(-1);
}
 
static void initComs()
{
port = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
port.DataReceived += new SerialDataReceivedEventHandler(RecievedData);
port.Open();
}


#3 jschaud

jschaud

    New Member

  • Members
  • Pip
  • 8 posts

Posted 11 March 2013 - 03:55 AM

Thank you NooM. The start with 1 thing was a testing trial that I forgot to put back, I had some other code that kept giving me index overruns that I thought might be related. I used the sleep -1 thing from some code I found and tried to replicate. I've got a long way to go, thank you very much for taking the time to point out my mistake. 






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.