I'm doing a final year project on car park display system. What it does basically is to detect any cars in the parking lot, update the number of lots available and show whether it's full or not.
I'm having trouble writing a programme in C# using Microsoft Visual Studio 2008. I'm getting data from the serial port that my other project mate is sending through. All I need to do is to receive the data, then read the data. This is just to simulate the car lots being occupied and emptied. I need to edit my program so that i can receive and update data regarding the lots whether it is occupied or not and calling other functions. The data receiving will be in ascii. each lot will be assigned to a specific ascii. Like for example 0x5A is for lot1, 0x59 is for lot 2 etc..
Here is a sample of my project.
int intNumberLots = 6;
SerialPort serialPort = new SerialPort("COM1");
serialPort.BaudRate = 1开发者_StackOverflow社区15200;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
serialPort.DataBits = 8;
serialPort.Handshake = Handshake.None;
serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
serialPort.Open();
private void startButton_Click(object sender, EventArgs e)
{
while(){
if (serialPort.ReadLine == "0x57" && intNumberLots >= 0) // if data received is "1", do tasks below
{
inputTextBox.Clear(); // clear input textbox
pictureBox1.Visible = true; // show a picture of a vehicle
intNumberLots--; // decrease the number of available parking lots shown
label9.Text = intNumberLots.ToString();
}
else if (serialPort.ReadLine == "0x58" && intNumberLots >= 0)
{
inputTextBox.Clear();
pictureBox2.Visible = true;
intNumberLots--;
label9.Text = intNumberLots.ToString();
}
else if (serialPort.ReadLine == "0x59" && intNumberLots >= 0)
{
inputTextBox.Clear();
pictureBox3.Visible = true;
intNumberLots--;
label9.Text = intNumberLots.ToString();
}
else if (serialPort.ReadLine == "0x5A" && intNumberLots >= 0)
{
inputTextBox.Clear();
pictureBox4.Visible = true;
intNumberLots--;
label9.Text = intNumberLots.ToString();
}
}
Then for the code that updates if there is no car in the lot;
else if (string.str != "0x57" && intNumberLots >= 3)
{
inputTextBox.Clear();
pictureBox1.Visible = false;
intNumberLots++;
label9.Text = intNumberLots.ToString();
}
else if (string.str != "0x58" && intNumberLots >= 3)
{
inputTextBox.Clear();
pictureBox2.Visible = false;
intNumberLots++;
label9.Text = intNumberLots.ToString();
}
else if (string.str != "0x59" && intNumberLots >= 3)
{
inputTextBox.Clear();
pictureBox3.Visible = false;
intNumberLots++;
label9.Text = intNumberLots.ToString();
}
else if (string.str != "0x5A" && intNumberLots >= 3)
{
inputTextBox.Clear();
pictureBox4.Visible = false;
intNumberLots++;
label9.Text = intNumberLots.ToString();
}
Each call to serialPort.ReadLine
is going to block until the next line comes in.
You need to serialPort.ReadLine
once at the top of the loop into a variable and test that.
While(){
message = serialPort.ReadLine();
if (message == "0x57" && intNumberLots >= 0) // if data received is "1", do tasks below
{
...etc.
Also, is the data really a string comprising the characters "0x5A"
etc. or is each a character '0x5A'
?
I notice that your example isn't real code - (While
instead of while
, serialPort.ReadLine
instead of serialPort.ReadLine()
, string.str
, for example). Please try to post proper example code that we can help you fix - there are too many issues with this to identify your true problem (especially since you've not said what the problem is!)
I see you have hooked up the datareceived event handler for the serial port. If so, why are you not using it? The event will fire up whenever data comes thru the serial port, which is better then aimlessly polling the serial port for data which is only a waste of resources.
public void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
var datareceived = serialport.ReadLine()
// Do data processing here
// Invoke delegate using dispatcher to update UI
}
精彩评论