Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace ultra
{
    public partial class Form1 : Form
    {
        private SerialPort myport;
        private DateTime date;
        private string in_data;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myport = new SerialPort();
            myport.BaudRate = 9600;
            myport.PortName = "COM5";
            myport.Parity = Parity.None;
            myport.DataBits = 8;
            myport.StopBits = StopBits.One;
            myport.DataReceived += myport_DataReceived;
            try
            {
                myport.Open();
                textBox1.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
        void myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            in_data = myport.ReadLine();
            this.Invoke(new EventHandler(displaydata_event));

        }
        private void displaydata_event(object sender, EventArgs e)
        {
            date = DateTime.Now;
            string time = date.Hour + ";" + date.Minute + ";" + date.Second;
            textBox1.AppendText(time + "\t\t\t\t\t" + in_data);
            label2.Text = in_data;
        }
    }
}


What I have tried:

i am trying to read port data there is no error but it did not display any data
Posted
Updated 30-Mar-20 22:29pm
Comments
Padanian 31-Mar-20 4:21am    
Is there data incoming through? How do you know?

1 solution

1) That's not "Visual studio 2015"
2) That's not "VB".

That's C# - it's a language that Visual Studio supports, but not the only one. Correctly identifying the language is important as it means that people with good knowledge of that language will look at your question. Saying it's VB instead will put those people off.

The reason it doesn't display any data is probably down to one (or more) of several possible factors:
1) The communications with the device is badly configured:
1.1) The device may not be connected to Com5
1.2) The device may be using different speed, BPC, or parity.
1.3) The connection may require handshaking, depending on what type of connector it is using and how the device is configured.
2) The device may not talk all the time - in fact, it may not talk at all unless you talk to it first.
3) The data may not be terminated with a newline

Start by isolating the factors: use Hyperterminal or similar to connect to the device and see if you can see data coming from it. Until you can, you aren't going anywhere with your code. If you can't start with the manual for the device and see what it says you need to do.
Try downloading sample software from the device manufacturer - most provide at least a basic "it works!" test app to check it and the source code is probably available.

Get it working with everything else before you even start to write code - you introduce far too many variables to debug otherwise.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900