private bool OpenSerial() { try { int portNum = (int)_readerProfile.ComPort; FrmPortIndex = 0; string strException = string.Empty; fBaud = Convert.ToByte(3); if (fBaud > 2) { fBaud = Convert.ToByte(fBaud + 2); } fComAdr = 255; //Broadcast address to open the device fCmdRet = RWDev.OpenComPort(portNum, ref fComAdr, fBaud, ref FrmPortIndex); if (fCmdRet != 0) { string strLog = "Connect reader failed: " + GetReturnCodeDesc(fCmdRet); // WriteLog(lrtxtLog, strLog, 1); return(false); } else { frmcomportindex = FrmPortIndex; fCmdRet = RWDev.SetRfPower(ref fComAdr, Convert.ToByte(_readerProfile.PowerDbm), frmcomportindex); string strLog = "Connect: "; // + ComboBox_COM.Text + "@" + ComboBox_baud2.Text; // WriteLog(lrtxtLog, strLog, 0); _readerConnected = true; return(true); } } catch (Exception e) { Console.WriteLine(e); MessageBox.Show("Serial Communication Error or Occupied - exception message " + e.Message); } return(false); }
private void buttonConnect_Click(object sender, EventArgs e) { int portNum = comboBoxCOM.SelectedIndex + 1; byte baudRate = Convert.ToByte(comboBoxBaud.SelectedIndex); if (baudRate > 2) { baudRate = Convert.ToByte(baudRate + 2); } int portHandle = 0; try { ErrorsCatcher.errorName = RWDev.OpenComPort(portNum, ref RWDev.comAddress, baudRate, ref portHandle); if (ErrorsCatcher.errorName != 0) { string strLog = "Connection failed: " + ErrorsCatcher.GetReturnCodeDesc(ErrorsCatcher.errorName) + Environment.NewLine; richTextBoxLogs.AppendText(strLog); richTextBoxLogs.ScrollToCaret(); return; } else { RWDev.frmComPortIndex = portHandle; string strLog = "Connected " + comboBoxCOM.Text + "@" + comboBoxBaud.Text + Environment.NewLine; richTextBoxLogs.AppendText(strLog); } EnabledForm(); buttonConnect.Enabled = false; buttonDisconnect.Enabled = true; } catch { richTextBoxLogs.AppendText("Connected failed!" + Environment.NewLine); } }