static void Main() { UserNativeFunction.InitializeComponent(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Main()); UserNativeFunction.ReleaseComponent(); }
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 = "串口关闭失败"; } }
public Form1() { InitializeComponent(); // Initialize the serial port list IntPtr lpCount = Marshal.AllocCoTaskMem(sizeof(uint)); IntPtr lpPortList = UserNativeFunction.SerialPort_Proc(SPM_GETPORTNAMES, lpCount, IntPtr.Zero); int count = Marshal.ReadInt32(lpCount); if (count != 0) { IntPtr com = lpPortList; for (uint i = 0; i < count; i++) { cb_serialport.Items.Add(Marshal.PtrToStringAuto(Marshal.ReadIntPtr(com))); com = (IntPtr)(com.ToInt64() + Marshal.SizeOf(com)); } cb_serialport.SelectedItem = 0; } }
public Main() { InitializeComponent(); BTN_CLOSE_SERIAL_PORT.Enabled = false; // Initialize the serial port list IntPtr lpCount = Marshal.AllocCoTaskMem(sizeof(uint)); IntPtr lpPortList = UserNativeFunction.SerialPort_Proc(SPM_GETPORTNAMES, lpCount, IntPtr.Zero); int count = Marshal.ReadInt32(lpCount); if (count != 0) { IntPtr com = lpPortList; for (uint i = 0; i < count; i++) { CB_CHOSE_SERIAL_PORT.Items.Add(Marshal.PtrToStringAuto(Marshal.ReadIntPtr(com))); com = (IntPtr)(com.ToInt64() + Marshal.SizeOf(com)); } CB_CHOSE_SERIAL_PORT.SelectedItem = 0; } op.Height = tabPage1.Height; op.Width = tabPage1.Width - 210; op.Show(); }