Здравствуйте. Хочу написать программу для кпк с использованием bluetooth. Может у кого нибудь есть исходники или сами помогут?Буду благодарен любой помощи
Спасибо
roboforum.ruТехнический форум по робототехнике. |
|
|
try
{
if (!port.IsOpen && "" != port.PortName) port.Open();
}
catch (InvalidOperationException)
{
//The specified port is open.
}
catch (ArgumentOutOfRangeException)
{
//One or more of the properties for this instance are invalid.
//For example, the Parity, DataBits, or Handshake properties are not valid values;
//the BaudRate is less than or equal to zero;
//the ReadTimeout or WriteTimeout property is less than zero and is not InfiniteTimeout.
}
catch (ArgumentException)
{
//The port name does not begin with "COM".
//- or -
//The file type of the port is not supported.
}
catch (IOException)
{
//The port is in an invalid state.
//- or -
//An attempt to set the state of the underlying port failed. For example, the parameters passed from this SerialPort object were invalid.
}
catch (UnauthorizedAccessException)
{
//Access is denied to the port.
}
finally { //... }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace IrDa
{
public partial class Form1 : Form
{
System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort();
public Form1()
{
InitializeComponent();
button1.Enabled = false;
try
{
Cursor.Current = Cursors.WaitCursor;
sp.PortName = "COM6";
sp.BaudRate = 115200;
sp.Parity = System.IO.Ports.Parity.None;
sp.StopBits = System.IO.Ports.StopBits.One;
sp.DataBits = 8;
sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(sp_DataReceived);
sp.Open();
button1.Enabled = true;
Cursor.Current = Cursors.Default;
}
catch (Exception ex)
{
if (sp.IsOpen) { sp.Close(); }
MessageBox.Show(ex.Message);
}
}
void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
label1.Text = sp.ReadExisting();
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
if (sp.IsOpen) { sp.Close(); }
}
private void button1_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
sp.WriteLine("hello");
Cursor.Current = Cursors.Default;
}
}
}
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 2