如何在C#中从USB虚拟COM读取数据? [英] How do I read data from USB Virtual COM in C#?

查看:109
本文介绍了如何在C#中从USB虚拟COM读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在搜索互联网上的解决方案,但没有运气.

我正在使用Windows窗体串行端口控件和richTextBox.我正在尝试从呼叫者ID设备(CTI彗星)读取电话号码 呼叫者ID),它会从电话线上解码呼叫者的电话号码,并将其显示在我的PC的虚拟COM5端口上,因为呼叫者ID是COM设备.

呼叫者ID通过USB转COM适配器连接到我的PC,因为我的PC没有物理COM端口. USB到COM适配器驱动程序也安装正确.请注意,电话线支持来电显示功能.

当电话铃响起时,电话号码应显示在richTextBox中,我尝试了以下代码,但没有任何反应(电话号码未出现),我是否丢失了某些东西?

我的问题:

如何从C#中的虚拟COM5端口读取数据?请帮助我修改此代码以使其起作用.谢谢

  public   { 公共  mySerialPort   公共 ()  {  InitializeComponent  .  + =     SerialDataReceivedEventHandler  ( . 
      }  公共     { 如果  richTextBox1  ) 
           { .  ( 发件人 }  其他  { .  mySerialPort  ());  

           }  }  私有  serialPort1_DataReceived  发件人     { }  }   


解决方案

如果它显示为标准COM端口,应该可以,但是您的代码不正确.

首先,在创建表单时,您不应该打开端口.构造函数早在显示之前就已被调用(如果有的话).此外,ctor由设计人员调用,因此即使您尝试在VS中设计表单,它也会试图打开 港口.您需要将实际的打开调用移至该过程的后面(OnLoad是个好地方).

第二,您可能应该考虑将SerialPort从工具箱拖放到窗体上,而不是自己创建字段并对其进行初始化.这很重要,原因有两个.首先,它将删除您拥有的所有代码,除了 您的处理程序和公开通话.其次,它将端口同步到您的UI线程,从而完全不需要您使用InvokeRequired.该事件将在UI线程上引发.

接下来,您有一个serialPort1_DataReceived处理程序,使我相信您已经从设计器中拖放了一个端口.它可以消失.

最后,除了名称之外,您还没有配置其他端口.通常,您需要指定波特率,奇偶校验等才能使串行通信正常工作.这些值必须与COM端口上的设备一致,并且应该在您的文档中.

请参阅 MSDN 有关使用串行端口类的完整示例.

迈克尔·泰勒
http://blogs.msmvps.com/p3net


I searched for a solution on the internet but no luck.

I am using windows form with Serial Port control and richTextBox. I am trying to read the phone number from caller ID device (CTI comet caller ID) which decodes the Caller's telephone number from the telephone line and presenting it on a virtual COM5 Port of my PC because the Caller ID is a COM device.

The caller ID is connected to my PC via USB to COM Adapter because my PC doesn't have physical COM ports. Also the USB to COM Adapter driver is installed well.Note that the telephone line supports the caller ID feature.

When the phone rings the phone number should show up in richTextBox, I tried the following code but nothing happens (phone number did not appear) Am I missing something?.

My question:

How can I read data from virtual COM5 port in C#? Please help me to modify this code to make it work. Thank you

 public partial class Form1 : Form
    {
      public  SerialPort mySerialPort = new SerialPort("COM5");

    public Form1()
    {
        InitializeComponent();

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataR);
        mySerialPort.Open();
     }

    public void DataR(object sender, SerialDataReceivedEventArgs e)

    {

        if (richTextBox1.InvokeRequired)
         {
             richTextBox1.Invoke(new SerialDataReceivedEventHandler(DataR), sender, e);
         }

         else
         {

            richTextBox1.AppendText(mySerialPort.ReadExisting()); //recieve data in real time.

          }
      }


    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {

    }
}


解决方案

Provided it shows up as a standard COM port it should be fine but your code is not correct.

Firstly you should not be opening the port when the form is created. The constructor is called long before it is ever shown (if at all). Furthermore the ctor is called by the designer so even when you're trying to design the form in VS it is trying to open the port.  You need to move the actual open call to later in the process (OnLoad is a good place). 

Secondly you should probably consider dragging and dropping the SerialPort from the toolbox onto your form rather than creating a field and initializing it yourself. This is important for a couple of reasons. Firstly it removes all the code you have except your handler and the open call.  Secondly it syncs the port up to your UI thread which eliminates the need for you to use InvokeRequired at all.  The event will be raised on the UI thread.

Next you have a serialPort1_DataReceived handler which leads me to believe you already dragged and dropped a port from the designer. It can go away.

Lastly, you haven't configured the port other than the name. In general you need to specify baud rate, parity, etc in order for serial communication to work. Those values must agree with the device that is on the COM port and should be in your documentation. 

Refer to MSDN for a full example of using the serial port class.

Michael Taylor
http://blogs.msmvps.com/p3net


这篇关于如何在C#中从USB虚拟COM读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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