PDA

View Full Version : سوال: قابلیت خواندن از port در vb



haghi.mahdi
سه شنبه 22 اردیبهشت 1388, 14:38 عصر
سلام
دوستان یک سوال راجع به vb دارم
در vb قابلیت خواندن از portدر از کامبیوتر وجود داره یا نه.اگر بله این قابلیت به چه صورت است.مثل جاوا حالت client server داره؟

aliila
چهارشنبه 23 اردیبهشت 1388, 21:09 عصر
اگر منظورت همان پورت كام يا پرينتر باشه بلي دارد و كلاس به همين منظور است


Public Sub Open(ByVal portname As String, _
ByVal Spd As Integer, ByVal Pty As enumParity, _
ByVal Dtb As Integer, ByVal Stp As enumStopBits)

Dim m_CommDCB As String
Dim m_Baud As String
Dim m_Parity As String
Dim m_Data As String
Dim m_Stop As Byte

hPort = CreateFile(portname, GENERIC_READ + GENERIC_WRITE, _
0, 0, OPEN_EXISTING, 0, 0)

If hPort < 1 Then
Throw New Exception("Can't open the comport! (Errorcode :" _
& GetLastError().ToString() & ")")
End If

m_Baud = Spd.ToString()
m_Parity = PARITYSTRING.Substring(Pty, 1)
m_Data = Dtb.ToString()
m_Stop = Stp

m_CommDCB = String.Format("baud={0} parity={1} data={2} stop={3}", _
m_Baud, m_Parity, m_Data, m_Stop)

BuildCommDCB(m_CommDCB, dcbPort)

If SetCommState(hPort, dcbPort) = 0 Then
Throw New Exception("kan de compoort niet openen(" & _
GetLastError().ToString() & ")")
End If

m_opened = True
End Sub





Public Sub Write(ByVal data As Byte)
Dim dt As Byte()
Dim written As Integer

dt(0) = data 'We have a multi-byte buffer, which you can of

'course enable...


If Opened = True Then
WriteFile(hPort, dt, 1, written, Nothing)
Else
Throw New Exception("Comport not opened")
End If

dt = Nothing
End Sub


Public Function Read() As Byte
Dim rd As Integer
Dim ovl As New OVERLAPPED
Dim dt As Byte()

dt = Array.CreateInstance(GetType(Byte), 1) 'Initialize the buffer


If Opened = True Then
ReadFile(hPort, dt, 1, rd, ovl)
Else
Throw New Exception("Comport not opened")
End If

Return dt(0)
End Function

haghi.mahdi
پنج شنبه 24 اردیبهشت 1388, 23:45 عصر
سلام خیلی ممنون.
اما اگه نخواهیم از پورت پرینتر بخونه چی؟