首页 新闻 搜索 专区 学院

C#怎么捕获USB转串口消息?

0
悬赏园豆:20 [待解决问题]

我现在开发的程序需要与单片机交互,笔记本电脑没有串口所以需要用到USB转串口,在网上找到过一些资料但都只能捕获到插入消息,拨出消息怎么也捕获不到,代码如下:

public const Int32 WM_DEVICE_CHANGE = 0x219;        

public const Int32 DBT_DEVICEARRIVAL = 0x8000;        

public const Int32 DBT_DEVICE_REMOVE_COMPLETE = 0x8004; 

public const Int32 DBT_DEVTYP_PORT = 0x00000003;

[StructLayout(LayoutKind.Sequential)]

public struct DEV_BROADCAST_HDR        

{            

  public UInt32 dbch_size;            

  public UInt32 dbch_devicetype;            

  public UInt32 dbch_reserved;        

}

[StructLayout(LayoutKind.Sequential)]

protected struct DEV_BROADCAST_PORT_Fixed        

{            

  public UInt32 dbcp_size;            

  public UInt32 dbcp_devicetype;            

  public UInt32 dbcp_reserved;        

}

protected override void WndProc(ref Message m)        

{            

  if (m.Msg == WM_DEVICE_CHANGE)            

  {                

    switch (m.WParam.ToInt32())                

    {                    

      case DBT_DEVICEARRIVAL: //串口插入                          

       if (!IsCopy)                            

       {                                

        DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));

                   if (dbhdr.dbch_devicetype ==DBT_DEVTYP_PORT)                     

       {                                    

          IsCopy = true;  

          string portName = Marshal.PtrToStringUni((IntPtr)(m.LParam.ToInt32() + Marshal.SizeOf(typeof(DEV_BROADCAST_PORT_Fixed))));    

       }                            

      }                        

      break;

        case 0x8004://DBT_DEVICE_REMOVE_COMPLETE:    // 串口拔出                

      DEV_BROADCAST_HDR dbhd = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));

                 if (dbhd.dbch_devicetype == DBT_DEVTYP_PORT)   

                  {                                                    

      }                        

       break;                

    }            

   }            

  base.WndProc(ref m);        

}

请高手帮忙指点解决下谢谢!

为了面包的主页 为了面包 | 初学一级 | 园豆:162
提问于:2013-08-19 12:24
< >
分享
所有回答(2)
0

USB转串口后,就是读写串口操作了,不要考虑USB了。注意购买的转接线要注意,Z-Tek这个牌子的好用,我换过好几个。

主要是下面两个方法,你查查使用。

[DllImport("kernel32.dll")]        
private static extern bool ReadFile(
            IntPtr hFile,                         // handle to file
            byte[] lpBuffer,                   // data buffer
            int nNumberOfBytesToRead,          // number of bytes to read
            ref int lpNumberOfBytesRead,       // number of bytes read
            ref OVERLAPPED lpOverlapped        // overlapped buffer
        );

[DllImport("kernel32.dll")]
        private static extern bool WriteFile(
            IntPtr hFile,                         // handle to file
            byte[] lpBuffer,                   // data buffer
            int nNumberOfBytesToWrite,         // number of bytes to write
            ref int lpNumberOfBytesWritten,    // number of bytes written
            ref OVERLAPPED lpOverlapped        // overlapped buffer
        );

[DllImport("kernel32.dll")]
        private static extern bool CloseHandle(
            IntPtr hFile                    // handle to file
         );

kylin.chen | 园豆:983 (小虾三级) | 2013-08-19 13:20

还忘记一个打开串口的方法:
[DllImport("kernel32.dll")]
        private static extern IntPtr CreateFile(
            string lpFileName,                         // file name
            uint dwDesiredAccess,                      // access mode
            uint dwShareMode,                           // share mode
            uint lpSecurityAttributes,                  // SD
            uint dwCreationDisposition,                 // how to create
            uint dwFlagsAndAttributes,                  // file attributes
            int hTemplateFile                          // handle to template file
        );

支持(0) 反对(0) kylin.chen | 园豆:983 (小虾三级) | 2013-08-19 13:29

你这些API都是用来读写打开关闭串口的,跟检测USB消息完全没关系啊,有可以你还没有完全理解我的意思,现在什么都是使用USB,所以现在的串口也是用到USB转串口,既然是USB当然会在使用的过程会被拨出,所以我才会想到使用windows消息来检测USB转串口是否被拨出。

支持(0) 反对(0) 为了面包 | 园豆:162 (初学一级) | 2013-08-26 17:04

@为了面包: 我的意思是既然你转串口了,那就按串口的思路操作了,撇开USB驱动。通过打开串口,是否失败来判断线在不在啊,我没有测试过,不知道线拔出,串口是否打开成功。

支持(0) 反对(0) kylin.chen | 园豆:983 (小虾三级) | 2013-08-29 14:28
0

直接使用SerialPort 进行串口通信不就行了

龙亿 | 园豆:220 (菜鸟二级) | 2013-08-21 22:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册