Rinnakkaisportin datalinjojen ohjaus suoraan ohjelmasta
onnistuu QB:llä OUT tempulla, mutta miten vastaava
homma onnistuu VB5:lla ?
Kaima!!
Jopas heitit visasen... Mietin asiaa ja ainoa kylmiltään mieleen tuli MSComm-ActiveX komponentti, mutta se ainakaan dokumentaation mukaan tuoe kuin sarjaportteja.
Printer-olio osaa lähettää sarjaporttiin tavaraa, muttei muutoin kykene ohjata datalinjoja.
WinAPI:sta pitäisi löytyä vastaus...
Jotain tälläistä koodia löysin kun pyyhin pölyjä...
Private Declare Function ConfigurePort Lib "winspool.drv" Alias "ConfigurePortA" (ByVal pName As String, ByVal hwnd As Long, ByVal pPortName As String) As Long
Private Sub Command1_Click()
MsgBox ConfigurePort("", Me.hwnd, "COM1:")
MsgBox ConfigurePort("", Me.hwnd, "LPT1:")
End SubSeuraava koodi ei ole omaa tavaraa vaan ladattu joskus jostain vihjelistalta (en muista mistä). Koodi pitäisi toimia vain 95,98 ja NT alustalla, siispä en edes vaivautunut tesstaamaan sitä W2000 - serverilläni.
Moduliin seuraava koodi:
Option Explicit
'API calls
Private Declare Function EnumPorts Lib "winspool.drv" Alias "EnumPortsA" (ByVal pName As String, ByVal Level As Long, ByVal lpbPorts As Long, ByVal cbBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
Private Declare Function lstrlenW Lib "kernel32.dll" (ByVal lpString As Long) As Long
Private Declare Sub CopyMem Lib "kernel32.dll" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function HeapAlloc Lib "kernel32.dll" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GetProcessHeap Lib "kernel32.dll" () As Long
Private Declare Function HeapFree Lib "kernel32.dll" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long
'API Structures
Private Type PORT_INFO_2
pPortName As String
pMonitorName As String
pDescription As String
fPortType As Long
Reserved As Long
End Type
Private Type API_PORT_INFO_2
pPortName As Long
pMonitorName As Long
pDescription As Long
fPortType As Long
Reserved As Long
End Type
'Public Data Structure - up to 100 Ports Information
Public Ports(0 To 100) As PORT_INFO_2
Public Function TrimStr(strName As String) As String
'Finds a null then trims the string
Dim x As Integer
x = InStr(strName, vbNullChar)
If x > 0 Then TrimStr = Left(strName, x - 1) Else TrimStr = strName
End Function
Public Function LPSTRtoSTRING(ByVal lngPointer As Long) As String
Dim lngLength As Long
'Get number of characters in string
lngLength = lstrlenW(lngPointer) * 2
'Initialize string so we have something to copy the string into
LPSTRtoSTRING = String(lngLength, 0)
'Copy the string
CopyMem ByVal StrPtr(LPSTRtoSTRING), ByVal lngPointer, lngLength
'Convert to Unicode
LPSTRtoSTRING = TrimStr(StrConv(LPSTRtoSTRING, vbUnicode))
End Function
'Use ServerName to specify the name of a Remote Workstation i.e. "//WIN95WKST"
'or leave it blank "" to get the ports of the local Machine
Public Function GetAvailablePorts(ServerName As String) As Long
Dim ret As Long
Dim PortsStruct(0 To 100) As API_PORT_INFO_2
Dim pcbNeeded As Long
Dim pcReturned As Long
Dim TempBuff As Long
Dim i As Integer
'Get the amount of bytes needed to contain the data returned by the API call
ret = EnumPorts(ServerName, 2, TempBuff, 0, pcbNeeded, pcReturned)
'Allocate the Buffer
TempBuff = HeapAlloc(GetProcessHeap(), 0, pcbNeeded)
ret = EnumPorts(ServerName, 2, TempBuff, pcbNeeded, pcbNeeded, pcReturned)
If ret Then
'Convert the returned String Pointer Values to VB String Type
CopyMem PortsStruct(0), ByVal TempBuff, pcbNeeded
For i = 0 To pcReturned - 1
Ports(i).pDescription = LPSTRtoSTRING(PortsStruct(i).pDescription)
Ports(i).pPortName = LPSTRtoSTRING(PortsStruct(i).pPortName)
Ports(i).pMonitorName = LPSTRtoSTRING(PortsStruct(i).pMonitorName)
Ports(i).fPortType = PortsStruct(i).fPortType
Next
End If
GetAvailablePorts = pcReturned
'Free the Heap Space allocated for the Buffer
If TempBuff Then HeapFree GetProcessHeap(), 0, TempBuff
End FunctionJa käyttö:
Option Explicit
'Create a Form with a Command Button and a List
Private Sub Command1_Click()
Dim NumPorts As Long
Dim i As Integer
'Get the Numbers of Ports in the System
'and Fill the Ports Structure
NumPorts = GetAvailablePorts("")
'Fill the List with the available Ports
List1.Clear
For i = 0 To NumPorts - 1
List1.AddItem Ports(i).pPortName
Next
End SubOlisiko noista jotain apua...
Olisi muuten erittäin kiva tietää miten se onnisttu kun käyttis on Windows 2000, ME tai XP.
Juu... niin varmaan....mitä sinun pitää tehdä on etsiä vastaavat kyseisten käyttöjärjestelmien vastaavat API-kutsut.
Olenpa monesti yrittänyt etsiä, mutta ilman tulosta. Eikä se nyt maailmaa kaada vaikka en sitä koskaan tietäisi. Kunhan kysyin jos joku sattuisi tietämään.
Joo... W98:lla ja NT-pohjaisilla Windowseilla NT4.0 ja W2000 on hieman eri kutsut - niissä on vain vähän eroa. MSDN kyllä kuvaa kaikki ja kertoo niinpäin missä käyttöjärjestelmässä mikäkin API-kutsu toimii, mutta sen vastaavaa W98:ssa tai W2000:ssa se ei kerro...
Täältä Ohjelmointiputkasta löytyy yksi esimerkki, en tiedä, että onko tämä tarvitsemasi mutta käy katsomassa:
https://www.ohjelmointiputka.net/koodivinkit/
Aihe on jo aika vanha, joten et voi enää vastata siihen.