Načítanie com portov na pc :
Na načítanie všetkých dostupných comportov vo windowse pouzijeme statickú metódu triedy SerialPort podľa nasledujúceho kódu:
SerialPort.GetPortNames();
Vytvorenie objektu a otvorenie portu :
Po vytvorení objectu priradíme nazov portu PortName ,prenosovú rýchlosť BaudRate, ďalej ešte nastavíme DataBits, Parity, StopBits . Nakoniec zavoláme metódu na otvorenie comportu Open() .
this.mySerialPort = new SerialPort(); this.mySerialPort.PortName = this.comboBox_ports.Text; this.mySerialPort.BaudRate = Convert.ToInt16(this.comboBox_baud.Text); this.mySerialPort.DataBits = Convert.ToInt16(this.comboBox_data.Text); this.mySerialPort.Parity = (Parity)Enum.Parse(typeof(Parity), this.comboBox_parity.Text); this.mySerialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), this.comboBox_stop.Text); this.mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived); this.mySerialPort.Open();
Posielanie dát :
private void sendData() { try { if (this.transMod == mod.ASCI) { this.mySerialPort.WriteLine(this.richTextBox_trans.Text); } if (this.transMod == mod.HEX) { byte[] data; int i = 0; string[] dataString = this.richTextBox_trans.Text.Split(new string[] { this.comboBox_sep.Text }, StringSplitOptions.RemoveEmptyEntries); data = new byte[dataString.Length]; foreach (string d1 in dataString) { data[i++] = Convert.ToByte(d1, 16); } this.mySerialPort.Write(data, 0, data.Length); } if (this.transMod == mod.DEC) { byte[] data; int i = 0; string[] dataString = this.richTextBox_trans.Text.Split(new string[] { this.comboBox_sep.Text }, StringSplitOptions.RemoveEmptyEntries); data = new byte[dataString.Length]; foreach (string d1 in dataString) { data[i++] = Convert.ToByte(d1, 10); } this.mySerialPort.Write(data, 0, data.Length); } if (this.transMod == mod.BINARY) { byte[] data; int i = 0; string[] dataString = this.richTextBox_trans.Text.Split(new string[] { this.comboBox_sep.Text }, StringSplitOptions.RemoveEmptyEntries); data = new byte[dataString.Length]; foreach (string d1 in dataString) { data[i++] = Convert.ToByte(d1, 2); } this.mySerialPort.Write(data, 0, data.Length); } } catch(Exception ex1) { MessageBox.Show(ex1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
Prijímanie dát :
void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { byte[] buffer = new byte[0]; while (mySerialPort.BytesToRead > 0) { buffer = new byte[mySerialPort.BytesToRead]; this.mySerialPort.Read(buffer, 0, mySerialPort.BytesToRead); } if (this.recMod == mod.ASCI) { this.prijateData += Encoding.ASCII.GetString(buffer); } if (this.recMod == mod.HEX) { foreach(byte s in buffer) { this.prijateData += s.ToString("X2") + this.separator; } } if (this.recMod == mod.DEC) { foreach (byte s in buffer) { this.prijateData += s.ToString() + this.separator; } } if (this.recMod == mod.BINARY) { foreach (byte s in buffer) { this.prijateData += Convert.ToString(s, 2).PadLeft(8,'0') + this.separator; } } // this.prijateData += Encoding.ASCII.GetString(buffer); this.SetControlPropertyValue(this.richTextBox_rec, "Text", this.prijateData); } catch (Exception ex1) { MessageBox.Show(ex1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
GUI programu :
V prílohe su na stiahnutie kompletné zdrojové kódy k program.
V ver.1.0.0.2 je pridaná možnosť odosielať CRC16 pre odoslané dáta.