本文整理汇总了C#中System.IO.Ports.SerialPort.ReadTo方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.ReadTo方法的具体用法?C# SerialPort.ReadTo怎么用?C# SerialPort.ReadTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Ports.SerialPort
的用法示例。
在下文中一共展示了SerialPort.ReadTo方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadInitialization
// TODO постараться вынести инициализацию в метод Read
private InitializationData ReadInitialization(SerialPort readport)
{
while (true)
{
try
{
readport.ReadTo("\xAA\x55");
// Считывание данных для создания пакета
// Здесь важен строгий порядок считывания байтов, точно как в пакете.
byte recipient = (byte)readport.ReadByte();
byte sender = (byte)readport.ReadByte();
ushort dataLenght = BitConverter.ToUInt16(
new byte[] { (byte)readport.ReadByte(), (byte)readport.ReadByte() }, 0);
byte option1 = (byte)readport.ReadByte();
byte option2 = (byte)readport.ReadByte();
ushort crc = BitConverter.ToUInt16(
new byte[] { (byte)readport.ReadByte(), (byte)readport.ReadByte() }, 0);
// Счетчик количества итерация цикла while
int count = 0;
while (readport.BytesToRead < dataLenght)
{
count++;
Thread.Sleep(_sleepTime);
if (count > dataLenght)
{
break;
}
}
byte[] data = new byte[dataLenght];
readport.Read(data, 0, dataLenght);
var packet = new Packet.Packet(new Header(recipient, sender, option1, option2), data);
// Проверка crc и id клиента, id отправителя и типа пакета
if (packet.Header.Crc == crc && packet.Header.Recipient == ClietnId && packet.Header.Sender == 0 && packet.Data.Type == DataType.Initialization)
{
SendAcknowledge(packet);
var initialization = StructConvertor.FromBytes<InitializationData>(packet.Data.Content);
return initialization;
}
}
catch (InvalidOperationException)
{
IsPortAvailable(readport);
Thread.Sleep(3000);
}
catch (TimeoutException exception)
{
// TODO ограничить количество отправок запроса
SendInitializationRequest();
}
catch (Exception exception)
{
LogHelper.GetLogger<CommunicationUnit>().Error("Ошибка при чтение с порта", exception);
}
}
}
示例2: Read
public static String[] Read(SerialPort port)
{
long Inlatitude;
long Inlongitude;
string strLatitude = "";
string strLongitude = "";
double latitude;
double longitude;
int Heart_Rate;
bool flag_badData = false;
string Date_Time;
string[] words = {""};
string data = "";
do
{
flag_badData = false;
data = port.ReadTo("]");
words = data.Split(',');
if (words.Length != 4 || words[0].Length < 8 || words[1].Length < 10 || words[2].Length > 3)
{
flag_badData = true;
}
else
{
strLatitude = words[0].Remove(0, words[0].Length - 8);
strLongitude = words[1].Remove(0, words[1].Length - 10);
if (!IsAllDigits(strLatitude) || !IsAllDigits(strLongitude) || !IsAllDigits(words[2]))
{
flag_badData = true;
}
}
} while ( flag_badData || Int64.Parse(strLatitude) > 90000000 || Int64.Parse(strLatitude) < -90000000 || Int64.Parse(strLongitude) < -180000000 || Int64.Parse(strLongitude) > 180000000);
// lat = 90 long = 180
if (words[0] != null && words[1] != null)
{
try
{
Inlatitude = Int64.Parse(strLatitude);
latitude = (Inlatitude / 1000000.0);
Inlongitude = Int64.Parse(strLongitude);
longitude = (Inlongitude / 1000000.0);
words[0] = ("" + latitude);
words[1] = ("" + longitude);
//Location_ShowAll_Tab.Text = ("Lat:" + latitude + " Lon:" + longitude);
}
catch (FormatException)
{
// Location_ShowAll_Tab.Text = ("Format Issue!!!!");
latitude = 38.556868;
words[0] = "38.556868";
longitude = -121.358592;
words[1] = "-121.358592";
Read(port);
}
if (words[2] == null)
{
Heart_Rate = 0; //set default heart rate
words[2] = ("N/A");
// Heart_Rate_ShowAll_Tab.Text = ("Set to default");
}
else
{
try
{
Heart_Rate = Convert.ToInt32(words[2]); // Convert String into int
words[2] = ("" + Heart_Rate);
}
catch (FormatException)
{
words[2] = ("Format Error");
}
}
if (words[3] == null)
{
words[3] = ("Not available");
}
else
{
try
{
Date_Time = words[3]; // Keeps string as a string
words[3] = ("" + Date_Time);
}
catch (FormatException)
{
words[3] = ("Format Error");
}
}
}
else
{
//set default lat/ long - csus
latitude = 38.556868;
words[0] = "38.556868";
longitude = -121.358592;
words[1] = "-121.358592";
Read(port);
//.........这里部分代码省略.........