Const GENERIC_READ = &H80000000
    Const GENERIC_WRITE = &H40000000    Const OPEN_EXISTING = 3    Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
    (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As IntPtrPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim iHandle As IntPtr
        iHandle = CreateFile("LPT1", GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
        If (iHandle.ToInt32 = -1) Then
            MsgBox("can not open port")
        Else
            MsgBox("Open Succes!")
            Dim fs As New FileStream(iHandle, FileAccess.ReadWrite)
            Dim sr As New StreamReader(fs) '读数据
            Dim sw As New StreamWriter(fs) '写数据
        End If
    End Sub

解决方案 »

  1.   

    关于串口:
    权威代码
    http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/default.aspx
    关于PCI数据采集卡通信,我不知怎么做,学习
      

  2.   

    public bool PrintBar(string vehicleName,string cardId,string vehicleClass,string fileName)
    {
    if (barCommand==null)
    {
    GetBarCommand();
    }
    try
    {
    System.IntPtr  pHandle;
    int handle;
    //opens the existing file...
    handle = CreateFile(fileName,
    GENERIC_WRITE,
    0, 
    0,
    OPEN_EXISTING,
    0,
    0);
        
    pHandle=new System .IntPtr (handle);
    FileStream fs=new FileStream (pHandle,FileAccess.Write );

    if (((byte)vehicleName[0])>128)
    {
    string temp=vehicleName.Substring (1,vehicleName.Length -1);
    vehicleName=" " + temp;
    }
    else
    {
    vehicleName=" "+ vehicleName;
    }
    string s;
    if ((fileName.IndexOf ("O")>0)||(fileName.IndexOf ("o")>0))
    {
      s=string.Format ("{0}\r\n{1}","9600,N,8,1,P",barCommand);
    }
    else
    { s=barCommand;
    }
    StringBuilder sb=new StringBuilder (s);
    sb.Replace ("@cardId",cardId);
    sb.Replace ("@vehicleClass",vehicleClass);
    sb.Replace ("@vehicleName",vehicleName);
    s=sb.ToString () ; byte[] byteOut; 
    Int32 dwCount = s.Length;
    byteOut = new byte[dwCount]; 
    for (int i = 0;i<dwCount;i++)  
    {    
    //  ByteStrings.to 
    byteOut[i] = System.Convert.ToByte( s[i]);  }  
    // byteOut=Encoding.ASCII .GetBytes (s);

    // How many characters are in the string?
    fs.Write (byteOut,0,dwCount);
    fs.Flush ();
    fs.Close ();
    return true;
    }
    catch(Exception e)
    {
    throw e;

    }
    } }