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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace td5
{
public partial class Form1 : Form
{
const int MAXDATA = 512;
const byte TERMINATEUR = 0x0A;
public int giIndex ;
byte[] MyReceiveBuffer = new byte[MAXDATA];
public Form1()
{
InitializeComponent();
System.Windows.Forms.TextBox.CheckForIllegalCrossThreadCalls = false;
giIndex = 0 ;
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
if (serialPort1.IsOpen == false)
serialPort1.Open();
textBox3.AppendText("Programme demarré" + Environment.NewLine);
serialPort1.ReadExisting();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.Write(textBox1.Text);
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int iNbByteReceived;
string szCommande;
ASCIIEncoding ascii = new ASCIIEncoding();
iNbByteReceived = serialPort1.BytesToRead;
textBox3.AppendText("reception de " + iNbByteReceived.ToString() + "octets");
textBox3.AppendText(Environment.NewLine);
if (iNbByteReceived != 0)
{
serialPort1.Read(MyReceiveBuffer, giIndex, iNbByteReceived);
giIndex = giIndex + iNbByteReceived;
if (MyReceiveBuffer[giIndex - 1] == TERMINATEUR)
{
szCommande = ascii.GetString(MyReceiveBuffer, 0, giIndex - 2);
textBox2.Text = textBox2.Text + szCommande;
giIndex = 0;
//textBox2.AppendText("caractere CR reçu " + Environment.NewLine);
/* switch (MyReceiveBuffer.ToString())
{
case "\r" : textBox1.AppendText("\r<Executed/>"); break;
case "initlogging\r" : textBox1.AppendText("initlogging\rthis command will change the scan length and/or initialize logging\rrepeat the command to verify\r<Executed/>"); break;
case " startnow\r" : textBox1.AppendText(" "); break;
case "Stop\r" : textBox1.AppendText("Stop"); break;
}
*/
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (listBox1.SelectedItem.ToString())
{
case "300" : serialPort1.BaudRate = 300; break;
case "600" : serialPort1.BaudRate = 600; break;
case "1200" : serialPort1.BaudRate = 1200; break;
case "2400" : serialPort1.BaudRate = 2400; break;
case "4800" : serialPort1.BaudRate = 4800; break;
case "9600" : serialPort1.BaudRate = 9600; break;
default : serialPort1.BaudRate = 9600; break;
}
}
}
} |
Partager