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());
}
}
}
// 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”?
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!
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.
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
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.
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!