private void btn_openserial_Click(object sender, EventArgs e) { IntPtr serialport = new IntPtr(); IntPtr command = Marshal.StringToHGlobalAnsi(Properties.Settings.Default.OpenSerial); if (cb_serialport.SelectedItem != null) { serialport = Marshal.StringToHGlobalAnsi(cb_serialport.SelectedItem.ToString()); UserNativeFunction.DispatchCommand(command, serialport, 0); } else { MessageBox.Show("请选择一个串口~~", "错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); UserNativeFunction.DispatchCommand(Marshal.StringToHGlobalAnsi("OPEN"), Marshal.StringToHGlobalAnsi("COM0"), 0); } }
private void BTN_OPEN_SERIAL_PORT_Click(object sender, EventArgs e) { IntPtr serialport = new IntPtr(); IntPtr command = Marshal.StringToHGlobalAnsi(Properties.Settings.Default.OpenSerial); int arg = 0; if (CB_CHOSE_SERIAL_PORT.SelectedItem != null) { serialport = Marshal.StringToHGlobalAnsi(CB_CHOSE_SERIAL_PORT.SelectedItem.ToString()); if (CB_BAUD.SelectedItem == null) { CB_BAUD.SelectedIndex = 5; } arg |= CB_BAUD.SelectedIndex; arg <<= 8; if (CB_CHECK.SelectedItem == null) { CB_CHECK.SelectedIndex = 2; } arg |= CB_CHECK.SelectedIndex; arg <<= 8; if (CB_FLOW_CONTROL.SelectedItem == null) { CB_FLOW_CONTROL.SelectedIndex = 0; } arg |= CB_FLOW_CONTROL.SelectedIndex; if (UserNativeFunction.DispatchCommand(command, serialport, arg) == 1) { CB_BAUD.Enabled = false; CB_CHECK.Enabled = false; CB_CHOSE_SERIAL_PORT.Enabled = false; CB_FLOW_CONTROL.Enabled = false; BTN_OPEN_SERIAL_PORT.Enabled = false; BTN_CLOSE_SERIAL_PORT.Enabled = true; STATUS_SERIAL_PORT.Text = "串口打开成功"; } else { STATUS_SERIAL_PORT.Text = "串口打开失败"; } } else { MessageBox.Show("请选择一个串口~~", "错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); } }
private void BTN_CLOSE_SERIAL_PORT_Click(object sender, EventArgs e) { IntPtr command = Marshal.StringToHGlobalAnsi(Properties.Settings.Default.CloseSerial); if (UserNativeFunction.DispatchCommand(command, IntPtr.Zero, 0) == 1) { CB_BAUD.Enabled = true; CB_CHECK.Enabled = true; CB_CHOSE_SERIAL_PORT.Enabled = true; CB_FLOW_CONTROL.Enabled = true; BTN_OPEN_SERIAL_PORT.Enabled = true; BTN_CLOSE_SERIAL_PORT.Enabled = false; STATUS_SERIAL_PORT.Text = "串口关闭成功"; } else { STATUS_SERIAL_PORT.Text = "串口关闭失败"; } }