read and write at the same time
-
Hi all! i have a read and write function which call from method in other class.. instead of either can read or write at a time,i want to make it both can work at the same time(i want to communicated via serial port).Does anybody know how to do it? using System; using System.Text; namespace yong { public class Class2 { static void Main(string[] args) { // Convert string to byte array and send string x = Console.ReadLine(); byte[] byteDateLine = Encoding.ASCII.GetBytes( x.ToCharArray() ); port CommPort = new port(); CommPort.Open(); CommPort.Write(byteDateLine); //Convert Byte to String //string sBuffer = Encoding.ASCII.GetString(dcbCommPort.Read(10)); //Console.WriteLine( sBuffer.ToString() ); } } }
-
Hi all! i have a read and write function which call from method in other class.. instead of either can read or write at a time,i want to make it both can work at the same time(i want to communicated via serial port).Does anybody know how to do it? using System; using System.Text; namespace yong { public class Class2 { static void Main(string[] args) { // Convert string to byte array and send string x = Console.ReadLine(); byte[] byteDateLine = Encoding.ASCII.GetBytes( x.ToCharArray() ); port CommPort = new port(); CommPort.Open(); CommPort.Write(byteDateLine); //Convert Byte to String //string sBuffer = Encoding.ASCII.GetString(dcbCommPort.Read(10)); //Console.WriteLine( sBuffer.ToString() ); } } }
Assuming hte port allows it, try running the methods on different threads. Otherwise, each method will only be called when the last method ends. Why do you need them to be intermixed ? I suspect the end result will be the same, in terms of speed, unless you're on a multi CPU machine. Christian Graus - Microsoft MVP - C++
-
Assuming hte port allows it, try running the methods on different threads. Otherwise, each method will only be called when the last method ends. Why do you need them to be intermixed ? I suspect the end result will be the same, in terms of speed, unless you're on a multi CPU machine. Christian Graus - Microsoft MVP - C++
-
I am not so familliar with threads methods.May you try to edit for me?thank a lot!How about i use While loop and at the same time inside the While loop contain a if-else loop?
levi`s wrote: I am not so familliar with threads methods.May you try to edit for me? MSDN[^] has plenty of example code. levi`s wrote: How about i use While loop and at the same time inside the While loop contain a if-else loop? Well, they would still be on the same thread, but if you put the write and read code in the same loop, that would make them both happen together, but I don't see how it would get faster as a result. Christian Graus - Microsoft MVP - C++
-
levi`s wrote: I am not so familliar with threads methods.May you try to edit for me? MSDN[^] has plenty of example code. levi`s wrote: How about i use While loop and at the same time inside the While loop contain a if-else loop? Well, they would still be on the same thread, but if you put the write and read code in the same loop, that would make them both happen together, but I don't see how it would get faster as a result. Christian Graus - Microsoft MVP - C++
-
thanks for your advice..i think using thread is better...may you help me to edit my source code using thread?i am just a very new beginner in c#..I need your help a lot.Thank so much!
Using threads is cleaner, but the code is a lot harder to debug, and I doubt it will be any faster, it will just be interspersed, which seems to be what you're hoping for. If you read the MSDN articles, they should give you a good start. Whatever you try, just post your code here if you get stuck ( i.e. as a new question ) and I'd be happy to help. Christian Graus - Microsoft MVP - C++
-
Using threads is cleaner, but the code is a lot harder to debug, and I doubt it will be any faster, it will just be interspersed, which seems to be what you're hoping for. If you read the MSDN articles, they should give you a good start. Whatever you try, just post your code here if you get stuck ( i.e. as a new question ) and I'd be happy to help. Christian Graus - Microsoft MVP - C++