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;
using System.IO.Ports;
namespace Test
{
public partial class SerialPort : Form
{
//static SerialPort USB_RS235;
//byte port_open = 0;
//SerialPort port = new SerialPort();
public string PortName;
public string Parity;
public string Databits;
public string Baudrate;
public string Stopbits;
//public string Name;
public SerialPort()
{
InitializeComponent();
//USB_RS235 = new SerialPort();
}
private void btncancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void SerialPort_Load(object sender, EventArgs e)
{
SerialPort port = new SerialPort();
//Reading portnames
this.cbComport.Items.Clear();
string[] ports = System.IO.Ports.SerialPort.GetPortNames();
foreach (string port in ports)
{
this.cbComport.Items.Add(port);
}
//cbComport.Text = "Select Port";
this.cbComport.SelectedIndex = 0;
// //Reading parity
this.cbparity.Items.Clear();
Parity[] parities = (Parity[])Enum.GetValues(typeof(Parity));
foreach (Parity parity in parities)
{
this.cbparity.Items.Add(parity.ToString());
}
this.cbparity.SelectedIndex = 0;
//Reading stopbits
this.cbStopbits.Items.Clear();
StopBits[] stopbit = (StopBits[])Enum.GetValues(typeof(StopBits));
foreach (StopBits stopbits in stopbit)
{
this.cbStopbits.Items.Add(stopbits.ToString());
}
this.cbStopbits.SelectedIndex = 0;
//this.cbDatabits.SelectedIndex = 0;
//this.cbBaudrate.SelectedIndex = 0;
}
public bool OpenPort()
{
if (port.open = true)
port.Close();
port.PortName = cbComport.Text;
port.Parity = cbparity.Text;
port.Databits = cbDatabits.Text;
port.Baudrate = cbBaudrate.Text;
port.Stopbits = cbStopbits.Text;
port.open();
//private void cbComport_SelectedIndexChanged(object sender, EventArgs e)
//{
// //if (port_open != 0)
// //{
// // USB_RS235.Close();
// // port_open = 0;
// //}
// port.Close();
// port.PortName = cbComport.Text;
// port.Parity = cbparity.Text
// USB_RS235.PortName = cbComport.Text;
// USB_RS235.Parity = cbparity.Text;
// USB_RS235.DataBits = cbDatabits.Text;
// USB_RS235.BaudRate = cbBaudrate.Text;
// USB_RS235.StopBits = cbStopbits.Text;
// USB_RS235.Name = textBox1.Text;
}
private void btnok_Click(object sender, EventArgs e)
{
byte[] buff = new byte[6];
port.open();
buff[0] = 0;
buff[1] = 1;
buff[2] = 2;
buff[3] = 3;
buff[4] = 4;
buff[5] = 5;
MessageBox.Text = string.Format("Data returned={0},{1},{2},{3}", buff[0], buff[1], buff[2], buff[3]);
}
}
}
** Edit **

New Topic/Question
Reply




MultiQuote




|