本文整理汇总了C#中System.IO.Ports.SerialPort.Open方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.Open方法的具体用法?C# SerialPort.Open怎么用?C# SerialPort.Open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Ports.SerialPort
的用法示例。
在下文中一共展示了SerialPort.Open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SerialDevice
public SerialDevice(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits, string returnToken = "\n")
{
_dataReading = string.Empty;
_dataQueue = new ConcurrentQueue<string>();
_timeCounter = new Stopwatch();
_timeCounter.Start();
_COMPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
_returnToken = returnToken;
_COMPort.NewLine = _returnToken;
_COMPort.RtsEnable = true;
_COMPort.DtrEnable = true;
_COMPort.ReadTimeout = SerialPort.InfiniteTimeout;
_COMPort.WriteTimeout = SerialPort.InfiniteTimeout;
_COMPort.Open();
if (!_COMPort.IsOpen)
_COMPort.Open();
if (!_COMPort.IsOpen)
throw new Exception("Can't connect to the COM port!");
_serialThread = new Thread(new ThreadStart(GetSerialDataContinious));
_serialThread.Priority = ThreadPriority.Normal;
_serialThread.Name = string.Format("SerialHandle{0}", _serialThread.ManagedThreadId);
_communicatyionIsActive = true;
_serialThread.Start();
}
示例2: GetTemperature
public XmlElement GetTemperature(string port, string baudRate)
{
try
{
_serialPort = new SerialPort(port);
_serialPort.BaudRate = Convert.ToInt32(baudRate);
_serialPort.Parity = Parity.None;
_serialPort.StopBits = StopBits.One;
_serialPort.DataBits = 8;
_serialPort.Handshake = Handshake.None;
_serialPort.RtsEnable = true;
_serialPort.ReadTimeout = 1000;
if (_serialPort.IsOpen)
{
_serialPort.Close();
_serialPort.Open();
}
else
{
_serialPort.Open();
}
Thread.Sleep(1000);
count = _serialPort.BytesToRead;
if (count < 1)
{
throw new Exception("No Data to Read..."+count);
}
else
{
while (count > 0)
{
int byteData = _serialPort.ReadByte();
data = data + Convert.ToChar(byteData);
count--;
}
}
_serialPort.DiscardOutBuffer();
_serialPort.Close();
_result = GetXML(data.Trim());
}
catch (Exception ex)
{
if (_serialPort.IsOpen)
_serialPort.Close();
_result = GetExceptionXML(ex.ToString());
}
return _result;
}
示例3: ModbusSerialAsciiMasterReadRegisters
/// <summary>
/// Simple Modbus serial ASCII master read holding registers example.
/// </summary>
public static void ModbusSerialAsciiMasterReadRegisters()
{
using (SerialPort port = new SerialPort("COM1"))
{
// configure serial port
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
// create modbus master
IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);
byte slaveId = 1;
ushort startAddress = 1;
ushort numRegisters = 5;
// read five registers
ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);
for (int i = 0; i < numRegisters; i++)
Console.WriteLine("Register {0}={1}", startAddress + i, registers[i]);
}
// output:
// Register 1=0
// Register 2=0
// Register 3=0
// Register 4=0
// Register 5=0
}
示例4: Open
public void Open()
{
serial = new SerialPort(portName, boudRate);
serial.DataReceived += new SerialDataReceivedEventHandler(Serial_DataReceived);
serial.Open();
Write("Hello there.");
}
示例5: BalanceReader
/*
* public string ComPort = String.Empty;
public int BaudRate = 110;
public StopBits StopBits = StopBits.None;
public int DataBits = 7;
public Parity Parity = Parity.None;
* */
public BalanceReader(string comPort = "COM1", int baudRate = 9600, StopBits stopBits = StopBits.One,
int dataBits = 7, Parity parity = Parity.Even, string sicsCommand = "SI", bool rts = false)
{
_sicsCommand = sicsCommand;
if (comPort == String.Empty) comPort = "COM1";
_port = new SerialPort
{
PortName = comPort,
BaudRate = baudRate,
StopBits = stopBits,
DataBits = 7,
Parity = Parity.Even,
RtsEnable = rts
};
// from Page 39 of NewClassic Balances METTLER TOLEDO manual for MS-S / MS-L Models
//_port.Handshake = Handshake.XOnXOff;
if (_port.IsOpen == false)
{
try
{
_port.Open();
}
catch (Exception)
{
// port will not be open, therefore will become null
}
}
_port.DataReceived += _port_DataReceived;
}
示例6: Main
static void Main(string[] args)
{
string result;
int end = 0;
SerialPort sp = new SerialPort("COM2");
sp.BaudRate = 115200; // 56700;
sp.NewLine = "\r\n";
sp.Open();
sendCommand(sp, "AT+RST", eWaitFor.eOK);
Thread.Sleep(1000);
sendCommand(sp, "AT+CWJAP=\"\",\"\"", eWaitFor.eOK | eWaitFor.eFAIL);
Thread.Sleep(1000);
sendCommand(sp, "AT+CWMODE=1", eWaitFor.eOK | eWaitFor.eNoChange);
Thread.Sleep(1000);
//sendCommand(sp, "AT+CWJAP=\"CBA_F1\",\"abc\"", eWaitFor.eOK);
//sendCommand(sp, "AT+CWJAP=\"LumiaTk5276\",\"abc\"", eWaitFor.eOK);
sendCommand(sp, "AT+CWJAP=\"edup-rt\",\"abc\"", eWaitFor.eOK);
Thread.Sleep(1000);
Console.WriteLine("IP: " + getIP(sp));
sendCommand(sp, "AT+CIPMUX=1", eWaitFor.eOK);
sendCommand(sp, "AT+CIPSTART=4,\"TCP\",\"google.com\",80", eWaitFor.eOK);
sendCommand(sp, "AT+CIPSEND=4,18", eWaitFor.eGT);
sp.Write("GET / HTTP/1.0\r\n\r\n");
Thread.Sleep(5000); //Czekamy tu na internet
result = sp.ReadExisting();
Console.WriteLine(result);
//sp.WriteLine("AT+CIFSR");
//result = sp.ReadTo("OK\r");
sp.Close();
}
示例7: 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();
}
示例8: LCD43
public LCD43()
{
comLcdPri = new SerialPort(SerialPorts.COM1, 115200, Parity.None, 8, StopBits.One);
comLcdPri.DataReceived += new SerialDataReceivedEventHandler(Lcd_DataReceived);
comLcdPri.ReadTimeout = 100;
comLcdPri.Open();
}
示例9: Start
public void Start(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
{
if (!GetAvailablePorts().Contains(portName))
{
throw new Exception(string.Format("Unknown serial port: {0}", portName));
}
// Start the timer to empty the receive buffer (in case data smaller than MAX_RECEIVE_BUFFER is received)
_bufferTimer = new Timer();
_bufferTimer.Interval = BUFFER_TIMER_INTERVAL;
_bufferTimer.Elapsed += _bufferTimer_Elapsed;
_bufferTimer.Start();
// Instantiate new serial port communication
_serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
// Open serial port communication
_serialPort.Open();
// Check that it is actually open
if (!_serialPort.IsOpen)
{
throw new Exception(string.Format("Could not open serial port: {0}", portName));
}
_serialPort.ReadTimeout = 100; // Milliseconds
_readThread = new System.Threading.Thread(ReadThread);
_readThreadRunning = true;
_readThread.Start();
}
示例10: PortModel
public PortModel(string portName)
{
_serialPort = new SerialPort {PortName = portName};
_serialPort.Open();
_serialPort.ReadTimeout = _readTimeout;
_serialPort.WriteTimeout = _writeTimeout;
}
示例11: start
// Start Comms
private void start() {
clearcontainers();
arduino = new SerialPort(port, 9600);
arduino.DataReceived += new SerialDataReceivedEventHandler(receivedTag);
arduino.Open();
STATUS = DEVICE_STATE_LISTENING;
}
示例12: Connect
public void Connect(string portName, int baudRate = 115200)
{
try
{
serialPort = new SerialPort(portName);
serialPort.BaudRate = baudRate;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
serialPort.DataBits = 8;
serialPort.DataReceived += serialPort_DataReceived;
serialPort.Open();
isConnected = true;
DebugPortState(String.Format("Connected to port {0}.", portName));
if (OnConnectedEvent != null)
OnConnectedEvent(this,null);
}
catch (Exception ex)
{
DebugPortState(String.Format("Can`t connect to port {0}.", portName));
if (OnConnectingError != null)
OnConnectingError(ex);
}
}
示例13: SerialConnect
// Connect to transmitter via serial port
public void SerialConnect()
{
if (BaudRate != 0 && PortName != "None")
{
port = new SerialPort(PortName, BaudRate);
port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
try
{
port.Open();
}
catch
{ //todo
}
if (port.IsOpen)
{
Console.WriteLine(PortName + " is open");
}
else
{
Console.WriteLine("Unable to open " + PortName);
}
}
else
{
Console.WriteLine("Port name or baud rate not set prior to opening connection");
}
}
示例14: MidiDriver
public MidiDriver(Cpu.Pin resetPin, string serialPortName = Serial.COM2)
{
_resetPinPort = new OutputPort(resetPin, false);
_serialPort = new SerialPort(serialPortName, 31250, Parity.None, 8, StopBits.One);
_serialPort.Open();
}
示例15: Serial2Matlab
// public Serial2Matlab(string port, int baudRate = 115200, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One)
public Serial2Matlab()
{
serialPort = new SerialPort("COM43", 115200, Parity.None, 8, StopBits.One);
serialPort.ReadTimeout = 2;
serialPort.Open();
serialPort.DiscardInBuffer();
}