-
Zugriff auf Serielle Schnittstelle aus RPG/VARPG
Moin, moin,
ich möchte bzw muss mit RPG/VARPG auf ein Gerät zugreifen, welches über die RS232 Schnittstelle angeschlossen ist. Das Gerät ist ein Handscanner, welcher eine Art RFID Chip ausliest. Dabei muss ich über die serielle Schnittstelle zum einen Befehle an das Gerät senden und zum anderen die Daten die das Gerät aus den Chips ausliest in mein Programm bekommen. Übers Hyperterminal geht das natürlich simpel, aber wie ich überhaupt auf die serielle Schnittstelle mit RPG/VARPG zugreife, darüber finde ich weder hier noch generell im Internet Informationen.
Hat wer von euch Infos wie man sowas bewerkstelligt? Oder ein Beispielprogramm, bei dem ich die generelle Vorgehensweise ersehen kann.
Schon mal danke im vorraus für die Hilfe!!!
Betriebssystem ist V5R2 und VARPG Version ist 5.1.2
Schönen Feierabend nachher!!
MFG KAMPI
-
Hallo Kampi,
bzgl. VARPG und COM-Ports habe ich folgendes gefunden:
Using COM ports in VARPG
by Les Lieurance
This tip from VARPG developer Les Lieurance shows how to open a COM port (or any port of file for that matter) and send information out the port. Les uses three Windows DLL functions, CreateFileA, WriteFile, and CloseHandle, to accomplish the task. Because Les is using Windows DLLs that often require null terminated strings, he appends a null character, X'00', to the end of the strings. To open COM1:, use COM1 in VARPG part ENT1. To open COM2:, use COM2 in VARPG part ENT1.
************************************************** *******************
* Author . . . . : Les Lieurance, Braden Carco Gearmatic *
* Program ID . . : Com port *
* *
* Description . : Sample program to write data to a com port *
* *
* *
* *
************************************************** *******************
HDEBUG(*YES)
D************************************************* ********************
D* Function Prototypes ****
D** ****
D************************************************* **************************
D* Crtfile: CreateFile function from the Kernel32.dll. **Used to Open Com Port**
D************************************************* **************************
D CRTFILE PR 10U 0 ExtProc('CreateFileA')
0002 D DLL('Kernel32.dll')
0003 D Linkage(*StdCall)
D * VALUE OPTIONS(*STRING)
D 10I 0 Value
D 10I 0 Value
D $SEC LIKE(secattr)
D 10I 0 Value
D 10I 0 Value
D 10I 0 Value
D************************************************* **************************
D* WriteFile WriteFile function from the Kernel32.dll **Used to write to com port**
D************************************************* ***************************
D WRTFILE PR 10U 0 ExtProc('WriteFile')
0002 D DLL('Kernel32.dll')
0003 D Linkage(*StdCall)
D 10I 0 VALUE
D * VALUE OPTIONS(*STRING)
D 10U 0 Value
D $WRITE LIKE(WRITE)
D $OVER LIKE(OVERLAP)
D************************************************* ****************************
D* CloseFile CloseHandle function from the Kernel32.djll **Used to close the com port
D************************************************* **************************
D CLOSEFILE PR 10U 0 ExtProc('CloseHandle')
0002 D DLL('Kernel32.dll')
0003 D Linkage(*StdCall)
D 10I 0 VALUE
D************************************************* ****************************
D* Miscellaneous Initializations
D************************************************* ****************************
D secattr ds
D LENGTH 10I 0 INZ(80)
D SECDESC 10I 0 INZ(0)
D BOOL 10I 0 INZ(0)
D name S 10A
D access S 10U 0 INZ(3221225472)
D share S 10U 0 INZ(0)
Dcreate S 10I 0 INZ(3)
Dattr S 10U 0 INZ(128)
Dtemplate S 10I 0 INZ(0)
DBRC S 10U 0
DRET S 10I 0
DRET2 S 10U 0
DWRITE S 10U 0
DOVERLAP S 10U 0
D
D************************************************* ********************
D************************************************* ********************
* Notes: *
************************************************** *******************
*
C START BEGACT PRESS WIN2
C EVAL ENT1= %GETATR('WIN2':'ENT1':'TEXT') Get com port
C EVAL ENT1 = %TRIM(ENT1)+X'00' add null character t
C EVAL ENT2= %GETATR('WIN2':'ENT2':'TEXT') get data
C EVAL ENT2 = %TRIM(ENT2)+X'00' add null character
C*
C*
C Eval BRC = CRTFILE(ENT1:ACCESS:share:secattr: CreateFile function
C create:attr:template)
C Eval RET =WRTFILE(BRC:ENT2:LENGTH:WRITE:OVERLAP) WriteFile Function
C Eval RET2 = Closefile(brc) Close Com Port
C ENDACT
Gruß,
KM
-
Danke KM für den Tipp.
Werde mich da heute mal durcharbeiten und gucken ob ich es zum laufen bringe bzw da überhaupt durchsteige mit meiner kurzen RPG-Erfahrung.
Allen einen schönen und ruhigen Arbeitstag!
MFG KAMPI
-
Also in "ENT1" steht ja das ,was geöffnet werden soll.
Und in "ENT2" was geschrieben werden soll.
Ich habe errausgehfunden, dass wenn ich "ENT1" z.B. mit "C:\test.txt" fülle und "ENT2" mit "Test schreiben", dann öffnet er die Datei test.txt auf C: und schreibt den Inhalt von "ENT2" dort hinein. Anschliessen schliesst er die Datei wieder. Natürlich alles im Hintergrund, also die Datei geht nicht wirklich auf. Er hat halt nur zugriff darauf.
Aber ich will ja auf den Com-Port 1 zugreifen. Also habe ich in "ENT1" einfach "COM1" geschrieben. Leider funzt das aber nicht. In der Variable "RET" steht auch nachher der Fehlercode "0", da nichts geöffnet war, wo er reinschreiben konnte. (Als ich es mit der Datei gemacht habe, wo es ja geklappt hat, war die Variable "RET" auf 1.)
Es muss also daran liegen, was ich in "ENT1" schreibe, die Frage ist jetzt, was muss ich da reinschreiben, um den 1. Com-Port anzusprechen!??
Danke für weitere Hilfe im vorraus.
Habe über die kernel32.dll Funktionen noch folgende Seite gefunden (zwar für VBA, aber da kann man einiges erkennen. Habe so das mit den Fehlercodes und dem öffnen der Datei rausbekomme.
http://www.vbapihelpline.de/ref/func...eFile&Letter=c
Außerdem auf einer Seite noch folgenden VBA Code, der einen Com-Port öffnet, leider kann ich mangels VBA Kenntniss, nicht erkennen, was bei CreateFile steht um den Com Port zu öffnen.
Code:
Private Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" ( _
ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByRef lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
'
Private Declare Function CloseHandle Lib "kernel32.dll" ( _
ByVal hObject As Long) As Long
'
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const OPEN_EXISTING As Long = 3
'
Public Function OpenComPort(ByVal sPort As String) As Long
Dim hCom As Long
hCom = CreateFile("\\.\" & sPort, _
GENERIC_READ Or GENERIC_WRITE, _
0, ByVal 0, OPEN_EXISTING, 0, ByVal 0)
OpenComPort = hCom
End Function
'
Public Sub CloseComPort(ByVal hCom As Long)
CloseHandle hCom
End Sub
MFG KAMPI
-
Doch noch mal den VBA Code angeguckt, "ENT1" muss wohl
heissen. Macht den ohne Fehler auf und zu. Wenn ich vorher Hyperterminal geöffnet habe und auf den Port zugegriffen habe, dann läuft das Prog auf nen Fehler, also wird er wirklich den 1. Com Port öfnnen. Was jetzt noch nicht funzt ist der Befehl "lftx" den ich absetzen muss.
MFG KAMPI
-
So Fehler noch weiter eingekreist. Habe als ENT2, also das was gesendet werden soll "lftx" stehen. Habe das jetzt noch mal mit dem schreiben in eine Datei gemacht, um zu gucken was wirklich drin steht, da es über den Com Port immer noch nciht klappt mit dem Befehl, aber ich da ja nicht sehe, was üebrhaupt übertragen wird.
In der Textdatei steht nicht, wie ich angenommen habe nur "lftx" sodern folgendes:
lftx Ð× Ó× Ð× lftx txt ÿÿÿÿ ÿÿÿÿ 3 àÿÿÿÎt t ° Ìt
Wenn ich das sehe, kann ich mir vorstellen warum das über Com Port nicht klappt, weil mein Gerät "lftx" erwartet und nciht das ganze Zeug hintendran.
Waran kann es denn liegen, das son "Müll" noch dahinter steht???
MFG KAMPI
P.S. Sorry das es fast zum monolog hier wird, will nur immer posten was ich zum Problem noch errausgefunden habe.
-
Beim WriteFile muss die zu schreibende Länge korrekt übergeben werden !
-
Danke fuerchau für die Antwort, aber habe folgendes Problem:
Also wenn ich bei Length (80) eintrage, kommt bei nach dem wrtfile in der Variable write 80 zurück. Bei 99 bei Length kommt 99 in write zurück. Wenn ich bei Length mehr eintrage, führt er das wrtfile nciht aus. Wie finde ich denn die Länge von ENT2 raus? Weil der hängt da ja noch son "Müll" dran wie oben beschrieben?
Mfg KAMPI
-
Hallo Kampi,
ich antworte mal einfach.
Testweise würde ich mal folgendes versuchen:
-Eine andere variable für ENT2 (schreiben) mit varying definieren.
- vorher clear auf die variable
- Wert füllen mit X'00'
- länge der variable mit %len ermitteln
- entsprechend Länge mit übergeben.
Vielleicht hilfts
Gruss
Michael
-
WrtFile gibt die Anzahl der geschriebenen Daten zurück !
Die zu schreibende Länge wird im Parameter Length vorgegeben !!!
Schau dir obiges Beispiel GENAU an und nimm ggf. ein C/C++-Handbuch hinzu.
-
So habs jetzt alles hinbekommen, ein wenig anders als in dem Beipsiel, da das nicht so richtig geklappt hat. Trotzdem danke an alle für die Hilfen!!
Sieht jetzt folgendermaßen aus:
PHP-Code:
D* D* Create File D* D CRTFILE PR 10U 0 ExtProc('CreateFileA') D DLL('Kernel32.dll') D Linkage(*StdCall) D * VALUE OPTIONS(*STRING) D 10I 0 Value D 10I 0 Value D $SEC LIKE(secattr) D 10I 0 Value D 10I 0 Value D 10I 0 Value D* D* Write File D* D WRTFILE PR 10U 0 ExtProc('WriteFile') D DLL('Kernel32.dll') D Linkage(*StdCall) D 10I 0 Value D * VALUE OPTIONS(*STRING) D 10I 0 Value D $WRITE LIKE(WRITE) D $OVER LIKE(OVERLAP) D* D* Read File D* D READFILE PR 10U 0 ExtProc('ReadFile') D DLL('Kernel32.dll') D Linkage(*StdCall) D 10I 0 Value D * VALUE OPTIONS(*STRING) D 10I 0 Value D $WRITE LIKE(WRITE) D $OVER LIKE(OVERLAP) D* D* Close File D* D CLOSEFILE PR 10U 0 ExtProc('CloseHandle') D DLL('Kernel32.dll') D Linkage(*StdCall) D 10I 0 Value D* D* Initializations D* D secattr DS D LENGTH 10I 0 INZ(80) D SECDESC 10I 0 INZ(0) D BOOL 10I 0 INZ(0) D name S 10A D access S 10U 0 INZ(3221225472) D share S 10U 0 INZ(0) Dcreate S 10I 0 INZ(3) Dattr S 10U 0 INZ(128) Dtemplate S 10I 0 INZ(0) DBRC S 10U 0 DRET S 10I 0 DRET2 S 10U 0 DRET3 S 10U 0 DWRITE S 10U 0 DOVERLAP S 10U 0 DENT1 S 10A DENT2 S 80A DENT3 S 180A DaENT1 S * inz(%addr(ENT1)) DaENT2 S * inz(%addr(ENT2)) DaENT3 S * inz(%addr(ENT3)) C TEST BEGACT PRESS FRA000000B C EVAL zaehler = 0 C EVAL sstemp = 0 C TIME SysZeit C DOW zaehler < 4 C EVAL sstemp = ss C EVAL zaehler = zaehler + 1 C EVAL ENT1 = '\\.\com1' + X'00' C EVAL ENT2 = X'0D' + 'lftx' + X'0D' + X'00' C EVAL BRC = CRTFILE(aENT1:ACCESS:share: C secattr:create:attr:template) C EVAL RET = WRTFILE(BRC:aENT2:LENGTH:WRITE C :OVERLAP) C EVAL RET2 = Closefile(brc) C CLEAR ENT1 C CLEAR ENT2 C CLEAR BRC C CLEAR RET C CLEAR RET2 C CLEAR WRITE C DOW sstemp = ss C TIME SysZeit C ENDDO C ENDDO C EVAL ENT1 = '\\.\com1' + X'00' C EVAL ENT2 = X'0D' + 'last 1' + X'0D' + X'00' C EVAL BRC = CRTFILE(aENT1:ACCESS:share: C secattr:create:attr:template) C* C EVAL RET = WRTFILE(BRC:aENT2:LENGTH:WRITE C :OVERLAP) C* C EVAL Length = 180 C EVAL RET3 = READFILE(BRC:aENT3:Length:WRITE C :OVERLAP) C* C EVAL RET2 = Closefile(brc) C EVAL ID = %subst(ENT3:135:10) C EVAL FRA000000B.ID.TEXT = ID C EVAL TEMP = %subst(ENT3:154:2) C EVAL FRA000000B.TEMP.TEXT = TEMP C EVAL DAUER = %subst(ENT3:162:3) C EVAL FRA000000B.DAUER.TEXT = DAUER C CLEAR ENT1 C CLEAR ENT2 C CLEAR BRC C CLEAR RET C CLEAR RET2 C CLEAR WRITE C ENDACT
Mfg KAMPI
-
Für den WriteFile würde ich dir noch empfehlen, die Variable LENGTH mit der tatsächlichen Länge der Daten zu füllen, da die Leerzeichen immer mit ausgegeben werden (die Länge ist ja mit 80 initialisiert). Du benötigst auch X'00' nicht !
In der Variablen WRITE wird die tatsächliche Anzahl geschriebener Zeichen zurückgegeben.
WriteFile
The WriteFile function writes data to a file and is designed for both synchronous and asynchronous operation. The function starts writing data to the file at the position indicated by the file pointer. After the write operation has been completed, the file pointer is adjusted by the number of bytes actually written, except when the file is opened with FILE_FLAG_OVERLAPPED. If the file handle was created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the write operation is finished.
BOOL WriteFile(
HANDLE hFile, // handle to file to write to
LPCVOID lpBuffer, // pointer to data to write to file
DWORD nNumberOfBytesToWrite, // number of bytes to write
LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written
LPOVERLAPPED lpOverlapped // pointer to structure for overlapped I/O
);
Parameters
hFile
Handle to the file to be written to. The file handle must have been created with GENERIC_WRITE access to the file.
Windows NT:
For asynchronous write operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by thesocket oraccept functions.
Windows 95:
For asynchronous write operations, hFile can be a communications resource, mailslot, or named pipe handle opened with the FILE_FLAG_OVERLAPPED flag by CreateFile, or a socket handle returned by the socket or accept functions. Windows 95 does not support asynchronous write operations on disk files.
lpBuffer
Pointer to the buffer containing the data to be written to the file.
nNumberOfBytesToWrite
Number of bytes to write to the file.
A value of zero specifies a null write operation. A null write operation does not write any bytes but does cause the time stamp to change.
Named pipe write operations across a network are limited to 65,535 bytes.
lpNumberOfBytesWritten
Pointer to the number of bytes written by this function call. WriteFile sets this value to zero before doing any work or error checking.
Windows NT: If lpOverlapped is NULL, lpNumberOfBytesWritten cannot be NULL.
Windows NT: If lpOverlapped is not NULL, lpNumberOfBytesWritten can be NULL. If this is an overlapped write operation, you can get the number of bytes written by calling GetOverlappedResult. If hFile is associated with an I/O completion port, you can get the number of bytes written by calling GetQueuedCompletionStatus.
Windows 95 and Windows 98: This parameter cannot be NULL.
lpOverlapped
Pointer to an OVERLAPPED structure. This structure is required if hFile was opened with FILE_FLAG_OVERLAPPED.
If hFile was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL. It must point to a valid OVERLAPPED structure. If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the function can incorrectly report that the write operation is complete.
If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the write operation starts at the offset specified in the OVERLAPPED structure and WriteFile may return before the write operation has been completed. In this case, WriteFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue processing while the write operation is being completed. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the write operation.
If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the write operation starts at the current file position and WriteFile does not return until the operation has been completed.
If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the write operation starts at the offset specified in the OVERLAPPED structure and WriteFile does not return until the write operation has been completed.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Ebenso gibt die Funktion ReadFile im Parameter WRITE die Anzahl gelesener Zeichen zurück. Dies ist insbesonders wichtig, da beim Lesen von COM die Daten ggf. nicht sofort vollständig kommen !
ReadFile
The ReadFile function reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes actually read, unless the file handle is created with the overlapped attribute. If the file handle is created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the read operation.
BOOL ReadFile(
HANDLE hFile, // handle of file to read
LPVOID lpBuffer, // pointer to buffer that receives data
DWORD nNumberOfBytesToRead, // number of bytes to read
LPDWORD lpNumberOfBytesRead, // pointer to number of bytes read
LPOVERLAPPED lpOverlapped // pointer to structure for data
);
Parameters
hFile
Handle to the file to be read. The file handle must have been created with GENERIC_READ access to the file.
Windows NT:
For asynchronous read operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by thesocket oraccept functions.
Windows 95:
For asynchronous read operations, hFile can be a communications resource, mailslot, or named pipe handle opened with the FILE_FLAG_OVERLAPPED flag by CreateFile, or a socket handle returned by the socket or accept functions. Windows 95 does not support asynchronous read operations on disk files.
lpBuffer
Pointer to the buffer that receives the data read from the file.
nNumberOfBytesToRead
Number of bytes to be read from the file.
lpNumberOfBytesRead
Pointer to the number of bytes read. ReadFile sets this value to zero before doing any work or error checking. If this parameter is zero when ReadFile returns TRUE on a named pipe, the other end of the message-mode pipe called the WriteFile function with nNumberOfBytesToWrite set to zero.
Windows NT: If lpOverlapped is NULL, lpNumberOfBytesRead cannot be NULL.
Windows NT: If lpOverlapped is not NULL, lpNumberOfBytesRead can be NULL. If this is an overlapped read operation, you can get the number of bytes read by calling GetOverlappedResult. If hFile is associated with an I/O completion port, you can get the number of bytes read by calling GetQueuedCompletionStatus.
Windows 95 and Windows 98: This parameter cannot be NULL.
lpOverlapped
Pointer to an OVERLAPPED structure. This structure is required if hFile was created with FILE_FLAG_OVERLAPPED.
If hFile was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL. It must point to a valid OVERLAPPED structure. If hFile was created with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the function can incorrectly report that the read operation is complete.
If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure and ReadFile may return before the read operation has been completed. In this case, ReadFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue while the read operation finishes. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the read operation.
If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the read operation starts at the current file position and ReadFile does not return until the operation has been completed.
If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure. ReadFile does not return until the read operation has been completed.
Hier hast du ggf. noch ein Problem, wenn mal keine Antwort kommt, da du keinen Timeout angeben kannst.
Similar Threads
-
By schatte in forum NEWSboard Linux
Antworten: 12
Letzter Beitrag: 29-01-08, 14:02
-
By marmart in forum IBM i Hauptforum
Antworten: 5
Letzter Beitrag: 25-09-07, 15:29
-
By schatte in forum NEWSboard Windows
Antworten: 7
Letzter Beitrag: 21-11-06, 11:37
-
By pwrdwnsys in forum IBM i Hauptforum
Antworten: 2
Letzter Beitrag: 07-11-06, 15:34
-
By cami in forum IBM i Hauptforum
Antworten: 3
Letzter Beitrag: 27-08-06, 17:31
Berechtigungen
- Neue Themen erstellen: Nein
- Themen beantworten: Nein
- You may not post attachments
- You may not edit your posts
-
Foren-Regeln
|
Erweiterte Foren Suche
Google Foren Suche
Forum & Artikel Update eMail
AS/400 / IBM i
Server Expert Gruppen
Unternehmens IT
|
Kategorien online Artikel
- Big Data, Analytics, BI, MIS
- Cloud, Social Media, Devices
- DMS, Archivierung, Druck
- ERP + Add-ons, Business Software
- Hochverfügbarkeit
- Human Resources, Personal
- IBM Announcements
- IT-Karikaturen
- Leitartikel
- Load`n`go
- Messen, Veranstaltungen
- NEWSolutions Dossiers
- Programmierung
- Security
- Software Development + Change Mgmt.
- Solutions & Provider
- Speicher – Storage
- Strategische Berichte
- Systemmanagement
- Tools, Hot-Tips
Auf dem Laufenden bleiben
|
Bookmarks