本文整理汇总了C#中SerialPort.ReadLine方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.ReadLine方法的具体用法?C# SerialPort.ReadLine怎么用?C# SerialPort.ReadLine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SerialPort
的用法示例。
在下文中一共展示了SerialPort.ReadLine方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendInfo
public static string SendInfo(HttpListenerRequest request)
{
string context = "";
Boolean succeeded = false;
int count = 0;
SerialPort serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
while (!succeeded)
{
try
{
serialPort.Open();
int cm1 = Convert.ToInt32(serialPort.ReadLine());
int cm2 = Convert.ToInt32(serialPort.ReadLine());
if (cm1 >= 30)
{
context = "false:" + cm1.ToString();
Console.WriteLine(context);
}
else
{
context = "true:" + cm1.ToString();
Console.WriteLine(context);
}
succeeded = true;
if (cm1 != cm2)
{
succeeded = false;
count++;
}
if (count>=9){
context = "false:outOfRange";
Console.WriteLine(context);
succeeded = true;
}
}
catch (Exception ex)
{
context = "open error " + ex.Message;
}
finally
{
serialPort.Close();
}
}
return string.Format(context);
}
示例2: GetArduino
private void GetArduino()
{
stream = new SerialPort(COMPort, 19200);
stream.Open();
Debug.Log("123");
while (myThread.IsAlive && !bclose) {
string value = stream.ReadLine(); //Read the information
string[] vec3 = value.Split(',');
if (vec3.Length >= 4)
{
fx_axis = Convert.ToSingle(vec3[0]);
fy_axis = Convert.ToSingle(vec3[1]);
fsound = Convert.ToSingle(vec3[2]);
fbtn = Convert.ToSingle(vec3[3]);
}
}
}
示例3: OpenConnection
public bool OpenConnection()
{
try
{
serialPort = new SerialPort("\\\\.\\" + portName, serialSpeed);
serialPort.Open();
if (serialPort.IsOpen)
{
serialPort.ReadTimeout = 1000 / 120; // this is half a unity frame
serialPort.WriteTimeout = 1000 / 120;
serialPort.WriteLine("arduinity_check"); // send message to arduino
string reply = "";
while (reply == "")
{
reply = serialPort.ReadLine();
if (reply != null && reply != "")
{
if (reply.StartsWith("arduinity_present"))
{
#if debugSerial
Debug.Log("ArduinityCommunicator::OpenConnection(): found arduinity on " + portName);
#endif
// parse the name
arduinoName = reply.Substring(reply.LastIndexOf('_') + 1);
return true;
}
else
{
#if debugSerial
Debug.Log("ArduinityCommunicator::OpenConnection(): got wrong response on " + portName + " " + reply);
#endif
return false;
}
}
}
}
else
{
#if debugSerial
Debug.Log("ArduinityCommunicator::OpenConnection(): " + portName + " is not open");
#endif
return false;
}
}
#if debugSerial
catch(Exception e)
{
Debug.Log("ArduinityCommunicator::OpenConnection(): exception trying to read from " + portName + " " + e.Message);
return false;
}
#else
catch
{
return false;
}
#endif
return false;
}
示例4: Start
/// <summary>
/// Initialisation of the Arduino IO-Box script.
/// </summary>
///
public void Start()
{
if ( !scriptVehicleData ) Debug.LogError("No vehicle data collector script defined!");
if ( !scriptConfiguration ) Debug.LogError("No simulation configuration script defined!");
vehicleData = null;
speedOfSound = scriptConfiguration.GetSpeedOfSound();
serialPort = new SerialPort(modulePort, moduleSpeed, Parity.None, 8, StopBits.One);
serialPort.Handshake = Handshake.None;
serialPort.RtsEnable = false;
serialPort.DtrEnable = false; // Disable DTR so NOT to reset Arduino board when connecting
serialPort.ReadTimeout = 250; // longer read timeout (module might have had a reset nevertheless)
bool success = false;
try
{
serialPort.Open();
}
catch (IOException)
{
serialPort = null;
}
if ( (serialPort != null) && serialPort.IsOpen )
{
int repeats = 8; // try several times to connect
while ( (repeats-- > 0) && !success )
{
// send the ECHO command
serialPort.WriteLine(CMD_ECHO);
try
{
String serialNo = serialPort.ReadLine();
if ( serialNo.Length > 1 )
{
Debug.Log ("Opened serial port " + modulePort + " to Arduino IO (Version: " + serialNo + ")");
success = true;
serialPort.ReadTimeout = 50; // from now on, shorter response times, please
StartCoroutine(RunDiagnose());
}
}
catch (TimeoutException)
{
// ignore
}
}
}
if ( !success )
{
Debug.LogError("Could not open serial port " + modulePort + " to the Arduino IO module.");
if ( serialPort != null )
{
serialPort.Close();
serialPort = null;
}
}
}
示例5: Read
string Read(SerialPort port)
{
string message = "";
try {
for (int i=0; i<maxLinesPerBatch; ++i) {
if (port.BytesToRead > 0) {
message = port.ReadLine();
}
}
if (port.BytesToRead > 0) {
port.ReadExisting();
}
} catch (Exception e) {
// swallow read timeout exceptions
if (e.GetType() == typeof(TimeoutException))
return message;
else
throw;
}
return message;
}
示例6: checkPort
public void checkPort(int player)
{
switch (player)
{
case 1:
if (portStr.Contains(player1.text))
{
stream1 = new SerialPort(player1.text, 115200);
stream1.Open();
stream1.Write("v");
string version = stream1.ReadLine();
if (version == "")
{
accept1.sprite = no;
GameData.Instance.Port1Open = 0;
}
else
{
accept1.sprite = yes;
GameData.Instance.Port1Open = 1;
GameData.Instance.Port1 = player1.text;
}
}
else
{
accept1.sprite = no;
GameData.Instance.Port1Open = 0;
}
break;
case 2:
if (portStr.Contains(player2.text))
{
stream2 = new SerialPort(player2.text, 115200);
stream2.Open();
stream2.Write("v");
string version = stream2.ReadLine();
if (version == "")
{
accept2.sprite = no;
GameData.Instance.Port2Open = 0;
}
else
{
accept2.sprite = yes;
GameData.Instance.Port2Open = 1;
GameData.Instance.Port2 = player2.text;
}
}
else
{
accept2.sprite = no;
GameData.Instance.Port2Open = 0;
}
break;
case 3:
if (portStr.Contains(player3.text))
{
stream3 = new SerialPort(player3.text, 115200);
stream3.Open();
stream3.Write("v");
string version = stream3.ReadLine();
if (version == "")
{
accept3.sprite = no;
GameData.Instance.Port3Open = 0;
}
else
{
accept3.sprite = yes;
GameData.Instance.Port3Open = 1;
GameData.Instance.Port3 = player3.text;
}
}
else
{
accept3.sprite = no;
GameData.Instance.Port3Open = 0;
}
break;
}
}
示例7: checkPorts
public void checkPorts()
{
SerialPort stream1 = new SerialPort(GameData.Instance.Port1, 115200);
SerialPort stream2 = new SerialPort(GameData.Instance.Port2, 115200);
SerialPort stream3 = new SerialPort(GameData.Instance.Port3, 115200);
if (GameData.Instance.Port1Open == 1)
{
stream1.Open();
}
if (GameData.Instance.Port2Open == 1)
{
stream2.Open();
}
if (GameData.Instance.Port3Open == 1)
{
stream3.Open();
}
if (stream1.IsOpen)
{
stream1.Write("v");
string version1 = stream1.ReadLine();
if (version1 == "")
{
p1 = false;
}
else
{
p1 = true;
}
}
if (stream2.IsOpen)
{
stream2.Write("v");
string version2 = stream2.ReadLine();
if (version2 == "")
{
p2 = false;
}
else
{
p2 = true;
}
}
if (stream3.IsOpen)
{
stream3.Write("v");
string version3 = stream3.ReadLine();
if (version3 == "")
{
p3 = false;
}
else
{
p3 = true;
}
}
int p = 0;
if (p1)
{
player1 = stream1;
p++;
}
if (p2)
{
p++;
}
if (p3)
{
p++;
}
print("Players: " + p + ", " + p1);
if (p == 0)
{
print("ERROR");
}
if (p != playerNumber)
{
initCamera(p);
playerNumber = p;
}
}