本文整理汇总了C#中System.IO.Ports.SerialPort.ReadLine方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.ReadLine方法的具体用法?C# SerialPort.ReadLine怎么用?C# SerialPort.ReadLine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Ports.SerialPort
的用法示例。
在下文中一共展示了SerialPort.ReadLine方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseResponse
public bool ParseResponse(SerialPort serialPort)
{
try
{
// Dummy read of command;
serialPort.ReadLine();
var resp = serialPort.ReadLine().Trim();
resp = resp.Replace('.', ',');
Regex regex = new Regex(@"time=""(.*)"",P=""([0-9,]*)"",H=""([0-9,]*)"",T=""([0-9,]*)""");
var match = regex.Match(resp);
// Empty line
serialPort.ReadLine();
var ack = serialPort.ReadLine().Trim();
Time = match.Groups[1].Value;
Pressure = float.Parse(match.Groups[2].Value);
Humidity = float.Parse(match.Groups[3].Value);
Temperature = float.Parse(match.Groups[4].Value);
return ack.Equals("OK");
}
catch (Exception)
{
return false;
}
}
示例2: ParseResponse
public bool ParseResponse(SerialPort serialPort)
{
try
{
// Dummy read of command;
serialPort.ReadLine();
var resp = serialPort.ReadLine().Trim();
Regex regex = new Regex(@"time=""(.*)"",P=""([0-9A-Fa-f]*)"",H=""([0-9A-Fa-f]*)"",T1=""([0-9A-Fa-f]*)""");
var match = regex.Match(resp);
// Empty line
serialPort.ReadLine();
var ack = serialPort.ReadLine().Trim();
Time = match.Groups[1].Value;
Pressure = int.Parse(match.Groups[2].Value, NumberStyles.AllowHexSpecifier);
Humidity = int.Parse(match.Groups[3].Value, NumberStyles.AllowHexSpecifier);
Temperature = int.Parse(match.Groups[4].Value, NumberStyles.AllowHexSpecifier);
return ack.Equals("OK");
}
catch (Exception)
{
return false;
}
}
示例3: ParseResponse
public bool ParseResponse(SerialPort serialPort)
{
try
{
StringBuilder sb = new StringBuilder(capacity: 50000);
var s = serialPort.ReadLine();
while (true)
{
s = serialPort.ReadLine().Trim();
if (string.IsNullOrEmpty(s))
continue;
if (s == "OK") break;
sb.Append(s);
}
string bigStr = sb.ToString();
SnapshotImage = MakeBitmap(bigStr);
return true;
}
catch (Exception ex) { MessageBox.Show(ex.Message); return false; }
}
示例4: connectDisplay
public static bool connectDisplay(SerialPort serial)
{
if (serial == null || !serial.IsOpen)
{
throw new Exception(StringResource.E00001);
}
string accessString = "";
try
{
accessString = serial.ReadLine();
}
catch (Exception)
{
//if Display is idle. Try to close it and reconnect
serial.WriteLine("close:");
try
{
accessString = serial.ReadLine();
}
catch (TimeoutException)
{
return false;
}
}
if (accessString == DisplayToAppAccessString)
{
serial.WriteLine(AppToDisplayAccessString);
return true;
}
return false;
}
示例5: acquire
void acquire()
{
if(port!=null)
port.Dispose();
port = new SerialPort("COM4", 9600);
port.Open();
DateTime refreshTimer = DateTime.Now;
while (!closeThread)
{
try
{
while (port.BytesToRead > 0 && !closeThread)
{
port.ReadLine();
string data = port.ReadLine();
DateTime newTime = DateTime.Now;
if (newTime.Subtract(refreshTimer).Milliseconds > 30)
{
data = data.Substring(7);
string[] vals = data.Split(new char[] { ' ' });
double[] dvals = new double[vals.Length];
double total = 0;
for (int i = 0; i < dvals.Length; i++)
{
dvals[i] = double.Parse(vals[i].Trim());
total += dvals[i];
}
if (setScalingValues)
{
scalingValues = new double[dvals.Length];
for (int i = 0; i < scalingValues.Length; i++)
scalingValues[i] = 255/dvals[i];
setScalingValues = false;
}
double avg = total / dvals.Length;
refreshTimer = newTime;
AddPoint(avg);
RefreshForm(dvals);
port.DiscardInBuffer();
}
}
}
catch (Exception) { }
Thread.Sleep(10);
}
closeThread = false;
}
示例6: Main
public static void Main(string[] args)
{
activeConfig = new SerialMonitorConfig ("Serial.conf",args);
//assign time outs to all network commands
foreach (int i in activeConfig.networkCommands.Keys) {
timeouts.Add (i, DateTime.Now);
}
stamp ();
SerialPort serialPort = new SerialPort ();
serialPort.BaudRate = activeConfig.baudrate;
serialPort.PortName = activeConfig.portname;
//serialPort.Parity = activeConfig.Parity;
serialPort.ReadTimeout = activeConfig.readtimeout;
serialPort.WriteTimeout = activeConfig.writetimeout;
serialPort.Open ();
string message;
while (true) {
try{
message= serialPort.ReadLine ();
int cmd = 0;
if(int.TryParse(message, out cmd)){
readCommand(cmd);
}
}
catch(Exception ex){
//
}
}
}
示例7: SPICommunicator
public SPICommunicator()
{
port = new SerialPort() {
BaudRate = 115200,
DataBits = 8,
StopBits = StopBits.One,
Parity = Parity.None,
PortName = "COM3",
Handshake = Handshake.None
};
if (!port.IsOpen) {
port.Open();
}
string line = "TEST";
int i = 0;
do {
port.Write(line);
string inLine = port.ReadLine();
i++;
} while (i < 3);
port.Close();
}
示例8: Main
static void Main(string[] args)
{
try
{
Serfid appSerfid = new Serfid();
appSerfid.Run();
_serfidPort = new SerialPort("COM3");
_serfidPort.Open();
System.Console.WriteLine("Serfid reading on port: COM3");
while (true)
{
System.Console.WriteLine("\nWaiting for readings...");
string reading = _serfidPort.ReadLine();
appSerfid.ReadWeft(reading);
System.Console.SetCursorPosition(0, System.Console.CursorTop - 1);
string result = string.Format("Tag {0} processed successful", reading.Replace("\r", ""));
System.Console.WriteLine(result);
}
}
finally
{
_serfidPort.Close();
}
}
示例9: Reading
private void Reading()
{
var currentPort = new SerialPort(_name, 115200);
currentPort.Open();
while (!stop)
{
var line = currentPort.ReadLine();
try
{
var strings = line.Replace("\r", "").Split(',');
var x = (int)int.Parse(strings[0]) / 16384.0F;
var y = (int)int.Parse(strings[1]) / 16384.0F;
var z = (int)int.Parse(strings[2]) / 16384.0F;
if (!_valuesInitialized)
{
_x = x;
_y = y;
_z = z;
_valuesInitialized = true;
}
_x = (_x*3 + x)/4;
_y = (_y*3 + y)/4;
_z = (_z*3 + z)/4;
}
catch (Exception)
{
}
HasNew = true;
}
}
示例10: Main
static void Main(string[] args)
{
String RxedData; // String to store received data
String COM_PortName; // String to store the name of the serial port
SerialPort MyCOMPort = new SerialPort(); // Create a new SerialPort Object
Console.WriteLine("\t+------------------------------------------+");
Console.WriteLine("\t| Reading from Serial Port using C# |");
Console.WriteLine("\t+------------------------------------------+");
Console.WriteLine("\t| (c) www.xanthium.in |");
Console.WriteLine("\t+------------------------------------------+");
Console.Write("\n\t Enter the COM port [eg COM32] -> ");
COM_PortName = Console.ReadLine();
COM_PortName = COM_PortName.Trim(); // Remove any extra spaces
COM_PortName = COM_PortName.ToUpper(); // convert the string to upper case
//------------ COM port settings to 8N1 mode ------------------//
MyCOMPort.PortName = COM_PortName; // Name of the COM port
MyCOMPort.BaudRate = 9600; // Baudrate = 9600bps
MyCOMPort.Parity = Parity.None; // Parity bits = none
MyCOMPort.DataBits = 8; // No of Data bits = 8
MyCOMPort.StopBits = StopBits.One; // No of Stop bits = 1
MyCOMPort.Open(); // Open the port
Console.WriteLine("\n\t Waiting for Data....");
RxedData = MyCOMPort.ReadLine(); // Wait for data reception
Console.WriteLine("\n\t \" {0} \"",RxedData); // Write the data to console
Console.WriteLine("\n\t Press any Key to Exit");
Console.Read(); // Press any key to exit
MyCOMPort.Close(); // Close port
}
示例11: Main
public static void Main()
{
string ttyname = ""; // for storing name of the serial port
string ReceivedData = ""; // for storing received data from microcontroller
SerialPort Serial_tty = new SerialPort(); //Create a new serialPort object
Console.Write("\nEnter Your SerialPort[tty] name (eg ttyUSB0)->");
ttyname = Console.ReadLine(); // Read the name of the port eg ttyUSB0
ttyname = @"/dev/" + ttyname; // Concatanate so name looks like /dev/ttyUSB0
Serial_tty.PortName = ttyname; // assign the port name
Serial_tty.BaudRate = 9600; // Baudrate = 9600bps
Serial_tty.Parity = Parity.None; // Parity bits = none
Serial_tty.DataBits = 8; // No of Data bits = 8
Serial_tty.StopBits = StopBits.One; // No of Stop bits = 1
try
{
Serial_tty.Open(); //open the port
Console.WriteLine("\nWaiting for Data......");
ReceivedData = Serial_tty.ReadLine(); // Read the data send from microcontroller
Serial_tty.Close(); // Close port
Console.WriteLine("\n{0} received\n", ReceivedData);
}
catch
{
Console.WriteLine("Error in Opening {0}\n",Serial_tty.PortName);
}
}
示例12: Main
static void Main(string[] args)
{
////////////// KONSOL AYARLARI ///////////////////////////////////
Console.SetWindowSize(70, 30);
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
SerialPort myport = new SerialPort();
myport.BaudRate = 9600;
myport.PortName = "COM3";
myport.Open();
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
bool devametsinmi = true;
while (devametsinmi && true)
{
string data_rx = myport.ReadLine();
Console.WriteLine(data_rx);
}
/////////////////////////////////////////////////////////////////
}
示例13: ParseReceivedLine
private static decimal ParseReceivedLine(SerialPort port)
{
string line = port.ReadLine();
var match = Regex.Match(line, @"^(\d+\.\d+)\s*$");
var degrees = decimal.Parse(match.Groups[1].Value);
return degrees;
}
示例14: Main
static void Main(string[] args)
{
_port = new SerialPort();
_port.PortName = "COM3";
_port.BaudRate = 9600;
_port.ReadTimeout = 500;
_port.WriteTimeout = 500;
_port.Open();
Thread thread = new Thread(new ThreadStart(delegate()
{
while (_continue)
{
try
{
string data = _port.ReadLine();
string[] bits = data.Replace("\r", "").Split('\t').ToArray();
Console.WriteLine(data);
if (bits.Length == 3 && bits[0].Length > 0 && bits[1].Length > 0 && bits[2].Length > 0)
{
int xOriginal = int.Parse(bits[1]);
int yOriginal = int.Parse(bits[2]);
if (Math.Abs(xOriginal) > 50 || Math.Abs(yOriginal) > 50)
{
Cursor.Position = new System.Drawing.Point
{
X = Cursor.Position.X + -1 * xOriginal / 10,
Y = Cursor.Position.Y + yOriginal / 10
};
}
if (bits[0] == "1" && !_inClick)
{
_inClick = true;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
}
else if (bits[0] == "0")
{
_inClick = false;
}
}
}
catch (TimeoutException) { }
}
}));
thread.Start();
Console.WriteLine("Hit ENTER to quit.");
Console.ReadLine();
_continue = false;
thread.Join();
}
示例15: GetBridgeSignature
// Gets the uart->spi bridge's signature (MSP430 etc)
public static string GetBridgeSignature(SerialPort sp)
{
//sp.Write("{g00}");
sp.Write("{");
sp.Write("g");
sp.Write("0");
sp.Write("0");
sp.Write("}");
return sp.ReadLine();
}