如何识别Rs232串口? [英] How to identify Rs232 serial port ?

查看:278
本文介绍了如何识别Rs232串口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我非常努力争取这个,我正在开发小型桌面应用程序,(即)RS232端口



我有三个端口是COM2,COM5和COM3,COM2和COM5是另一台机器,需要COM3机器,



增加了三个端口在combobox COM2,COM5,COM3订单。



我的问题是,如何在表格加载时选择默认我的机器(COM3)?



 私人  void  Form1_Load(< span class =code-keyword> object  sender,EventArgs e)
{



 //添加可用端口名称 选择组合框
portNameSelectionComboBox.Items.AddRange(SerialPort.GetPortNames());
portNameSelectionComboBox.SelectedItem = SerialPort.GetPortNames()[ 0 ];
//初始化第一个端口名称 默认
// devicePort = new SerialPort (SerialPort.GetPortNames()[ 0 ]);
devicePort = new SerialPort(portNameSelectionComboBox.SelectedItem.ToString());
devicePort.DataReceived + = new SerialDataReceivedEventHandler(postDataRecieved);
devicePort。打开();



}



先谢谢..

解决方案

通过调用 SerialPort.GetPortNames()



选择所需的端口,但 SerialPort 对此没有帮助。您需要让用户选择一个端口,或者向每个端口发送一些内容,看看哪个端口以您期望的方式响应。为其他软件正在使用的端口上的一些例外做好准备。



Properties.Settings.Default.RS232ConnectionName 。这样,您可以在软件中更改它并再次获取的OnLoad

Hi,

I am very Struggle for this one, I am developing small desktop application,(i.e)RS232 port

I have three port that is COM2,COM5 and COM3 , COM2 and COM5 are another machine, COM3 is needed machine,

Three port added in combobox COM2,COM5,COM3 order.

My question is , How do i select Default my machine(COM3)when form loading?

private void Form1_Load(object sender, EventArgs e)
          {


//Adding available port names to the selection combobox
            portNameSelectionComboBox.Items.AddRange(SerialPort.GetPortNames());
            portNameSelectionComboBox.SelectedItem = SerialPort.GetPortNames()[0];
            //initializing first port name by default
            //devicePort = new SerialPort(SerialPort.GetPortNames()[0]);
            devicePort = new SerialPort(portNameSelectionComboBox.SelectedItem.ToString());
            devicePort.DataReceived += new SerialDataReceivedEventHandler(postDataRecieved);
            devicePort.Open();


}

Thanks in Advance..

解决方案

Get all your serial ports by calling SerialPort.GetPortNames().

Select the port you want, but SerialPort won't help with that. You need to either let user select a port, or send something to every port and see which one responds in the way you expect it to. Be prepared for some exceptions on ports that are in use by other software.

Store selected port by its name in Properties.Settings.Default.RS232ConnectionName. That way, you can change it from within your software and get it again OnLoad.


这篇关于如何识别Rs232串口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆