몇군데 찾아본 소스가 제대로 작동 안하고 있음
고친 소스를 올림.
추신 : 수정함 - 자동 접속전에 타겟 텍스트 클리어 / 시리얼 수신 로직 오류 수정
================================
void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int temp = 0, i, ndx = 0, headFound = 0;
string str = string.Empty;
try // IO exception Error if try 'auto Connect' several times
{
//i = serialPort1.BytesToRead;
temp = serialPort1.Read(array, 0, serialPort1.BytesToRead);
}
catch (System.Exception ex)
{
//MessageBox.Show(ex.ToString()); // use only debuging time
}
=======================================
private void btnIS4autoConn_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
cbInterfaceType.Text = "RFID IS4200"; // set Interface type to detect
lblFirmwareV4.Text = "Firmware NA"; // Clear it before searching COM port
lblCardSerialV4.Text = "Card Serial NA";
serialPort1.StopBits = StopBits.One;
serialPort1.Parity = Parity.None;
serialPort1.DataBits = 8;
serialPort1.BaudRate = int.Parse(cbBaudRateAuto.Text);
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
string devicename = string.Empty;
//devicename = Encoding.Default.GetString(command); // byte to string conversion
//serialPorts = SerialPort.GetPortNames();
//foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
//foreach (string s in SerialPort.GetPortNames())
string[] portNames = SerialPort.GetPortNames(); // 시스템의 모든 COM 포트 이름 수집
foreach (string cPorts in portNames)
{
try
{
serialPort1.PortName = cPorts;
serialPort1.Open();
tbRecvLog.Text += cPorts; // Record Log information
tbRecvLog.Text += "\r\n";
//ReadRFIDcardV1(0x14, 1, 5);
ReadRFIDcardV4(0x10, 0, 6); // Query IS4200 Firmware V
Delay(200);
//System.Threading.Thread.Sleep(100); // 1000 msec - Freezing error !!
//if (lblFirmwareV4.Text.StartsWith(tbDeviceName.Text))
if (lblFirmwareV4.Text.Contains(tbDeviceName.Text))
{
p_tail = p_head = 0;
for (int i = 0; i < Contants.BUFFSIZE; i++) // Clear the buffer
{
command[i] = 0x00;
msgBuff[i] = 0x00;
}
if (serialPort1.IsOpen) serialPort1.Close();
serialPort1.Open();
btnClose.Enabled = true;
btnOpen.Enabled = false;
cbBaudrate.Text = int.Parse(cbBaudRateAuto.Text).ToString();
cbPortName.Text = cPorts;
break;
}
if (serialPort1.IsOpen) serialPort1.Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
if (serialPort1.IsOpen)
MessageBox.Show("COM Open OK");
else
MessageBox.Show("Fail to Open");
}