我正在做停車場顯示系統的最後一年項目。它的基本功能是檢測停車場中的任何車輛,更新可用的車輛數量並顯示是否已滿。串口通訊工程
我在使用Microsoft Visual Studio 2008編寫C#程序時遇到了麻煩。我從串口收到我的其他項目隊友發送的數據。我需要做的就是接收數據,然後讀取數據。這只是爲了模擬車輛被佔用和清空。我需要編輯我的程序,以便我可以接收和更新有關批次的數據,無論它是否被佔用並調用其他功能。數據接收將採用ASCII格式。每批將被分配到一個特定的ascii。例如0x5A是lot1,0x59是lot 2等。
這是我的項目的一個示例。
int intNumberLots = 6;
SerialPort serialPort = new SerialPort("COM1");
serialPort.BaudRate = 115200;
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();
}
}
然後對於更新如果沒有汽車的地段的代碼;
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();
}
你真正的問題是什麼?我不認爲這是一個問題。 –
好...繼續下去。 – Reniuz