简体   繁体   English

Visual Studio C#中的串行端口通信

[英]Serial port communication in Visual Studio C#

I have used this code in VS 我在VS中使用了这段代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    string RxString,ComPort;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
 {

serialPort1.PortName = "COM5";

serialPort1.BaudRate = 9600;  
serialPort1.Parity = Parity.None; 
serialPort1.StopBits = StopBits.One; 
serialPort1.Handshake = Handshake.None;

serialPort1.DataReceived += new       SerialDataReceivedEventHandler(serialPort1_DataReceived);
  }
    private void Start_Click(object sender, EventArgs e)
    {
        serialPort1.PortName = ComPort;
        serialPort1.BaudRate = 9600;

        serialPort1.Open();
        if(serialPort1.IsOpen)
        {
            Start.Enabled = false;
            Stop.Enabled = true;
            textBox1.ReadOnly = false;
        }
    }

    private void Stop_Click(object sender, EventArgs e)
    {
        if (serialPort1.IsOpen)
        {
            serialPort1.Close();
            Start.Enabled = true;
            Stop.Enabled = false;
            textBox1.ReadOnly = true;
        }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (serialPort1.IsOpen) serialPort1.Close();
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!serialPort1.IsOpen) return;

        char[] buff = new char[1];

        buff[0] = e.KeyChar;

        serialPort1.Write(buff, 0 , 1);

        e.Handled = true;
    }

    private void DisplayText(object sender, EventArgs e)
    {
        textBox1.AppendText(RxString);
    }

    private void serialPort1_DataReceived(object sender,        System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        RxString = serialPort1.ReadExisting();
        this.Invoke(new EventHandler(DisplayText));
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComPort = comboBox1.SelectedItem.ToString();
    }

  }
  }

but it's not working, I have tried using an avr for transmitting characters and successfully tested in hercules what its transmitting. 但是它不起作用,我尝试使用av​​r传输字符,并在大力士中成功测试了其传输的内容。 But it is not showing up in my program. 但是它没有显示在我的程序中。 Please help. 请帮忙。

I have updated the code and its working fine for receiving part but not transmitting correctly, i am not getting any error it is just not working as it should have worked. 我已经更新了代码及其接收部分的正常工作,但无法正确传输,我没有收到任何错误,只是它没有工作,因为它应该工作。

You must set all the properties of your serialPort1 . 您必须设置serialPort1的所有属性。

Also, you should try to debug at multiple place to help us where it's going wrong : Does IsOpen return true? 另外,您应该尝试在多个地方进行调试,以帮助我们解决出现问题的地方: IsOpen是否返回true? if no, this explain why you receive nothing. 如果没有,这说明了为什么您什么也得不到。

See MSDN example if you want to try something is supposed to work : http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx 如果您想尝试某些操作,请参见MSDN示例: http : //msdn.microsoft.com/zh-cn/library/system.io.ports.serialport.aspx

Be sure that the serial port is not alerady open by another program and you have selected the good COM PORT . 确保串行端口没有被其他程序打开,并且您选择了正确的COM PORT Otherwise the code looks good. 否则代码看起来不错。 (You could also looks in the RxString value each time you are passing by. (maybe many empty space or "/r")) (您也可以在每次经过时在RxString值中查找。(可能有很多空白或“ / r”))

[Serializable]
public class Customer
{
    [XmlElement("FirstName")]
    public string FirstName { get; set; }

    [XmlElement("LastName")]
    public string LastName { get; set; }

    [XmlElement("Age")] public int Age { get; set; }

    public bool ShouldSerializeLastName()
    {
        return Age > 18; // Enter here only if it is XmlSerialize.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM