PC Program that reads VEX serial port

Hey,

I haven’t been on here lately because I’ve been tied up with FRC, but now I’m embarking on something I started before FRC season.

I am sending data from my Vex microprocessor via the serial port on it to my laptop via the orange cable.

I wrote a program in C# that attempts to read that data, but everything I try doesn’t work. The closest I’ve come is getting a console window reading the port, but instead of the number ‘2’ it prints out question marks??

Right now I’m lost. Could anyone point me in the right direction, or if you’ve done this before… how did you get it to work? Thanks!

Here is my C# code:

#region Namespace Inclusions
using System;
using System.IO.Ports;
#endregion

namespace SerialPortRead
{
    class SerialPortProgram
    {
        // Create the serial port with basic settings
        private SerialPort port = new SerialPort("COM4", 15200, Parity.None, 8, StopBits.One);

        [STAThread]
        static void Main(string] args)
        {
            // Instatiate this class
            new SerialPortProgram();
        }

        private SerialPortProgram()
        {
            Console.WriteLine("Incoming Data:");

            // Begin communications
            if (port.IsOpen)
            {
                port.Close();
            }
            port.Open();
            port.DtrEnable = true;
            port.RtsEnable = true;
            port.NewLine = "\n";

            Console.WriteLine(port.ToString());
            Console.WriteLine(port.PortName);
            Console.WriteLine(port.NewLine.ToString());
            Console.WriteLine(port.IsOpen.ToString());
            Console.WriteLine(port.ReadExisting());

            //Enter an application loop to keep this thread alive
            //Application.Run();
            int i = 0;
            while (i < 1000)
            {
                // Attach a method to be called when there      
                // is data waiting in the port's buffer
                port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
                Console.WriteLine(port.ReadExisting());
                i++;
            }
            //port.Close();
        }

        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Show all the incoming data in the port's buffer
            Console.WriteLine(port.ReadExisting());
        }
    }
}

Hallelujah!! (If you build it, they will come!)

I have been waiting for this day…

First Off, what version of C#???

Second, I don’t know much about C#, But the Code:


        // Create the serial port with basic settings
        private SerialPort port = new SerialPort("COM4", 15200, Parity.None, 8, StopBits.One);


Needs to be:


        // Create the serial port with basic settings
        private SerialPort port = new SerialPort("COM4", 115200, Parity.None, 8, StopBits.One);


The Wrong Baud Rate will get you those Question Marks, among other strange “non visable” Characters…

Also, do you have to open the Serial Port in the “namespace SerialPortWrite”, or Should the “private SerialPort port = new SerialPort(“COM4”, 115200, Parity.None, 8, StopBits.One);” be moved to a Third namespace that can be Instantiated separately from the “SerialPortRead” or “SerialPortWrite”?

Ah-ha!

MarkO, I that your acquired wisdom and expertise in the programming field would catch my error! You’re right, the baud rate was wrong. Now it displays correctly. Thanks again!

Yea in visual studio when I use serial ports the wrong baud rate causes some really funky stuff to occur. Odd characters spaces ect…

What is the default baud rate on the vex micro controller like if you printed a string in easy c because I don’t remember an option for it.

The baud rate is 115,200

Yeah, it was really messing me up!

So what is your Goal??

Are you willing to Post your C# Code??

Are you using VS 2008???

Tell Us, Tell Us!!!

Right now I’m just experimenting with reading data from a serial port in my free time. Currently, I don’t have a clear defined goal, but I am really enjoying learning more about how computers talk to each other.

I just bought my first car (1972 Mercedes Benz 220D) and was thinking of incorporating VEX/Arduino into it. Maybe reading a sensor or something??? I really don’t know, any ideas?

BTW - when I get my code cleaned up, I’m going to put it on Cheifdelphi or this forum somewhere.

Oh, I forgot!

MarkO - I am using VS 2008. :wink:

Why do you ask?

Way to go!! Computers Talking Together is Networking, and the Computer"is.

Nice car!! Going to run BioDiesel?? My dad has a 1965 190D, with a 200D in it, and a 1959 180D.

Ultrasonic sensors on each side, back and front… to help you park and Backup, and avoid running into the Vehicle in front of you.

Post some pictures…

Thanks, I would really appreciate that!!!

I’ve finished the program. I have only tested it on my machine, so please let me know if there is something wrong. Source code is provided, so make changes if you need too - I don’t mind :wink:

You can download it here: (it’s at the bottom of the page)
https://vexforum.com/local_links.php?catid=26&sort=N&pp=10&page=3

Enjoy!

I can not locate your Source Code, just all of the system code.

Which Folder has your code:

https://vexforum.com/attachment.php?attachmentid=1806&stc=1&d=1245864959

Try double clicking the Form1.cs. Does it show you the form designer?

Yes:

https://vexforum.com/attachment.php?attachmentid=1811&stc=1&d=1245865961

And you can’t view my code by double clicking on that form?

I can, also a Right Click on the Form and then Left Click on “View Code” works as well.

https://vexforum.com/attachment.php?attachmentid=1816&stc=1&d=1245866368

But, where is the Code for “serialPort1.Open()”, “serialPort1.Close()” and “serialPort1.ReadExisting()”??

private void button1_Click(object sender, EventArgs e)
        {
            // assigns the data coming in from the serial port to the variable 'data'
            data = serialPort1.ReadExisting();

            // displays the contents of 'data' in textBox1
            textBox1.Text = data.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();

                MessageBox.Show("The Serial Port was already opened, this program closed it to prevent an error. Please set the COM port value again");
            }
            else
            {
                // sets what come port you want to use
                serialPort1.PortName = "COM" + numericUpDown1.Value;

                // sets the read timeout value
                serialPort1.ReadTimeout = 500;

                // opens the serial port
                serialPort1.Open();
            }
        }

It’s all right there. I have the program open the serial port when the user chooses what COM port they are using. Then I use the data variable to store the information coming from the serial port.


data = serialPort1.ReadExisting();

serialPort1.Open(), serialPort1.Close() and serialPort1.ReadExisting() are all part of the C# SerialPort class. I think that might be what you’re asking? I didn’t write the SerialPort class - I wish I could take credit for that though!

Yes, and displayed in TextBox1 with:


            // displays the contents of 'data' in textBox1
            textBox1.Text = data.ToString();


I found thisForm1.Designer.cs , where some Defaults are set for the Serial Port:

https://vexforum.com/attachment.php?attachmentid=1821&stc=1&d=1245867400

So is this a Total Re-Write from your First Post??

Yes, my first program was a simple console application. This one is a windows form application.

OK… That now makes sense…

Do you know how to make a Two State Button on the Form, or have two Buttons share the same “space” (e.g. X, Y coordinates)??