1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| public partial class Form1 : Form
{
SerialPort _liaison;
string texte;
public Form1()
{
InitializeComponent();
_liaison = new SerialPort();
_liaison.PortName = "COM1";
_liaison.BaudRate = 115000;//Vitesse de dialogue avec le Millenium.
_liaison.Parity = Parity.Even;//Parité paire (fixé par le Millenium).
_liaison.DataBits = 7;//Format de la trame 7 bits (lui aussi fixé par le Millenium).
_liaison.StopBits = StopBits.One;////1 bit de Stop(fixé par le Millenium).
_liaison.Handshake = Handshake.RequestToSend;
_liaison.DtrEnable = true;
_liaison.RtsEnable = true;
_liaison.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
_liaison.Open();
}
private void bt_lecture_Click(object sender, EventArgs e)
{
if (_liaison.IsOpen == false)
{
_liaison.Open();
}
textBox1.Text = _liaison.ReadExisting();//.ToString();
}
void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
textBox1.Text = _liaison.ReadExisting();
this.Invoke(new EventHandler(DoUpdate));
}
private void DoUpdate(object s, EventArgs e)
{
textBox1.Text = _liaison.ReadExisting();
}
private void button_ecriture_Click(object sender, EventArgs e)
{
try
{
if (_liaison.IsOpen == false)
{
_liaison.Open();
}
//Ecriture sur le port serie.
_liaison.Write("test");
}
catch (TimeoutException) { }
}
} |
Partager