本文整理汇总了C#中SerialPort.ReadByte方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.ReadByte方法的具体用法?C# SerialPort.ReadByte怎么用?C# SerialPort.ReadByte使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SerialPort
的用法示例。
在下文中一共展示了SerialPort.ReadByte方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: runProgram
public static void runProgram(SerialPort port)
{
//System.IO.Ports.SerialPort serialPort1;
//System.ComponentModel.IContainer components = new System.ComponentModel.Container();
//serialPort1 = new System.IO.Ports.SerialPort(components);
//serialPort1.PortName = "COM4";
//serialPort1.BaudRate = 9600;
port.Open();
// this turns on !
// serialPort1.DtrEnable = true;
Console.Write("starting loop");
while (true)
{
//port.Write(new byte[] { 1 }, 0, 1);
//System.Threading.Thread.Sleep(1000);
//port.Write(new byte[] { 0 }, 0, 1);
//System.Threading.Thread.Sleep(1000);
Transmit(port, 63, 63, 40, 63);
//wait for received
while (port.BytesToRead==0)
{
System.Threading.Thread.Sleep(10);
}
if (port.ReadByte() != 103)
{
Console.Write("Bad ack");
break;
}
}
}
示例2: handleRs232c
private returnType handleRs232c(ref SerialPort mySP, ref UdpClient client, string fromUdp, int fromPort)
{
// UDP to RS-232C
try {
if (fromUdp.Length > 0) {
mySP.Write (fromUdp);
fromUdp = "";
}
}
catch (System.Exception) {
}
// RS-232C to UDP
try {
byte rcv = (byte)mySP.ReadByte();
if (rcv != 255) {
char tmp = (char)rcv;
byte [] data = System.Text.Encoding.ASCII.GetBytes(tmp.ToString());
client.Send(data, data.Length, ipadr1, fromPort);
return returnType.Continue;
}
} catch (System.Exception) {
}
return returnType.FallThrough;
}
示例3: Main
static void Main()
{
try
{
for (;;)
{
string port = null;
SerialPort s = null;
{
var pp = new List<string>();
for (int p = 0; p < 10; ++p)
pp.Add(string.Format("/dev/ttyACM{0}", p));
for (int p = 0; p < 10; ++p)
pp.Add(string.Format("/dev/ttyUSB{0}", p));
bool ok = false;
do
{
for (int p = 0; p < pp.Count; ++p)
{
try
{
port = pp[p];
s = new SerialPort(port);
//s.BaudRate = 19200;
//s.BaudRate = 115200;
s.BaudRate = 38400;
s.DataBits = 8;
s.Parity = Parity.None;
s.StopBits = StopBits.One;
s.Handshake = Handshake.None;
s.DtrEnable = false; // cosi non resetta
s.ReadTimeout = -1;
s.Open();
ok = true;
break;
}
catch (Exception)
{
}
}
}
while(ok == false);
}
Console.WriteLine("Port open on {0}", port);
for (;;)
{
int b = s.ReadByte();
if (b == '7')
{
var n = (DateTime.Now - new DateTime (1970, 1, 1)).TotalSeconds;
var str = String.Format("T{0} ", n);
s.Write(str);
Console.WriteLine("Sync done");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Task.Run error. {0}", ex);
}
}
示例4: leerBloque
public static int[] leerBloque(SerialPort puerto, int n)
{
int[] bloque = new int[n];
for (int i = 0; i < bloque.Length; i++)
bloque[i] = puerto.ReadByte();
return bloque;
}
示例5: leer
public static int leer(SerialPort puerto, string debug)
{
int i = puerto.ReadByte();
string i_str = String.Format("{0,10:G} {0,10:X}", i);
Debug.Log(debug + i_str);
return i;
}
示例6: rcvAndEcho
private bool rcvAndEcho(ref SerialPort mySP)
{
byte rcv;
char tmp;
bool hasRcvd = false;
try {
rcv = (byte)mySP.ReadByte();
if (rcv != 255) {
hasRcvd = true;
tmp = (char)rcv;
if (tmp != 0x0d && tmp != 0x0a) { // not CRLF
accRcvd = accRcvd + tmp.ToString();
}
if (tmp == 0x0d) { // CR
mySP.WriteLine(accRcvd);
rcvdCRLF = true;
}
}
} catch (System.Exception) {
}
return hasRcvd;
}