本文整理汇总了C#中System.IO.Ports.SerialPort.ReadChar方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.ReadChar方法的具体用法?C# SerialPort.ReadChar怎么用?C# SerialPort.ReadChar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Ports.SerialPort
的用法示例。
在下文中一共展示了SerialPort.ReadChar方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WritePage
public static void WritePage(byte[] pageData, int pageNumber, int pageSize, SerialPort port)
{
port.Write(new byte[1]{Convert.ToByte(pageNumber)},0,1);
port.Write(pageData,pageNumber*pageSize,pageSize);
if(port.ReadChar()!='A')
throw new Exception("No acknowledge from target!");
}
示例2: SearchPort
public static AsyncSerial SearchPort(string write, char search, int baud=9600)
{
foreach (string port in SerialPort.GetPortNames())
{
SerialPort temp;
try
{
temp = new SerialPort(port, baud);
try
{
temp.Open();
temp.ReadTimeout = 2500;
temp.Write(write);
var v = temp.ReadChar();
MainForm.Instance.logControl.Add(v.ToString(), LogEntryType.Error);
if (v == search)
{
temp.Close();
return new AsyncSerial(port);
}
}
catch (Exception ex)
{}
finally
{
temp.Close();
}
}
catch (Exception ex)
{}
}
throw new Exception("Port not found.");
}
示例3: DetectSerialCom
private bool DetectSerialCom()
{
foreach (var portName in SerialPort.GetPortNames())
{
_serialPort = new SerialPort(portName, 38400, Parity.None, 8, StopBits.One) { ReadTimeout = 10 };
try
{
_serialPort.Open();
}
catch (IOException)
{
continue;
}
try
{
_serialPort.Write("T");
int read = _serialPort.ReadChar();
if ((char)read == 'T')
return true;
}
catch (Exception)
{
continue;
}
}
return false;
}
示例4: InternalConditions
InternalConditions()
{
//verify available ports through printout
string[] ports = SerialPort.GetPortNames();
foreach (string s in ports)
{
System.Console.WriteLine(s);
}
weatherPort = new SerialPort("COM7", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
weatherPort.ReadTimeout = 500;
weatherPort.Close();
weatherPort.Open();
if (weatherPort.IsOpen)
{
System.Console.WriteLine("Port open!");
char[] readStream = new char[255];
byte commaCount = 0;
int availableBytes;
while (true)
{
/* System.Threading.Thread.Sleep(500);//give the board long enough to poll data
availableBytes = Math.Min(255,weatherPort.BytesToRead);
weatherPort.Read(readStream, 0, availableBytes);
for (int i = 0; i < availableBytes; i++)
{
System.Console.Write(readStream[i]);
}
System.Console.WriteLine();*/
try
{
System.Console.Write(weatherPort.ReadChar());
}
catch (TimeoutException)
{
System.Console.WriteLine("Timed out.");
}
//System.Console.WriteLine("Data: " + weatherPort.ReadExisting());
}
}
else
{
System.Console.WriteLine("Port not open.");
}
System.Console.ReadLine();
}
示例5: Main
public static void Main(string[] args)
{
SerialPort sp = new SerialPort("/dev/ttyACM0", 115200, Parity.None, 8, StopBits.One);
sp.Open();
//byte buff[];
int input;
for (int index = 0; index < 10; index++)
{
//string result = string.Format("{0} Testing", index);
//sp.Write(result);
input = sp.ReadChar();
Console.Write("Recieved: ");
Console.WriteLine(input);
}
sp.Close();
}
示例6: SendSerialCommand
public static String SendSerialCommand(SerialCommand cmd)
{
System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(cmd.Command));
System.Diagnostics.Debug.Assert(cmd.SerialPort != 0);
using (SerialPort serialPort = new SerialPort(cmd.SerialPortString, cmd.BaudRate, cmd.Parity, cmd.DataBits, cmd.StopBits))
{
try
{
serialPort.ReadTimeout = 1000;
serialPort.Open();
if (cmd.Hex)
{
byte[] buffer = HexStringToBytes(cmd.Command);
serialPort.Write(buffer, 0, buffer.Length);
}
else
serialPort.WriteLine(cmd.Command + "\r"); // Add a carriage return because most devices want one
string actualResponse = String.Empty;
foreach (char c in cmd.ExpectedResponse)
{
char rcvdChar = (char)serialPort.ReadChar();
actualResponse += rcvdChar;
if (rcvdChar != c)
{
actualResponse += serialPort.ReadExisting();
return actualResponse + "!=" + cmd.ExpectedResponse;
}
}
//System.Threading.Thread.Sleep(cmd.SleepTime);
return actualResponse;
}
finally
{
serialPort.Close();
}
}
}
示例7: Main
public static void Main()
{
string[] porte = GetPortNames();
foreach(string porta in porte)
{
Console.WriteLine(porta);
}
// initialize the serial port
serial = new SerialPort(porte[0], 115200, Parity.None, 8, StopBits.One);
// open the serial-port, so we can send & receive data
serial.Open();
// add an event-handler for handling incoming data
//serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);
while(true)
{
Console.WriteLine("Stringa da spedire, poi Enter");
string toSend = Console.ReadLine();
// send the same byte back
serial.Write(toSend);
// create a single byte array
byte[] bytes = new byte[1];
string str = "";
// as long as there is data waiting to be read
while (serial.BytesToRead > 0)
{
// read a single byte
//serial.Read(bytes, 0, bytes.Length);
char ch = (char) serial.ReadChar();
str += ch;
}
Console.Write(str);
}
}
示例8: StartCounterThread
private void StartCounterThread()
{
SerialPort ComPort = new SerialPort(selectedPortName);
ComPort.Open();
using (System.IO.StreamWriter fs = System.IO.File.AppendText(this.fileName.Text))
{
fs.WriteLine(DateTime.Now);
fs.Close();
}
while (durationTimer.Enabled)
{
int num = (int)ComPort.ReadChar();
if ((num==48)|| (num==49))
{ counts++;}
}
ComPort.Close();
}
示例9: CommandCompleted
private static bool CommandCompleted(SerialPort serialPort)
{
serialPort.Write("Q\r");
char result = (char)serialPort.ReadChar();
Console.WriteLine(result);
if (result == '.')
{
return true;
}
else
{
return false;
}
}
示例10: EnsurePortOpen
private bool EnsurePortOpen()
{
try
{
if (m_Port != null && m_Port.IsOpen)
return true;
lock (m_Lock)
{
// Re-check after acquiring lock
if (m_Port != null && m_Port.IsOpen)
return true;
// Get rid of any old closed ports
if (m_Port != null)
m_Port.Dispose();
m_Port = new SerialPort(m_PortName);
m_Port.BaudRate = 1200;
m_Port.DtrEnable = true;
m_Port.Open();
// A quick echo test to make sure it exists
m_Port.DiscardInBuffer();
m_Port.Write(new byte[] { 0x00, 0x04, 0x12 }, 0, 3);
int echo = m_Port.ReadChar();
if (echo != 0x12)
return false; // Something went wrong opening the port - not a WinKey?
// Open host mode
m_Port.Write(new byte[] { 0x00, 0x02 }, 0, 2);
int version = m_Port.ReadChar();
if (version < 20 || version > 30)
return false; // Weird version code - not a WinKey?
m_Port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
// Use the speed pot value
m_Port.Write(new byte[] { 0x02, 0x00 }, 0, 2);
return true;
}
}
catch
{
return false;
}
}
示例11: StartCounterThread
private void StartCounterThread()
{
SerialPort ComPort = new SerialPort(selectedPortName);
ComPort.Open();
using (System.IO.StreamWriter fs = System.IO.File.AppendText(this.fileName.Text))
{
// fs.WriteLine("StartTime: {0:O} EndTime{1:O}", num, time);
fs.AutoFlush = false;
while (durationTimer.Enabled)
{
char num = (char) ComPort.ReadChar();
DateTime time = DateTime.Now;
fs.WriteLine("{0} {1:O}", num, time);
AddText($"{num} {time:O} \n");
}
fs.Close();
}
ComPort.Close();
}
示例12: SearchPort
public static AsyncSerial SearchPort(string write, char search, int baud = 9600)
{
foreach (string port in SerialPort.GetPortNames())
{
SerialPort temp;
try
{
temp = new SerialPort(port, baud);
try
{
temp.Open();
temp.ReadTimeout = 2500;
temp.Write(write);
var v = temp.ReadChar();
if (v == search)
{
temp.Close();
return new AsyncSerial(port);
}
}
catch
{ }
finally
{
temp.Close();
}
}
catch
{ }
}
throw new Exception("Port not found.");
}