static void GetAvailableComPorts() { Console.WriteLine("Open Serial/COM ports for use:"); if (SerialPort.GetPortNames().Length > 0) { foreach (string serialPort in SerialPort.GetPortNames()) { GsmCommunication com = new GsmCommunication(serialPort); try { com.OpenPort(); if (com.CheckPort()) { string phoneNumber = com.GetNumber(); string imei = com.GetImei(); Console.WriteLine("Port {0} is available for use.", serialPort); Console.WriteLine("Port {0} SIM card number is: {1}", serialPort, phoneNumber); Console.WriteLine("Port {0} IMEI number: {1}", serialPort, imei); } } catch (Exception e) { Console.WriteLine("[Error] exception occured: {0}", e.Message); } finally { com.ClosePort(); } } } else { Console.WriteLine("No available COM ports for use now."); } }
public void CheckNumber() { try { string currentNumber = module.GetNumber(); if (!PhoneNumber.Equals(currentNumber)) { Console.WriteLine("[{0}][Info] Phone number from \"{1}\" changed to \"{2}\"", IMEI, PhoneNumber, currentNumber); PhoneNumber = currentNumber; dynamic phonenum = new JObject(); phonenum.number = currentNumber; phonenum.imei = IMEI; string registerPhoneJson = JsonConvert.SerializeObject(phonenum); RegisterPhone(registerPhoneJson); } } catch (Exception ex) { Console.WriteLine("[{0}][Error] Failed to read phone number: {1}", IMEI, ex.Message); } }
static void Main(string[] args) { while (true) { if (SerialPort.GetPortNames().Length > 0) { foreach (string serialPort in SerialPort.GetPortNames()) { GsmCommunication com = new GsmCommunication(serialPort); try { com.OpenPort(); if (com.CheckPort()) { string phoneNumber = com.GetNumber(); string imei = com.GetImei(); Console.WriteLine("[Main] Port {0} is available for use.", serialPort); Console.WriteLine("[Main] Port {0} SIM card number is: {1}", serialPort, phoneNumber); Console.WriteLine("[Main] Port {0} IMEI number: {1}", serialPort, imei); Module GSM = new Module(serialPort); GSM.IMEI = imei; GSM.PhoneNumber = phoneNumber; //Find existing module by IMEI - if not found we add new if (!modules.Exists(ByIMEI(imei))) { modules.Add(GSM); } } } catch (Exception e) { Console.WriteLine("[Main][Error] exception occured: {0}", e.Message); } finally { com.ClosePort(); } } } else { Console.WriteLine("[Main] No available COM ports for use now."); } foreach (var module in modules) { if (!module.Running) { module.Start(); module.Running = true; } } Console.WriteLine("[Main][Info] Modules total connected: {0}", modules.Count); Thread.Sleep(MAIN_DELAY); } }