3 Replies - 726 Views - Last Post: 26 August 2016 - 02:07 AM Rate Topic: -----

#1 VIPERHlr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 05-August 11

Does C# work with virtual ports ?

Posted 25 August 2016 - 02:44 PM

I am trying to receive data from a microcontroller which I am currently simulating using proteus because I dont have the hardware yet. I coded a gui using Visual Studio but when I try to recieve data, it just hangs or freezes the gui, no error, heres the code

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 SerialPortCommunication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            getPorts();
        }
        void getPorts()
        {
            String[] ports = SerialPort.GetPortNames();
            comboBox1.Items.AddRange(ports);
        }

        private void button1_Click(object sender, EventArgs e) //Open Port
        {
            try
            {
                if (comboBox1.Text == "" || comboBox2.Text == "")
                {
                    MessageBox.Show("Select Port Settings");
                }
                else
                {
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
                    serialPort1.Open();
                    progressBar1.Value = 100;
                }
            }
            catch(UnauthorizedAccessException)
            {
                MessageBox.Show("Unauthorized access");
            }

        }

        private void button2_Click(object sender, EventArgs e) //Close Port
        {
            serialPort1.Close();
            progressBar1.Value = 0;
        }

        private void button3_Click(object sender, EventArgs e) //Read Port
        {
            try
            {
                textBox1.Text = serialPort1.ReadLine();
            }
            catch(TimeoutException)
            {
                MessageBox.Show("Timeout Exception");
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Does C# work with virtual ports ?

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Does C# work with virtual ports ?

Posted 25 August 2016 - 03:56 PM

Start debugging with F10 then walk through it line by line.
I strongly urge you to set this aside for an hour and work through the Debugging Tutorials listed below. An hour or two spent learning this vital skill will save you 100x that on this project alone.


See FAQ # 2 "How do I debug my program?"

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2016
Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Does C# work with virtual ports ?

Posted 25 August 2016 - 10:21 PM

have you attempted to use the ReadExisting() or ReadChar() methods of the SerialPort Class
https://msdn.microso...(v=vs.110).aspx

also have you tested with a terminal emulator such as TeraTerm Pro
https://ttssh2.osdn.jp/index.html.en
Was This Post Helpful? 1
  • +
  • -

#4 VIPERHlr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 05-August 11

Re: Does C# work with virtual ports ?

Posted 26 August 2016 - 02:07 AM

Thanks guys, I managed to get it working by changing the read line to read existing and using a different port emulation software
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1