本文整理汇总了C#中System.IO.Ports.SerialPort.ReadExisting方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.ReadExisting方法的具体用法?C# SerialPort.ReadExisting怎么用?C# SerialPort.ReadExisting使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Ports.SerialPort
的用法示例。
在下文中一共展示了SerialPort.ReadExisting方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: getIP
//ready
//c_G?RS??FjS? fJ[??
//[Vendor: www.ai-thinker.com Version:0.9.2.4]
//OK
//ready
//FAIL
private static string getIP(SerialPort sp)
{
string cmd = "AT+CIFSR";
string result;
string tmp;
while (true) {
do {
tmp = sp.ReadExisting();
Console.WriteLine(tmp);
} while (tmp != "");
sp.WriteLine(cmd);
Thread.Sleep(1000);
int end = 0, findMsg = 0; ;
int resultReadCnt = 10;
while ((findMsg == 0 || end == 0) && resultReadCnt-- > 0) {
result = sp.ReadExisting();
if (result.Contains(cmd)) findMsg = 1;
Console.WriteLine(result);
if (findMsg != 0 && ( result.Contains("192.168.16.") || result.Contains("192.168.137.") || result.Contains("192.168.10."))) return result; //Nieładnie, ale na razie - czekamy na IP
if (findMsg != 0 && result.Contains("0.0.0.0")) { Thread.Sleep(1000); break; } //Ponawiamy
Thread.Sleep(1000);
}
Thread.Sleep(5000);
}
return null;
}
示例2: SMSDevice_Status
public static string SMSDevice_Status(string comPort)
{
SerialPort port = new SerialPort();
String operatorString = "Error";
try
{
port.PortName = comPort;
if (!port.IsOpen)
{
port.Open();
}
port.WriteLine("AT+CREG?\r");
Thread.Sleep(2000);
operatorString = port.ReadExisting();
return operatorString;
}
catch
{
return operatorString;
}
finally
{
port.Close();
}
}
示例3: getComPort
public static string getComPort()
{
try {
string[] str = SerialPort.GetPortNames();
string recieved = "";
string com = "";
serialPort = new SerialPort();
foreach (string value in str) {
Console.WriteLine(value);
serialPort.PortName = value;
serialPort.BaudRate = 9600;
serialPort.Parity = 0;
serialPort.DataBits = 8;
serialPort.ReadTimeout = 5000;
serialPort.WriteTimeout = 500;
serialPort.Open();
serialPort.Write(comTest, 0, comTest.Length);
Thread.Sleep(100);
recieved = serialPort.ReadExisting();
if (recieved.Equals("\u001by")) {
Console.WriteLine(value);
com = value;
}
serialPort.Close();
}
return com;
} catch(Exception e) {
Console.WriteLine(e);
return "";
}
}
示例4: Main
static void Main(string[] args)
{
int i = 0;
SerialPort SP = null;
Console.WriteLine("Specify which comport you want to connect to:");
foreach (string s in SerialPort.GetPortNames())
{
Console.WriteLine($"{i++,2} {s}");
}
int PortNo = int.Parse(Console.ReadLine());
try
{
SP = new SerialPort(SerialPort.GetPortNames()[PortNo]);
SP.Open();
while(true)
{
Console.Write(SP.ReadExisting());
}
}
finally
{
if (SP.IsOpen)
{
SP.Close();
}
}
Console.Read();
}
示例5: ReadResponse
public string ReadResponse(SerialPort port, int timeout)
{
string buffer = string.Empty;
do
{
if (receiveNow.WaitOne(timeout, false))
{
string t = port.ReadExisting();
buffer += t;
}
else
{
port.DiscardOutBuffer();
port.DiscardInBuffer();
receiveNow.Reset();
if (buffer.Length > 0)
throw new ApplicationException("Response received is incomplete - " + buffer);
else
throw new ApplicationException("No data received from phone.");
}
}
while (!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
return buffer;
}
示例6: Main
static void Main(string[] args)
{
sp = new SerialPort();
sp.PortName = "COM14";
Console.WriteLine("Com port: " + sp.PortName.ToString());
sp.BaudRate = 200000000;
Console.WriteLine("Baud rate: " +sp.BaudRate.ToString());
Console.WriteLine("dataBits: " + sp.DataBits.ToString());
Console.WriteLine("Stop bit: " + sp.StopBits.ToString());
Console.WriteLine("Parity bit: " + sp.Parity.ToString());
Console.WriteLine("Read Timeout: " + sp.ReadTimeout.ToString());
sp.Handshake = Handshake.RequestToSend;
sp.DtrEnable = true; // Data-terminal-ready
sp.RtsEnable = true; // Request-to-send
Console.WriteLine("Press enter to exit");
Console.WriteLine("\rPackets Recd: ");
Console.WriteLine("\rTime: " );
Console.WriteLine("\rMoving Avg: " );
Console.WriteLine("\rTotal Avg:" );
sp.ErrorReceived += Sp_ErrorReceived;
sp.Open();
sp.DataReceived += sp_DataReceived;
packetcount = 0;
DT = DateTime.Now;
string test = sp.ReadExisting(); // give it a kickstart
Timer t = new Timer();
t.Interval = 1000;
t.Elapsed += T_Elapsed;
t.Start();
Console.ReadLine();
sp.Close();
}
示例7: HamegOsziAdapter
public HamegOsziAdapter(string port, int baudrate)
{
try{
log.Info("Try to connecto to measurment device on port " + port + " with baud rate " + baudrate.ToString());
dataPort = new SerialPort(port, baudrate);
dataPort.Open();
if(!dataPort.IsOpen)
{
log.Error("Could not open comport on " + port + " with baudrate " + baudrate);
dataPort.Close();
return;
}
dataPort.WriteLine("*IDN?");
System.DateTime start = DateTime.Now;
while(dataPort.BytesToRead == 0)
{
if((DateTime.Now - start).TotalMilliseconds > 1000)
{
log.Error("No device response to *IDN? within 1s!");
dataPort.Close();
return;
}
}
deviceInfo = dataPort.ReadExisting();
log.Info("Connected to device " + deviceInfo);
}
catch(Exception ex)
{
log.Error(ex);
}
}
示例8: SerialController
public SerialController(SerialPort port)
{
_port = port;
_port.Open();
_port.DataReceived += (sender, args) => Console.Write("{0}", _port.ReadExisting());
}
示例9: Main
public static void Main(String[] Arguments)
{
foreach (var _COMPort in SerialPort.GetPortNames())
Console.WriteLine(_COMPort);
COMPort = new SerialPort("COM14", 9600, Parity.None, 8, StopBits.One);
COMPort.DataReceived += (s, e) => Console.Write(COMPort.ReadExisting());
COMPort.ReadTimeout = 4000;
COMPort.WriteTimeout = 6000;
try
{
COMPort.Open();
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
}
do
{
var CursorLeftPosition = Console.CursorLeft;
var line = Console.ReadLine();
Console.SetCursorPosition(CursorLeftPosition, Console.CursorTop -1);
COMPort.WriteLine(line);
Thread.Sleep(250);
} while (true);
}
示例10: Hexy
public Hexy()
{
LoadOffsets();
string hexyPort = "";
// Find the serial port
var ports = SerialPort.GetPortNames();
foreach (var portName in ports)
{
using (var serial = new SerialPort(portName, 9600))
{
serial.WriteTimeout = 1000;
serial.ReadTimeout = 1000;
serial.DtrEnable = true;
string check = "";
serial.DataReceived += (s, e) =>
{
check = serial.ReadExisting();
};
try
{
serial.Open();
serial.Write("V");
Thread.Sleep(100);
if (check.StartsWith("SERVOTOR"))
{
hexyPort = portName;
break;
}
}
catch (TimeoutException)
{ }
catch (IOException)
{ }
finally
{
if (serial.IsOpen)
serial.Close();
}
}
}
if (string.IsNullOrEmpty(hexyPort))
throw new Exception("Hexy port not found");
port = new SerialPort(hexyPort, 9600);
port.DtrEnable = true;
port.Open();
}
示例11: openSerial
public Boolean openSerial(String port, String arduinoVer)
{
if (SP != null && SP.IsOpen)
SP.Close();
if (port == "")
return false;
console("Opening Serial Port on: " + port);
SP = new SerialPort(port, 9600, Parity.None, 8);
SP.Open();
open = true;
//handshake
SP.ReadTimeout = 5000;
SP.Write("1");
String s = "";
int t = 0;
while (s.Length < 4 && t < 500)
{
System.Threading.Thread.Sleep(10);
s = SP.ReadExisting();
if (!s.Contains("."))
s = "";
else
s = s.Split('.')[1];
SP.Write("1");
t += 1;
}
String av = arduinoVer.Replace(@".", string.Empty);
if (av.Length < 4)
{
av = av[0] + "" + av[1] + "0" + av[2];
}
if (s == "")
console("No Responce from Arduino, going ahead with connection but errors may occour");
else
{
s = s.Substring(s.Length - 4, 4);
if (Convert.ToInt16(s) < Convert.ToInt16(av))
{
console("Arduino Code Outdated(v" + s[0] + "." + s[1] + "." + s[2] + s[3] + "). Please Update Arduino to at least v" + arduinoVer + " and then Retry");
return false;
}
console("Handshake Sucsessful, Connected to Arduino Running: v" + s[0] + "." + s[1] + "." + s[2] + s[3]);
iRacingSLI.currArduinoVersion = s[0] + "." + s[1] + "." + s[2] + s[3];
}
return true;
}
示例12: Find_Operator_Name
public static string Find_Operator_Name(string comPorts)
{
string operator_name;
SerialPort port = new SerialPort();
try
{
port.PortName = comPorts;
if (!port.IsOpen)
{
port.Open();
}
Thread.Sleep(100);
port.WriteLine("AT+COPS?\r");
Thread.Sleep(500);
String operatorString = port.ReadExisting();
string[] sub = operatorString.Split('\"');
if (sub[1] == "41301")
{
//port.WriteLine("AT+CUSD=1,\"AA180C3652281A\",15\r");
//System.Threading.Thread.Sleep(5000);
//operator_name=port.ReadExisting();
operator_name = "Mobitel";
}
else if (sub[1] == "41302" || sub[1]=="SRI DIALOG")
{
// Console.WriteLine("dialog");
// port.WriteLine("AT+CUSD=1,\"AA11AD661B291A\",15\r");
// System.Threading.Thread.Sleep(5000);
// port.WriteLine("AT+CMGF=1\r");
// port.WriteLine("ATZ\r");
// operator_name=port.ReadExisting();
operator_name = "Dialog";
}
else
{
operator_name = "Unknown";
}
}
catch
{
operator_name = "Error";
}
finally
{
port.Close();
}
return operator_name;
}
示例13: GetIpAddress
public string GetIpAddress(SerialPort serialPort1) //获取本机ip地址
{
int FrontPosition, BackPosition;
string Flag, Result;
serialPort1.Write("AT^IPINIT?\r");
do
{
Thread.Sleep(60);
Flag = serialPort1.ReadExisting();
}
while (Flag == "" || Flag == "\r\n^DSDORMANT:0\r\n\r\n" || Flag == "\r\n^DSDORMANT:1\r\n\r\n"); //申请ip后需要排除的“休眠”指令,下同
FrontPosition = Flag.IndexOf(",");
BackPosition = Flag.IndexOf(",", FrontPosition + 1);
return Result = Flag.Substring(FrontPosition + 1, BackPosition - FrontPosition - 1);
}
示例14: Init
public static void Init(string portname)
{
port = new SerialPort(portname, 115200, Parity.None, 8, StopBits.One);
port.Open();
port.WriteLine("ATZ");
log("ATZ Command send");
string response = port.ReadExisting();
if (!response.Contains("ATZ"))
{
log("Response: " + response);
throw new Exception("ATZ (reset) failed.");
}
}
示例15: StopEcho
public bool StopEcho(SerialPort serialPort1) //关闭回显
{
int FrontPosition, BackPosition;
string Flag, Result;
serialPort1.Write("ATE0\r");
do
{
Thread.Sleep(50);
Flag = serialPort1.ReadExisting();
}
while (Flag == "");
FrontPosition = Flag.IndexOf("\r\n");
BackPosition = Flag.LastIndexOf("\r\n");
Result = Flag.Substring(FrontPosition + 2, BackPosition - FrontPosition - 2);
return Result == "OK" ? true : false;
}