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
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
解决方案 »
- dateTimePicker1 我想只要日期,不需要时间。怎么设置
- 新手 关于使用 VS 2005 数据设计器编辑数据集 的问题
- WinForm界面中有5个ComboBox如何循环给他们赋值?
- 如何保存网页中的图像到SQLServer数据库(在线等待~~)
- 确认一个低级问题?函数的返回值
- 请教关于DataGrid的问题~!
- VS 2005里有BUG管理这个功能吗?
- 找c#的类库文档■C#类库电子文档■
- ▃▄▆500分提供,有关P/Invoked的问题。▆▄▃
- List.GroupBy(x => x.x1)是否可以对两个字段进行分组
- 请大家谈谈Visual Studio 2002与Visual Studio 2003有异同点!
- 怎样让一个窗体总是在桌面的最前面,不会被其他程序的窗口挡住?
权威代码
http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/default.aspx
关于PCI数据采集卡通信,我不知怎么做,学习
{
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;
}
} }