public Form1() { InitializeComponent(); plcStatusLabel = this.PLCStatusLabel; messageReceived = this.MessageRecieved; messageSent = this.MessageSent; ReadBitValue = this.readBitValue; Read32FloatValue = this.read32FloatValue; Read32InFloatValue = this.read32InFloatValue; Read32Value = this.read32Value; Read16Value = this.read16Value; SerialComms = new USBSerialComms(this); Factory mbFactory = new Factory(); var json = System.IO.File.ReadAllText(@"c:\users\kevin\desktop\modbus.json"); var objects = JArray.Parse(json); // parse as array foreach (JObject root in objects) { foreach (KeyValuePair <String, JToken> app in root) { var n = app.Key; var appName = app.Key; var type = (String)app.Value["Type"]; byte device = (byte)app.Value["Device"]; byte writeCmnd = (byte)app.Value["WriteCmnd"]; byte readCmnd = (byte)app.Value["ReadCmnd"]; byte addHi = (byte)app.Value["addHi"]; byte addLo = (byte)app.Value["addLo"]; ModbusObjInitializer init = new ModbusObjInitializer(type, device, writeCmnd, readCmnd, addHi, addLo); MBLst.Add(mbFactory.GetModbusObj(init)); } } }
public void checkConns() { if (!bInsideCheckConns) { bInsideCheckConns = true; while ((bCheckConnsCheckAgain) || (bInsideCheckConns)) { try { if (_serialPort.IsOpen) { _serialPort.Close(); //doing this early on to reduce likelyhood of trying to write to closed port which throws unrecoverable error _serialPort.Dispose(); } } catch { //unplugging USB Serial ports (which is what our PLCs act like) will cause an exception. This try/catch lets the program continue after an exception. } try { String[] portNames = SerialPort.GetPortNames(); //ArrayList alCommPortInfo = new ArrayList(); List <COMPortInfo> alCommPortInfo = new List <COMPortInfo>(); foreach (String s in portNames) { // s is like "COM14" COMPortInfo ci = new COMPortInfo(); ci.portName = s; ci.friendlyName = s; alCommPortInfo.Add(ci); } String[] usbDevs = USBSerialComms.GetUSBCOMDevices(); foreach (String s in usbDevs) { // Name will be like "USB Bridge (COM14)" int start = s.IndexOf("(COM") + 1; if (start >= 0) { int end = s.IndexOf(")", start + 3); if (end >= 0) { // cname is like "COM14" String cname = s.Substring(start, end - start); for (int i = 0; i < alCommPortInfo.Count; i++) { if (((COMPortInfo)alCommPortInfo[i]).portName == cname) { ((COMPortInfo)alCommPortInfo[i]).friendlyName = s; } } } } } int iCommNumTemp = -1; for (int i = 0; i < alCommPortInfo.Count; i++) { COMPortInfo myPort = (COMPortInfo)alCommPortInfo[i]; if (myPort.friendlyName.Contains("VelocioComm")) { String sCommNum = myPort.portName; sCommNum = sCommNum.Remove(0, 3); iCommNumTemp = Int32.Parse(sCommNum); break; } } if ((iCommNumTemp != iCommNum) || ((iCommNumTemp == -1) && (bDevicePluggedIn == true)) || ((iCommNumTemp != -1) && (bDevicePluggedIn == false))) { if (iCommNumTemp == -1) { form.Invoke(new delegateUpdateUSBEnabled(updateUSBEnabled), false); } else { form.Invoke(new delegateUpdateUSBEnabled(updateUSBEnabled), true); _serialPort = new SerialPort("COM" + iCommNumTemp.ToString(), 115200, Parity.None, 8, StopBits.One); _serialPort.Handshake = Handshake.None; if (!_serialPort.IsOpen) { _serialPort.Open(); _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived); iCommNum = iCommNumTemp; } } } } catch { //unplugging USB Serial ports (which is what our PLCs act like) will cause an exception. This try/catch lets the program continue after an exception. form.Invoke(new delegateUpdateUSBEnabled(updateUSBEnabled), false); } if (bCheckConnsCheckAgain) { bCheckConnsCheckAgain = false; } else { bInsideCheckConns = false; } } } else { bCheckConnsCheckAgain = true; } }