本文整理汇总了C#中SerialPort类的典型用法代码示例。如果您正苦于以下问题:C# SerialPort类的具体用法?C# SerialPort怎么用?C# SerialPort使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SerialPort类属于命名空间,在下文中一共展示了SerialPort类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Chronopic
//-- Constructor
public Chronopic(SerialPort sp)
{
//-- Comprobar si puerto serie ya estaba abierto
if (sp != null)
if (sp.IsOpen)
sp.Close();
//-- Abrir puerto serie
sp.Open();
//-- Configurar timeout por defecto
//de momento no en windows (hasta que no encontremos por qué falla)
//OperatingSystem os = Environment.OSVersion;
//not used, now there's no .NET this was .NET related
//on mono timeouts work on windows and linux
//if( ! os.Platform.ToString().ToUpper().StartsWith("WIN"))
sp.ReadTimeout = DefaultTimeout;
//-- Guardar el puerto serie
this.sp = sp;
// //-- Vaciar buffer
// //done in a separate method
// this.flush();
}
示例2: CreateRtu
/// <summary>
/// Modbus RTU slave factory method.
/// </summary>
public static ModbusSerialSlave CreateRtu(byte unitId, SerialPort serialPort)
{
if (serialPort == null)
throw new ArgumentNullException("serialPort");
return CreateRtu(unitId, new SerialPortAdapter(serialPort));
}
示例3: Open
public void Open () {
// Opens the serial port
stream = new SerialPort(port, baudrate);
stream.ReadTimeout = 50;
stream.Open();
//this.stream.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
示例4: Start
// Use this for initialization
void Start()
{
//Screen.showCursor = false;
isPlaying = false;
//Health = 100;
isDead = false;
Health = 100;
_curHealth = Health;
PlArmIdentity = PlayerArm.transform.rotation;
Instantiate(Spells[0], PlayerArm.transform.GetChild(0).position, Quaternion.identity);
CastingSpell = GameObject.FindGameObjectWithTag("Spell");
string[] ports = SerialPort.GetPortNames();
// foreach (string port in ports)
// {
// Console.WriteLine(port);
// }
_serialPort = new SerialPort(ports[1], 9600);
_serialPort.Open();
Debug.Log(_serialPort.PortName);
Debug.Log(_serialPort.BaudRate);
Debug.Log(_serialPort.IsOpen);
}
示例5: spInit
private void spInit()
{
sp = new SerialPort();
sp.PortName = "COM40";
sp.BaudRate = 57600;
sp.DataBits = 8;
sp.Parity = Parity.Even;
try
{
sp.Open();
try
{
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);//事件
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception)
{
MessageBox.Show("无法连接串口");
}
}
示例6: Start
// Use this for initialization
void Start()
{
Debug.Log(SerialPort.GetPortNames().ToString());
mySerialPort = new SerialPort("\\\\.\\COM18");
mySerialPort.BaudRate = 9600;
//mySerialPort.Parity = Parity.None;
//mySerialPort.StopBits = StopBits.One;
//mySerialPort.DataBits = 8;
//mySerialPort.Handshake = Handshake.None;
//mySerialPort.RtsEnable = true;
//mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
if ( mySerialPort != null )
{
if ( mySerialPort.IsOpen ) // close if already open
{
mySerialPort.Close();
Debug.Log ("Closed stream");
}
mySerialPort.Open();
Debug.Log ("Opened stream");
}
else
{
Debug.Log ("ERROR: Uninitialized stream");
}
myThread = new Thread(new ThreadStart(GetArduino));
myThread.Start();
}
示例7: 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;
}
}
}
示例8: Class_FeedbackStage
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="_sleep">モニタのスリープ時間</param>
public Class_FeedbackStage(string _stageName="")
{
stageName = _stageName + ": ";
serialPort = new SerialPort();
serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
}
示例9: GetMavStreamFromArgs
public static Stream GetMavStreamFromArgs(string[] args)
{
Stream strm = null;
if (args.Length == 0)
{
strm = Console.OpenStandardInput();
}
else if (args.Length == 1)
{
Console.WriteLine("Usage (todo)");
Console.ReadKey();
Environment.Exit(0);
}
else if (args[0] == "-S")
{
if (args.Length != 3)
{
Console.WriteLine("Usage (todo)");
Environment.Exit(1);
}
var comport = args[1];
var baud = Convert.ToInt32(args[2]);
var port = new SerialPort(comport, baud);
port.Open();
strm = port.BaseStream;
}
else
{
strm = File.OpenRead(args[1]);
}
return strm;
}
示例10: Start
//int leftPos=500;
// Start
void Start()
{
//start the serial connection.
if (gameObject.GetComponent<COMParser>() != null)
COMPort = gameObject.GetComponent<COMParser>().com;
else if (gameObject.transform.parent.GetComponent<COMParser>() != null)
COMPort = gameObject.transform.parent.GetComponent<COMParser>().com;
else
COMPort = gameObject.transform.parent.parent.GetComponent<COMParser>().com;
if (COMPort != "COM0")
{
_SerialPort = new SerialPort(COMPort, 9600);
OpenConnection();
//check how many outputs and inputs there are.
for (int pinconfigcounter = 0; pinconfigcounter < pinConfig.Length; pinconfigcounter++)
{
if (pinConfig[pinconfigcounter].Equals("o")) outputcount++;
if (pinConfig[pinconfigcounter].Equals("i")) inputcount++;
if (pinConfig[pinconfigcounter].Equals("p")) pwmcount++;
}
DigitalInputs = new int[inputcount];
FromJavaToArduino = new char[pinConfig.Length];
}
}// Start
示例11: Main
public static void Main()
{
Debug.Print("BLE Shield");
var port = SerialPorts.COM1; // using D0 & D1
//var port = SerialPorts.COM2; // using D2 & D3
var bleShield = new SerialPort(port, 19200, Parity.None, 8, StopBits.None);
bleShield.DataReceived += (sender, args) =>
{
var receiveBuffer = new byte[16];
int bytesReceived = bleShield.Read(receiveBuffer, 0, receiveBuffer.Length);
if (bytesReceived > 0)
{
Debug.Print("Bytes received: " + bytesReceived);
Debug.Print(new String(Encoding.UTF8.GetChars(receiveBuffer)));
}
};
bleShield.Open();
while (true)
{
var random = new Random();
var sendBuffer = new byte[4];
random.NextBytes(sendBuffer);
bleShield.Write(sendBuffer, 0, sendBuffer.Length);
Thread.Sleep(1000);
}
}
示例12: Start
// Use this for initialization
void Start()
{
nazwaPortu = COM.com;
// utworzenie obiektu do komunikacji na danym porcie
stm = new SerialPort(nazwaPortu, 38400, Parity.None, 8, StopBits.One);
// proba otwarcia portu
try
{
stm.Open();
if (!stm.IsOpen)
{
// jezeli nie udalo sie utworzyc portu to komponent jest likwidowany
Debug.LogWarning("Nie udalo sie otworzyc polaczenia z portem: " + nazwaPortu);
Destroy(this);
}
else
{
Debug.Log("Otwarto polaczenie z portem: " + nazwaPortu);
// udalo sie utworzyc polaczenie z portem - startuje watek do czytania danych z portu
stmThread = new Thread(stmOdczyt);
stmThread.Start();
}
}
catch (Exception e)
{
// jezeli nie udalo sie utworzyc portu to komponent jest likwidowany
Debug.LogWarning("Nie udalo sie otworzyc polaczenia z portem: " + nazwaPortu);
Destroy(this);
}
}
示例13: TrainDrukteForm
public TrainDrukteForm()
{
InitializeComponent();
timer1.Start();
_serialPort = new SerialPort();
}
示例14: Start
// Use this for initialization
void Start () {
serialPort = new SerialPort ("/dev/cu.usbmodem1421", 57600);
serialPort.Open ();
serialPort.ReadTimeout = 1;
}
示例15: Start
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
stream = new SerialPort("COM4", 9600);
stream.ReadTimeout = 50;
stream.Open();
}