Teensy++ 2.0 & Serial Port

Status
Not open for further replies.

QuantumPhysGuy

New member
Hello,

I am trying to communicate with my Teensy via the serial port and a c# application I am writing. Problem is, when I attempt to write data to the Teensy, my application just freezes and not sure why.

Here is my Teensy code:

Code:
const int FUEL_GAUGE_PWM = 27;

boolean dataComplete = false;  // whether the string is complete
int controlPacket[4];
int packetIndex = 0;

void setup() 
{
  pinMode(FUEL_GAUGE_PWM, OUTPUT);

  Serial.begin(9600);
}

void loop() 
{
  if (dataComplete) 
  {
    analogWrite(FUEL_GAUGE_PWM, controlPacket[0]);
    dataComplete = false;
  }
}

void serialEvent() 
{
  while (Serial.available()) 
  {
    controlPacket[packetIndex] = (byte)Serial.read();
    Serial.write(controlPacket[packetIndex]);
    packetIndex++;
    
    if (packetIndex == 4)
    {
      packetIndex = 0;
      dataComplete = true;
    }
  }
 }

Here is my C# app code:

Code:
        private SerialPort sp = new SerialPort("COM8", 9600, Parity.None, 8);
        private Timer tmr = new Timer();

        public Form1()
        {
            InitializeComponent();

            tmr.Interval = 10;

            tmr.Tick += tmr_Tick;
            button1.Click += button1_Click;

            sp.Open();
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            try
            {
                byte[] data = new byte[4];
                data[0] = Convert.ToByte(trackBar1.Value);
                data[1] = 0;
                data[2] = 0;
                data[3] = 0;

                sp.Write(data, 0, data.Length);
            }
            catch (Exception ex)
            {
                tmr.Stop();
                MessageBox.Show("Error! " + ex.Message);
            }

        }

        void button1_Click(object sender, EventArgs e)
        {
            if (tmr.Enabled)
            {
                tmr.Stop();
            }
            else
            {
                tmr.Start();
            }

        }

Any idea why the application would just freeze? I have tried almost everything I can think of. I have the Teensy set as a Serial+Keyboard+Mouse+JoyStick.
 
Status
Not open for further replies.
Back
Top