public void FillForm(InputLine16 p, float step) { rssiInfoLbl.Text = "" + p.Rssi + " -dBm"; switch (p.Broadcast) { case 0: broadcastInfoLbl.Text = "OFF"; break; case 2: broadcastInfoLbl.Text = "Address"; break; case 4: broadcastInfoLbl.Text = "PAN"; break; default: break; } sourceInfoLbl.Text = "" + p.SourceAddress.ToString("X"); samplesInfoLbl.Text = "" + p.SampleAmount; anaMaskInfoLbl.Text = "" + Convert.ToString(p.AdcStatus, 2).PadLeft(8, '0'); ad0InfoLbl.Text = Math.Round(p.getAdcVal(0) * step, 3) >= 0 ? "" + Math.Round(p.getAdcVal(0) * step, 3) : "OFF"; ad1InfoLbl.Text = Math.Round(p.getAdcVal(1) * step, 3) >= 0 ? "" + Math.Round(p.getAdcVal(1) * step, 3) : "OFF"; ad2InfoLbl.Text = Math.Round(p.getAdcVal(2) * step, 3) >= 0 ? "" + Math.Round(p.getAdcVal(2) * step, 3) : "OFF"; ad3InfoLbl.Text = Math.Round(p.getAdcVal(3) * step, 3) >= 0 ? "" + Math.Round(p.getAdcVal(3) * step, 3) : "OFF"; ad4InfoLbl.Text = Math.Round(p.getAdcVal(4) * step, 3) >= 0 ? "" + Math.Round(p.getAdcVal(4) * step, 3) : "OFF"; ad5InfoLbl.Text = Math.Round(p.getAdcVal(5) * step, 3) >= 0 ? "" + Math.Round(p.getAdcVal(5) * step, 3) : "OFF"; digiMaskInfoLbl.Text = "" + Convert.ToString(p.DioStatus, 2).PadLeft(8, '0'); d0InfoLbl.Text = "" + p.SingleDioVal(0); d1InfoLbl.Text = "" + p.SingleDioVal(1); d2InfoLbl.Text = "" + p.SingleDioVal(2); d3InfoLbl.Text = "" + p.SingleDioVal(3); d4InfoLbl.Text = "" + p.SingleDioVal(4); d5InfoLbl.Text = "" + p.SingleDioVal(5); d6InfoLbl.Text = "" + p.SingleDioVal(6); d7InfoLbl.Text = "" + p.SingleDioVal(7); }
/// <summary> /// method that will be called when theres data waiting in the buffer /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] incByte = new byte[3]; int checksum = 0; if (comPort.BytesToRead > 0) { comPort.Read(incByte, 0, 1); if(incByte[0] == 0x7E) { frameNr++; comPort.Read(incByte, 0, 3); int length = (incByte[1] + (incByte[0] << 8)) ; // Payload length + checksum byte while (comPort.BytesToRead < length) System.Threading.Thread.Sleep(50); // Wait until enough bytes have arrived byte[] fbytes = new byte[length]; comPort.Read(fbytes, 0, length); switch (incByte[2]) // Test the API identifier { case (byte)FrameType.ATCommand: break; case (byte)FrameType.ATCommandQPV: break; case (byte)FrameType.ATCommandResponse: break; case (byte)FrameType.InputLine16: InputLine16 inframe = new InputLine16(this.frameNr, FrameType.InputLine16, length); inframe.addHex(0x7E); foreach(byte b in incByte) inframe.addHex(b); foreach(byte b in fbytes) inframe.addHex(b); inframe.SourceAddress = (fbytes[1] + (fbytes[0] << 8)); inframe.Rssi = fbytes[2]; inframe.Broadcast = fbytes[3]; inframe.SampleAmount = fbytes[4]; inframe.AdcStatus = (byte)(fbytes[5] >> 1); //Bitshift to remove the D8 inframe.DioStatus = fbytes[6]; int bcount = 8; if (inframe.DioStatus > 0) { inframe.DioVal = fbytes[bcount++]; // MSB is zero as D8 is unused in current FW } if (inframe.AdcStatus > 0) { for (int i = 0; i <= 5; i++) { if((inframe.AdcStatus & (1 << i)) != 0) { inframe.setAdcVal(i, fbytes[bcount+1] + (fbytes[bcount] << 8)); bcount += 2; } } } inframe.Checksum = fbytes[bcount]; checksum += incByte[2]; for (int i = 0; i < fbytes.Length-1; i++) checksum += fbytes[i]; int res = 0xFF - (checksum & 0xFF); if (res == inframe.Checksum) { Frames.Add(inframe); newCompleteFrame(this, new DataEventArgs(inframe)); } else Console.Write("CHECKSUM FAILED! Got " + inframe.Checksum + " while expecting " + res); break; case (byte)FrameType.InputLine64: break; case (byte)FrameType.ModemStatus: break; case (byte)FrameType.RemoteATCmdReq: break; case (byte)FrameType.RemoteCmdResp: break; case (byte)FrameType.RXPacket16: break; case (byte)FrameType.RXPacket64: break; case (byte)FrameType.TXReq16: break; case (byte)FrameType.TXReq64: break; case (byte)FrameType.TXStatus: break; case (byte)FrameType.Unknown: default: throw new Exception("Unsupported Frame Type: " + incByte[2]); } } } /* //determine the mode the user selected (binary/string) switch (CurrentTransmissionType) { //user chose string case TransmissionType.Text: //read data waiting in the buffer string msg = comPort.ReadExisting(); //display the data to the user DisplayData(MessageType.Incoming, msg + "\n"); break; //user chose hex case TransmissionType.Hex: //retrieve number of bytes in the buffer int bytes = comPort.BytesToRead; //create a byte array to hold the awaiting data byte[] comBuffer = new byte[1]; //read the data and store it comPort.Read(comBuffer, 0, 1); //display the data to the user if (comBuffer[0] == 0x7E) { DisplayData(MessageType.Normal, "\n"); } DisplayData(MessageType.Incoming, ByteToHex(comBuffer)); break; default: //read data waiting in the buffer string str = comPort.ReadExisting(); //display the data to the user DisplayData(MessageType.Incoming, str + "\n"); break; }*/ }