Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Why does this serial port program on MSDN use System::Threading class?

Status
Not open for further replies.

matrixofdynamism

Advanced Member level 2
Joined
Apr 17, 2011
Messages
593
Helped
24
Reputation
48
Reaction score
23
Trophy points
1,298
Activity points
7,681
I only have experience with low level programming in C so I am not sure about all these namespaces found in visual studio. I posted a question earlier about needing an application that can write a txt file to serial port with a caveat. I was told that I shall have to write my own and thus this question was born after my investigation.

The code under question is found here: https://msdn.microsoft.com/en-us/li...?cs-save-lang=1&cs-lang=csharp#code-snippet-2

Please scroll down to the "Examples" section where there is example of the same prgram in C#, C++ and VB. Why do we need this application to be multithreaded. What will happen if we do not use multithreads? What if we only want to write to serial port and not read back?
 

you have two input streams
1. the user typing on the console keyboard with character transmitted to the serial line
2. characters being received from the serial line to be displayed on the console screen

as these are independed you require two treads of control

it may be possible to have a single tread if the two input streams have a polling mechanism where one can, in a loop, poll the input streams in turn to see if data is available - if data is available read and data and output it

for example,
1. the SerialPort class has the BytesToRead property which can be polled
2. the Console class has a KeyAvailable property
https://msdn.microsoft.com/en-us/library/system.console(v=vs.110).aspx
 
Last edited:
it may be possible to have a single tread if the two input streams have a polling mechanism where one can, in a loop, poll the input streams in turn to see if data is available - if data is available read and data and output it
this works
Code:
 //   readThread->Start();
// poll console and serial port for characters
while (_continue)
      {
       if (Console::KeyAvailable)
	   {
           ConsoleKeyInfo message= Console::ReadKey();//Line();
           _serialPort->Write(message.KeyChar.ToString());
	   }
	if(_serialPort->BytesToRead)
	   {
            Char message = _serialPort->ReadChar();//Existing();//ReadLine();
            Console::Write(message);
	    }
        }
but it has the disadvantage that the polling loop runs continuously consuming system resources
the threading version only runs when data is available
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top