ThomasArdal
Posts: 4
Joined: Tue Mar 20, 2007 6:19 pm
Location: Denmark
Contact: Website

Error getting data from port

Tue Mar 20, 2007 6:23 pm

Hi

I'm trying to implement a small application creating a virtual com port using Franson SerialTools and echo back the data send to the port. I can't figure out why my code is not working. The examples from SerialTools works fine.

Here's my code:

Code: Select all

using System;
using System.Threading;
using SerialNET;

namespace VirtualComPortExample
{
  class Program
  {
    private static VPort port;

    static void Main(string[] args)
    {
      ThreadStart starter = new ThreadStart(StartMe);
      Thread thread = new Thread(starter);
      thread.Start();
      Console.ReadLine();
    }

    static void StartMe()
    {
      License license = new License();
      license.LicenseKey = "<MyLicenseKey>";

      port = new VPort();
      port.ComPort = 45;
      port.Created = true;
      port.OnDataFromPort += new OnDataFromPort(DataFromPort);
    }
    static void DataFromPort(string data)
    {
      Console.WriteLine("Data from port: " + data);
      port.DataToPort("\r\nOK\r\n");
    }
  }
}
I run the app and connect to COM45 with hyperterminal. When inputting data in the hyperterminal, the OnDataFromPort event is never triggered.

Anyone have an idea on what i'm doing wrong here?

Regards,
Thomas Ardal

User avatar
johan
GpsGate
Posts: 14372
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Error getting data from port

Tue Mar 20, 2007 6:32 pm

I don't know if you can assign an event to a static method.

Regards,
Johan

Franson Support

ThomasArdal
Posts: 4
Joined: Tue Mar 20, 2007 6:19 pm
Location: Denmark
Contact: Website

RE: Error getting data from port

Tue Mar 20, 2007 6:39 pm

Hi Johan

I think you can. I refactored the code a bit. Still doesn't work:

Code: Select all

using System;
using System.Threading;
using SerialNET;

namespace VirtualComPortExample
{
  public class Program
  {
    private static VPort port;

    static void Main(string[] args)
    {
      new Program();
    }

    public Program()
    {
      ThreadStart starter = new ThreadStart(StartMe);
      Thread thread = new Thread(starter);
      thread.Start();
      Console.ReadLine();
    }

    public void StartMe()
    {
      License license = new License();
      license.LicenseKey = "<MyLicenseKey>";

      port = new VPort();
      port.ComPort = 45;
      port.Created = true;
      port.OnDataFromPort += new OnDataFromPort(DataFromPort);
    }

    public void DataFromPort(string data)
    {
      Console.WriteLine("Data from port: " + data);
      port.DataToPort("\r\nOK\r\n");
    }
  }
}
Regards,
Thomas Ardal

User avatar
johan
GpsGate
Posts: 14372
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Error getting data from port

Tue Mar 20, 2007 6:51 pm

Since the sample works, I would start from there make changes.

I cannot say why your code does not work. Maybe StartMe() is never called. Maybe COM45 is never opened and no data is sent to it. Sometimes it easily happens that you get the wrong focus when searching for errors.

Regards,
Johan

Franson Support

ThomasArdal
Posts: 4
Joined: Tue Mar 20, 2007 6:19 pm
Location: Denmark
Contact: Website

RE: Error getting data from port

Tue Mar 20, 2007 6:56 pm

I tried doing an example using a windows form (like the examples). Looks like this:

Code: Select all

using System;
using System.Windows.Forms;

namespace VirtualComPortGuiExample
{
  public partial class Form1 : Form
  {
    private SerialNET.VPort vport;

    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      SerialNET.License license = new SerialNET.License();
      license.LicenseKey = "<MyLicenseKey>";

      vport = new SerialNET.VPort();

      vport.OnDataFromPort += new SerialNET.OnDataFromPort(vport_OnDataFromPort);

      vport.Timeout = 5000;
      vport.ComPort = 45;
      vport.Created = true;

    }

    private void vport_OnDataFromPort(string Data)
    {
      if (Data != null)
      {
        vport.DataToPort("\r\nOK\r\n");
      }
    }
  }
}
This works fine. Do the VPort object needs to be instantiated in the Gui thread?

Regards,
Thomas Ardal

User avatar
johan
GpsGate
Posts: 14372
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Error getting data from port

Tue Mar 20, 2007 7:03 pm

No VPort does not need to be instantiated in the GUI thread.

There is some support for simply sync with the GUI thread by using VPort.Parent:
http://franson.com/serialtools/reference_manual.asp?comp=serial&class=VPort&item=Parent&platform=net

But I guess that is nothing you want.

Regards,
Johan

Franson Support

ThomasArdal
Posts: 4
Joined: Tue Mar 20, 2007 6:19 pm
Location: Denmark
Contact: Website

RE: Error getting data from port

Tue Mar 20, 2007 7:03 pm

Found the error. the OnDataFromPort event handler needs to be added before setting the Create property on the VPort object.

Sometimes it just helps to talk about the problem :o) Thanks for your help.

Regards,
Thomas Ardal

Torbjörn Nilsson
Posts: 1
Joined: Fri Oct 24, 2008 12:31 pm
Location: Sweden

RE: Error getting data from port

Fri Oct 24, 2008 1:07 pm

Originally posted by ThomasArdal

Found the error. the OnDataFromPort event handler needs to be added before setting the Create property on the VPort object.


Thank you!

I had the same problem and this helped!

BTW: The same applies to all events in the class.

Best regards
Torbjörn Nilsson

Return to “SerialTools”