private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { //string receivedData = serialPort1.ReadExisting(); string receivedData = serialPort1.ReadLine(); Int16 playerIndex = -1; // // Vin daachira pirveli // if (receivedData.IndexOf("GET /ax_SW&sw=00") > -1) { playerIndex = 0; } else if (receivedData.IndexOf("GET /ax_SW&sw=01") > -1) { playerIndex = 1; } else if (receivedData.IndexOf("GET /ax_SW&sw=02") > -1) { playerIndex = 2; } else if (receivedData.IndexOf("GET /ax_SW&sw=03") > -1) { playerIndex = 3; } if (playerIndex > -1) { timer1.Enabled = false; rtxtDataArea.Invoke(new Action(() => rtxtDataArea.AppendText(receivedData))); rtxtDataArea.Invoke(new Action(() => rtxtDataArea.Focus())); btnStart.Invoke((new Action(() => btnStart.Text = "RESUME"))); btnStart.Invoke((new Action(() => btnStart.Enabled = true))); Status_lbl.Invoke((new Action(() => Status_lbl.Text = "PAUSED"))); Player_groups[playerIndex].Invoke((new Action(() => Player_groups[playerIndex].BackColor = activeColor))); gBox_YesNO[playerIndex].Invoke((new Action(() => gBox_YesNO[playerIndex].Enabled = true))); if (cBox_MidiOnPush.Checked) { PlayMidiNote(MIDI_PUSH.Text); } } }
private bool ModbusUpdate() { try { lock (m_syncObject) { switch (state) { case State.Read: try { rdBuf = m_mbMaster.ReadHoldingRegisters(slaveaddr, read_start_addr, read_regs_number); state = State.New; Array.Copy(rdBuf, date_ind, date_regs, 0, 3); //++ Array.Copy(rdBuf, id_ind, ID, 0, 6); //++ Array.Copy(rdBuf, src_ind, src_num_regs, 0, 2); //++ Array.Copy(rdBuf, md5_ind, md5_regs, 0, 8); //++ Read_reg = rdBuf[19]; //++ ver_reg = rdBuf[39]; //++ Status_lbl.Invoke((MethodInvoker) delegate { Status_lbl.Text = "Подключен"; }); mb_ok++; is_connected = true; if (mb_ok > mb_tresh) { mb_ok = 0; error_cnt = 0; } } catch (Exception ex) { string strcgc = ex.Message; m_lastError = "Ошибка чтения"; error_cnt++; Status_lbl.Invoke((MethodInvoker) delegate { Status_lbl.Text = m_lastError + " " + error_cnt.ToString(); }); if (error_cnt > mb_tresh) { is_connected = false; } state = State.New; return(false); } break; case State.Write: try { state = State.Set; } catch (Exception ex) { return(false); } break; case State.New: break; case State.Set: state = State.Read; break; } } return(true); } catch (Exception ex) { m_lastError = ex.Message; return(false); } }