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()
  | ||||||||||||||
String to byte array for serial output
Started by jschaud, Mar 10 2013 09:06 PM
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
2 replies to this topic
#1Posted 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); }
#2Posted 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(); }
#3Posted 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 topic0 members, 0 guests, 0 anonymous users | ||||||||||||||
|
||||||||||||||
This webpage is licensed under a Creative Commons Attribution-ShareAlike License. | ||||||||||||||