// This function checks to see if the port is a modem, by sending // an AT command to the port. if the port responds, it is assumed to // be a modem. The function returns a bool. private bool IsPortAModem(int port) { // Always wrap up working with Comm Ports in exception handlers. try { // Attempt to open the port. m_CommPort.Open(port, 115200, 8, Rs232.DataParity.Parity_None, Rs232.DataStopBit.StopBit_1, 4096); // Write an AT Command to the Port. m_CommPort.Write(Encoding.ASCII.GetBytes("AT" + "\n")); // Sleep long enough for the modem to respond. System.Threading.Thread.Sleep(200); Application.DoEvents(); // try { to get info from the Comm Port. try { Byte b; // try { to read a single byte. if you get it, then assume // that the port contains a modem. Clear the buffer before // leaving. m_CommPort.Read(1); m_CommPort.ClearInputBuffer(); m_CommPort.Close(); return(true); } catch (Exception exc) { // null to read from the Comm Port, so set to false m_CommPort.Close(); return(false); } } catch (Exception exc) { // Port could not be opened or written to. this.clstPorts.SetItemChecked(port - 1, false); MessageBox.Show("Could not open port.", this.Text); return(false); } }
private void _RS232_DataReceived(Rs232 Source, byte[] DataBuffer) { mScannerTimeout.Stop(); //接受到了数据 //校验数据 if (DataBuffer.Length != 5) { Source.ClearInputBuffer(); StartNextCheckInformationRequest(); return; } byte[] ReceiveData = DataBuffer; //Special CheckSum校验 int CheckSumFlag = 0; for (int i = 1; i <= ReceiveData.Length - 2; i++) { CheckSumFlag += ReceiveData[i]; } if ((byte)(CheckSumFlag % 256) != ReceiveData[ReceiveData.Length - 1]) { StartNextCheckInformationRequest(); return; } ; if ((256 - (mCurrentRoom.Number + mCurrentRoom.MyFloor.Number) % 256) != ReceiveData[0]) { StartNextCheckInformationRequest(); return; } ; if (mCurrentReceiveDataCheckRepeatTimes == 0) { mReceiveCheckData = ReceiveData; } else { //判断是不是与前几次相同 if (!Utility.ArrayDataEquals(mReceiveCheckData, ReceiveData)) { StartNextCheckInformationRequest(); return; } ; } //下一步 mCurrentReceiveDataCheckRepeatTimes++; if (mCurrentReceiveDataCheckRepeatTimes >= mReceiveDataCheckRepeatTimes) { //数据都正确 ApplyData(); //判断是否断网 if (mDisconnectList[mCurrentRoom.ToString()] != null) { mDisconnectList.Remove(mCurrentRoom.ToString()); } mCurrentRoom.MyATM.Connect(); //延时一段时间再查询下一个房间 mScanNextRoomDelay.Start(); } else { //下一次校验查询 mScannerTimeout.Start(); SendRequestRoomInformationData(); } }