If newStopBits = \ Then newStopBits = defaultPortStopBits.ToString() End If
Return
CType([Enum].Parse(GetType(StopBits), newStopBits), StopBits) End Function
Public Shared Function SetPortHandshake(ByVal defaultPortHandshake As Handshake) As Handshake Dim newHandshake As String
Console.WriteLine(\options:\)
Dim s As String For Each s In [Enum].GetNames(GetType(Handshake))
Console.WriteLine(\, s) Next s
Console.Write(\, defaultPortHandshake.ToString())
newHandshake = Console.ReadLine()
If newHandshake = \ Then newHandshake = defaultPortHandshake.ToString() End If
Return CType([Enum].Parse(GetType(Handshake), newHandshake), Handshake) End Function End Class using System; using System.IO.Ports; using System.Threading; public class PortChat { static bool _continue; static SerialPort _serialPort; public static void Main() { string name; string message; StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; Thread readThread = new Thread(Read); // Create a new SerialPort object with default settings. _serialPort = new SerialPort(); // Allow the user to set the appropriate properties.
_serialPort.PortName = SetPortName(_serialPort.PortName); _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate); _serialPort.Parity = SetPortParity(_serialPort.Parity); _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits); _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits); _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);
// Set the read/write timeouts
_serialPort.ReadTimeout = 500; _serialPort.WriteTimeout = 500;
_serialPort.Open(); _continue = true; readThread.Start();
Console.Write(\); name = Console.ReadLine();
Console.WriteLine(\);
while (_continue)
{
message = Console.ReadLine();
if (stringComparer.Equals(\, message))
{
_continue = false; } else {
_serialPort.WriteLine(
String.Format(\{1}\, name, message) ); } }
readThread.Join(); _serialPort.Close(); }
public static void Read() {
while (_continue) {
try {
string message = _serialPort.ReadLine();
Console.WriteLine(message);
}
catch (TimeoutException) { } } }
public static string SetPortName(string defaultPortName) {
string portName;
Console.WriteLine(\);
foreach (string s in SerialPort.GetPortNames()) {
Console.WriteLine(\, s); }
Console.Write(\, defaultPortName);
portName = Console.ReadLine();
if (portName == \) {
portName = defaultPortName; }
return portName; }
public static int SetPortBaudRate(int defaultPortBaudRate)