public void SerialCommunicationInit()
 {
     port = new SerialPort("COM5", 115200, Parity.None, 8, StopBits.One);
   //  port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
     port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
     currentMessage = new Message();
     port.Open();
 }
 public void SerialCommunicationInit(String COMx)
 {
     try
     {
         port = new SerialPort(COMx, 115200, Parity.None, 8, StopBits.One);
         // port = new SerialPort(COMx, 9600, Parity.None, 8, StopBits.One);
         port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
         currentMessage = new Message();
         port.Open();
         PortIsOpen = true;
     }
     catch (Exception e)
     {
         PortIsOpen = false;
     }
 }
        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            while (port.BytesToRead != 0)
            {
                byte temp = (byte)port.ReadByte();
                if (port.BytesToRead > 10) OnThresholdReached(port.BytesToRead);
               //  Debug.WriteLine(port.BytesToRead.ToString()+":"+temp.ToString("x16"));
                switch (status)
                {
                    case State.unknown:
                        if (temp == 0xAA)
                            status = State.sof;
                        break;
                    case State.sof:
                        if (temp == 0x55)
                            status = State.sync;
                        else
                            status = State.unknown;
                        break;
                    case State.sync:
                        currentMessage.id = temp;
                        status = State.id;
                        break;
                    case State.id:
                        currentMessage.dlc = temp;
                        bytesToRead = currentMessage.dlc;
                        if (bytesToRead == 0)
                            status = State.eof;
                        else
                            status = State.dlc;
                        break;
                    case State.dlc:
                        currentMessage.addData(temp);
                        bytesToRead--;
                        if (bytesToRead == 0)
                            status = State.eof;
                        break;
                    case State.eof:
                        if (temp == 0x66)
                        {
                            lock (messageList)
                            {
                                messageList.Add(currentMessage);
                             //   Debug.WriteLine(currentMessage.id.ToString() + " " + currentMessage.messageData[0].ToString());
                            }
                            currentMessage = new Message();
                        }
                        status = State.unknown;
                        break;
                    default:
                        status = State.unknown;
                        break;
                }
            }
        }
 public void SendMessage(Main.MsgIDsToMCU Id, Byte ModId, Single val)
 {
     Message msg = new Message();
     msg.ConstructMessage(Id, ModId, val);
     port_sendSerialMessage(msg);
 }
 public void SendMessage(Main.MsgIDsToMCU Id, Byte PowerType, Byte NrOfPoints, UInt16[] CalPoints, Single[] RCvals, Single[] Bvals)
 {
     Message msg = new Message();
     msg.ConstructMessage(Id, PowerType, NrOfPoints, CalPoints, RCvals, Bvals);
     port_sendSerialMessage(msg);
 }
 public void SendMessage(Main.MsgIDsToMCU Id)
 {
     Message msg = new Message();
     msg.ConstructMessage(Id);
     port_sendSerialMessage(msg);
 }
 public void port_sendSerialMessage(Message TxMessage)
 {
     if (port.IsOpen)
     {
         byte[] buffer = new byte[TxMessage.dlc + 4];
         buffer[0] = SOF_BYTE;
         port.Write(buffer, 0, 1);
         buffer[0] = SYNC_BYTE;
         port.Write(buffer, 0, 1);
         buffer[0] = TxMessage.id;
         port.Write(buffer, 0, 1);
         buffer[0] = TxMessage.dlc;
         port.Write(buffer, 0, 1);
         port.Write(TxMessage.messageData.ToArray(), 0, TxMessage.dlc);
         buffer[0] = EOF_BYTE;
         port.Write(buffer, 0, 1);
     }
 }