在Visual Basic中使用C#从串口读取rdata [英] reading rdata from serial port using C# in visual basic

查看:102
本文介绍了在Visual Basic中使用C#从串口读取rdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我在串口工作。



这是'对我来说很新,所以请帮忙解决我的问题。



如何从串口读取并通过C#写入串口?



我试过了,但是我没有得到正确的结果。

我可以连接到com端口但我无法读取数据

我给出了我的代码。

请验证并给我一个解决方案。

Hi friends,

im working on "serial Port".

It''s new for me, so Please help to solve my Problem.

How do I read from Serial port and write to serial port via C#?

I tried it, but I didn''t get proper result.
I can connect to the com port but i cant read the data
I give a my code.
Please verify and give me a solution.

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

namespace WindowsApplication1

{
    
   
    
    public partial class Form1 : Form
    {
        
    
       
        private delegate void SetTextDeleg(string text);
        string data = string.Empty;
        public Form1()
        {
            InitializeComponent();

           
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            for (int i = 0; i < ports.Length; i++)
            {
                comboBox1.Items.Add(ports[i]);
            }
        }
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                
                serialPort1.PortName = "COM1";
                serialPort1.BaudRate = 9600;
                serialPort1.DataBits = 8;
                serialPort1.Parity = Parity.None;
                serialPort1.StopBits = StopBits.One;
                serialPort1.Open();
                serialPort1.ReadTimeout = 2000;
                serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
                MessageBox.Show("Success to Connect");
                serialPort1.Close();
               
               
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed because" + ex.Message);

            }
        }



          private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

              
            Thread.Sleep(500);
            data = serialPort1.ReadLine();
           
            this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data }); 
            
             
        }

       
        private void si_DataReceived(string data)
        {
            listBox1.Items.Add(data);
        }

推荐答案

如果您在打开串口后立即关闭串口,怎么可能收到任何串口数据?

删除

If you close the serial port immediately after you''ve open it, how can it possibly receive any data?
Remove the
Quote:

serialPort1.Close();

serialPort1.Close();



button1_Click 事件处理程序。


我认为你是硬编码的按钮点击事件中的端口名称,即COM1

但是你在组合框中清楚地提到你有很多可用于系统的端口。



如果您的设备已连接到COM3。你肯定会得到一个错误,因为你只是从COM1读书。

所以不要硬编码你的COM PORT。



And尝试使用close()完成工作时关闭端口。
I think is that you are hard coding the portname in Button click event i.e., COM1
But you mentioned clearly in combobox you got lot of ports that are available in the system.

If your device is connected to COM3. you will get definitely an error because you are reading from COM1 only.
So don''t hard code you COM PORT.

And try to close your ports when you finished up with you job using close().


这篇关于在Visual Basic中使用C#从串口读取rdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆