Restored from previous forum. Originally posted by dangur.
How would one go about opening com ports for read/write ? Setting buffers, handshaking, etc...
Com ports
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Rings.
dear dangur,
I'm working on a Windows-API-Version to implement COMPort-access.
Today PB doesn't support Seriell native.
I try to convert some VB-Examples, but have many problems due PB.
stay tuned, i'm (with a little help from fred) working on it.
I would be more happy, if PB has native access to Com-port.
Siggi
dear dangur,
I'm working on a Windows-API-Version to implement COMPort-access.
Today PB doesn't support Seriell native.
I try to convert some VB-Examples, but have many problems due PB.
stay tuned, i'm (with a little help from fred) working on it.
I would be more happy, if PB has native access to Com-port.
Siggi
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by dangur.
Thanks much for the information. I'll be looking forward to seeing it down the road. Most of the programming I do is with serial data in industrial application and believe it or not, I find quickbasic (not VB ) the quickest way to test an idea.
Thanks much for the information. I'll be looking forward to seeing it down the road. Most of the programming I do is with serial data in industrial application and believe it or not, I find quickbasic (not VB ) the quickest way to test an idea.
dear dangur,
I'm working on a Windows-API-Version to implement COMPort-access.
Today PB doesn't support Seriell native.
I try to convert some VB-Examples, but have many problems due PB.
stay tuned, i'm (with a little help from fred) working on it.
I would be more happy, if PB has native access to Com-port.
Siggi
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Rings.
Yes i'm also one of the Pro's and use often seriell-protocolls(work on SCADA and process-informationsystems) and know a lot about these topics.But also in the Windows-world we have to use System-Apis to use the seriell-interface
.
I know that in a DOS-Compiler the use of Seriell is very easy (Not only in QuickB,also in PowerBasic for DOS and WINDOWS !)But I prefer the multi-plattform of PureBasic to convert some Stuff easily from WIN to Linux.
There is today NO pure native Basic-Compiler on the market which can do both plattforms,Windows and Linux.But first there must be a (normaly) easy way to do these stuff under Windows.
If my Seriell-stuff works, we should put it into a library(and make the same
lib onto LINUX-version).
There are many libs for making games (DirectX etc..).I'm unluck about that, but that's life and fred do only the things
that most of the user's want.I'm also happy about the TCP-implementation,cause this a bit more of Pro-Stuff.Also the direct access over MemoryBanks(PEEK,POKE)
are great.Also you can put some real assembler-stuff into your code.
Fred is implementing some ODBC-Access into PureBAsic;great for Pro's
We will see what next happening.
Siggi
Yes i'm also one of the Pro's and use often seriell-protocolls(work on SCADA and process-informationsystems) and know a lot about these topics.But also in the Windows-world we have to use System-Apis to use the seriell-interface

I know that in a DOS-Compiler the use of Seriell is very easy (Not only in QuickB,also in PowerBasic for DOS and WINDOWS !)But I prefer the multi-plattform of PureBasic to convert some Stuff easily from WIN to Linux.
There is today NO pure native Basic-Compiler on the market which can do both plattforms,Windows and Linux.But first there must be a (normaly) easy way to do these stuff under Windows.
If my Seriell-stuff works, we should put it into a library(and make the same
lib onto LINUX-version).
There are many libs for making games (DirectX etc..).I'm unluck about that, but that's life and fred do only the things
that most of the user's want.I'm also happy about the TCP-implementation,cause this a bit more of Pro-Stuff.Also the direct access over MemoryBanks(PEEK,POKE)
are great.Also you can put some real assembler-stuff into your code.
Fred is implementing some ODBC-Access into PureBAsic;great for Pro's

We will see what next happening.
Siggi
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Don.
I just tried the PB functionand r returns a success value!
I can't really test this, but is it doing what I think it is, I wonder?
Don
I just tried the PB function
Code: Select all
r = OpenFile (0,"COM1:")
I can't really test this, but is it doing what I think it is, I wonder?
Don
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Don.
OK, to answer my own question, it does look like this opens the COM port properly. I've checked that this API call produces the same result:
Here's an example of how I show some of my COM port settings:
If you want to change the port settings, use SetCommState_ instead.
Don
OK, to answer my own question, it does look like this opens the COM port properly. I've checked that this API call produces the same result:
Code: Select all
r = CreateFile_("COM1:",#GENERIC_READ | GENERIC_WRITE,0,#NULL,#OPEN_EXISTING,0,#NULL)
Code: Select all
devCB.DCB
r = OpenFile(0,"COM1:")
If r
If GetCommState_(r,devCB)
SomeResults.s = "Baud rate = "+Str(devCB\BaudRate)+Chr(10)
SomeResults = SomeResults + "Data Bits = "+Str(devCB\ByteSize)+Chr(10)
SomeResults = SomeResults + "Parity = "+Str(devCB\Parity)+" (0-4=no,odd,even,mark,space)"+Chr(10)
SomeResults = SomeResults + "Stop Bits = "+Str(devCB\StopBits)+" (0,1,2 = 1, 1.5, 2)"+Chr(10)
MessageRequester("COM Port Test",SomeResults,0)
Else
MessageRequester("COM Port Test","Couldn't get COM port settings",0)
EndIf
CloseFile(0)
Else
MessageRequester("COM Port Test","Couldn't open COM port",0)
EndIf
End
Don
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Rings.
Sorry but this code crashes while reading from Comport (ReadFile_)
can anyone help ?
Structure _EXCEPTION_POINTERS
*ExceptionRecord.EXCEPTION_RECORD;
*ContextRecord.CONTEXT;
EndStructure
Procedure _MyExceptionFilter(*lpEP._EXCEPTION_POINTERS)
Shared TerminateInProcess.b
If TerminateInProcess.b = #TRUE
Result.l = SetUnhandledExceptionFilter_(0)
; und forcieren einen normalen Absturz unter Betriebssystemkontrolle
RaiseException_ (0,0,0,0)
End
Else
TerminateInProcess.b = #TRUE
;Wir geben nun einen passenden Nachruf aus
Titel$ = ErrorTrapTitel.s +Chr(0)
CallDebugger
MessageRequester("COM Port Test","There is some trouble... PRG is shut down now!",0)
;RaiseException_ (0,0,0,0)
End
EndIf
EndProcedure
; Error Handler aktivieren
Result.l = SetUnhandledExceptionFilter_(@_MyExceptionFilter())
devCB.DCB
CtimeOut.COMMTIMEOUTS
EnableDebugger
InitGadget(100)
WindowID = OpenWindow(1, 1, 1, 300, 150, #PB_Window_SystemMenu, "COM-TEST")
If WindowID
CreateGadgetList(WindowID())
ButtonGadget(1, 1, 1, 80, 25, "COM OPEN")
ButtonGadget(2, 1, 30, 80, 25, "COM Read")
ButtonGadget(3, 1, 60, 80, 25, "COM Close")
Repeat
EventID.l = WaitWindowEvent()
; SetGadgetText(9, "x: "+Str(WindowMouseX())+", y: "+Str(WindowMouseY()))
; If EventID = #WM_KEYUP Or EventID = #WM_KEYDOWN
; SetGadgetText(9, "Key: "+Str(EventwParam()))
; EndIf
If EventID = #PB_EventGadget
Select EventGadgetID()
Case 1
Gosub ComOpen
Case 2
Gosub ComRead
MessageRequester("COM Port Test","Back in Buisness" ,0)
Case 3
Gosub ComClose
EndSelect
EndIf
Until EventID = #PB_EventCloseWindow
EndIf
Result.l = SetUnhandledExceptionFilter_(0)
End
ComRead:
Result = AllocateMemory(1, 1024, 0)
If Result
RetBytes=10
Dim bytes.b(255)
MyString.s=Space(256)
MessageRequester("COM Port Test","AllocMem at :"+Str(Result) ,0)
retval = ReadFile_(r, MyString, 255, RetBytes, 0)
;RetBytes=ReadData(UseMemory(1),1)
;retval = ReadFile_(r, MyString, 51, RetBytes, 0)
MessageRequester("COM Port Test",Str(retval) + " : "+Str(RetBytes),0)
MessageRequester("COM Port Test",Str(PeekB(UseMemory(1))) ,0)
FreeMemory(1)
EndIf
Return
ComClose:
fincom = CloseHandle_(r)
Return
ComOpen:
;r = OpenFile(0,"COM1:")
r = CreateFile_("COM1:", $C0000000, 0, 0, 3, 0, 0)
If r
;Setup Time Outs For com port
CtimeOut\ReadIntervalTimeout = 20
CtimeOut\ReadTotalTimeoutConstant = 1
CtimeOut\ReadTotalTimeoutMultiplier = 1
CtimeOut\WriteTotalTimeoutConstant = 10
; CtimeOut.WriteTotalTimeoutMultiplier = 1
retval = SetCommTimeouts_(r, CtimeOut)
If GetCommState_(r,devCB)
SomeResults.s = "Baud rate = "+Str(devCB\BaudRate)+Chr(10)
SomeResults = SomeResults + "Data Bits = "+Str(devCB\ByteSize)+Chr(10)
SomeResults = SomeResults + "Parity = "+Str(devCB\Parity)+" (0-4=no,odd,even,mark,space)"+Chr(10)
SomeResults = SomeResults + "Stop Bits = "+Str(devCB\StopBits)+" (0,1,2 = 1, 1.5, 2)"+Chr(10)
; MessageRequester("COM Port war",SomeResults,0)
;devCB\BaudRate=19200
;devCB\ByteSize=8
;devCB\Parity=0
;devCB\StopBits=1
Comsettings$ ="9600,n,8,1"
retval = BuildCommDCB_(Comsettings$, devCB)
;SomeResults.s = "Baud rate = "+Str(devCB\BaudRate)+Chr(10)
;SomeResults = SomeResults + "Data Bits = "+Str(devCB\ByteSize)+Chr(10)
;SomeResults = SomeResults + "Parity = "+Str(devCB\Parity)+" (0-4=no,odd,even,mark,space)"+Chr(10)
;SomeResults = SomeResults + "Stop Bits = "+Str(devCB\StopBits)+" (0,1,2 = 1, 1.5, 2)"+Chr(10)
;MessageRequester("COM Port Test",SomeResults,0)
retval = SetCommState_(r, devCB)
; MessageRequester("COM Port SetCommTest",Str(retval),0)
If GetCommState_(r,devCB)
SomeResults.s = "Baud rate = "+Str(devCB\BaudRate)+Chr(10)
SomeResults = SomeResults + "Data Bits = "+Str(devCB\ByteSize)+Chr(10)
SomeResults = SomeResults + "Parity = "+Str(devCB\Parity)+" (0-4=no,odd,even,mark,space)"+Chr(10)
SomeResults = SomeResults + "Stop Bits = "+Str(devCB\StopBits)+" (0,1,2 = 1, 1.5, 2)"+Chr(10)
MessageRequester("COM Port Ist",SomeResults,0)
EndIf
FlushFileBuffers_(r)
Return
Else
MessageRequester("COM Port Test","Couldn't get COM port settings",0)
EndIf
; CloseFile_(0)
Else
MessageRequester("COM Port Test","Couldn't open COM port",0)
EndIf
Return
; ExecutableFormat=Windows
; Executable=D:\Programme\PureBasic\ComTest.exe
Siggi
Sorry but this code crashes while reading from Comport (ReadFile_)
can anyone help ?
Structure _EXCEPTION_POINTERS
*ExceptionRecord.EXCEPTION_RECORD;
*ContextRecord.CONTEXT;
EndStructure
Procedure _MyExceptionFilter(*lpEP._EXCEPTION_POINTERS)
Shared TerminateInProcess.b
If TerminateInProcess.b = #TRUE
Result.l = SetUnhandledExceptionFilter_(0)
; und forcieren einen normalen Absturz unter Betriebssystemkontrolle
RaiseException_ (0,0,0,0)
End
Else
TerminateInProcess.b = #TRUE
;Wir geben nun einen passenden Nachruf aus
Titel$ = ErrorTrapTitel.s +Chr(0)
CallDebugger
MessageRequester("COM Port Test","There is some trouble... PRG is shut down now!",0)
;RaiseException_ (0,0,0,0)
End
EndIf
EndProcedure
; Error Handler aktivieren
Result.l = SetUnhandledExceptionFilter_(@_MyExceptionFilter())
devCB.DCB
CtimeOut.COMMTIMEOUTS
EnableDebugger
InitGadget(100)
WindowID = OpenWindow(1, 1, 1, 300, 150, #PB_Window_SystemMenu, "COM-TEST")
If WindowID
CreateGadgetList(WindowID())
ButtonGadget(1, 1, 1, 80, 25, "COM OPEN")
ButtonGadget(2, 1, 30, 80, 25, "COM Read")
ButtonGadget(3, 1, 60, 80, 25, "COM Close")
Repeat
EventID.l = WaitWindowEvent()
; SetGadgetText(9, "x: "+Str(WindowMouseX())+", y: "+Str(WindowMouseY()))
; If EventID = #WM_KEYUP Or EventID = #WM_KEYDOWN
; SetGadgetText(9, "Key: "+Str(EventwParam()))
; EndIf
If EventID = #PB_EventGadget
Select EventGadgetID()
Case 1
Gosub ComOpen
Case 2
Gosub ComRead
MessageRequester("COM Port Test","Back in Buisness" ,0)
Case 3
Gosub ComClose
EndSelect
EndIf
Until EventID = #PB_EventCloseWindow
EndIf
Result.l = SetUnhandledExceptionFilter_(0)
End
ComRead:
Result = AllocateMemory(1, 1024, 0)
If Result
RetBytes=10
Dim bytes.b(255)
MyString.s=Space(256)
MessageRequester("COM Port Test","AllocMem at :"+Str(Result) ,0)
retval = ReadFile_(r, MyString, 255, RetBytes, 0)
;RetBytes=ReadData(UseMemory(1),1)
;retval = ReadFile_(r, MyString, 51, RetBytes, 0)
MessageRequester("COM Port Test",Str(retval) + " : "+Str(RetBytes),0)
MessageRequester("COM Port Test",Str(PeekB(UseMemory(1))) ,0)
FreeMemory(1)
EndIf
Return
ComClose:
fincom = CloseHandle_(r)
Return
ComOpen:
;r = OpenFile(0,"COM1:")
r = CreateFile_("COM1:", $C0000000, 0, 0, 3, 0, 0)
If r
;Setup Time Outs For com port
CtimeOut\ReadIntervalTimeout = 20
CtimeOut\ReadTotalTimeoutConstant = 1
CtimeOut\ReadTotalTimeoutMultiplier = 1
CtimeOut\WriteTotalTimeoutConstant = 10
; CtimeOut.WriteTotalTimeoutMultiplier = 1
retval = SetCommTimeouts_(r, CtimeOut)
If GetCommState_(r,devCB)
SomeResults.s = "Baud rate = "+Str(devCB\BaudRate)+Chr(10)
SomeResults = SomeResults + "Data Bits = "+Str(devCB\ByteSize)+Chr(10)
SomeResults = SomeResults + "Parity = "+Str(devCB\Parity)+" (0-4=no,odd,even,mark,space)"+Chr(10)
SomeResults = SomeResults + "Stop Bits = "+Str(devCB\StopBits)+" (0,1,2 = 1, 1.5, 2)"+Chr(10)
; MessageRequester("COM Port war",SomeResults,0)
;devCB\BaudRate=19200
;devCB\ByteSize=8
;devCB\Parity=0
;devCB\StopBits=1
Comsettings$ ="9600,n,8,1"
retval = BuildCommDCB_(Comsettings$, devCB)
;SomeResults.s = "Baud rate = "+Str(devCB\BaudRate)+Chr(10)
;SomeResults = SomeResults + "Data Bits = "+Str(devCB\ByteSize)+Chr(10)
;SomeResults = SomeResults + "Parity = "+Str(devCB\Parity)+" (0-4=no,odd,even,mark,space)"+Chr(10)
;SomeResults = SomeResults + "Stop Bits = "+Str(devCB\StopBits)+" (0,1,2 = 1, 1.5, 2)"+Chr(10)
;MessageRequester("COM Port Test",SomeResults,0)
retval = SetCommState_(r, devCB)
; MessageRequester("COM Port SetCommTest",Str(retval),0)
If GetCommState_(r,devCB)
SomeResults.s = "Baud rate = "+Str(devCB\BaudRate)+Chr(10)
SomeResults = SomeResults + "Data Bits = "+Str(devCB\ByteSize)+Chr(10)
SomeResults = SomeResults + "Parity = "+Str(devCB\Parity)+" (0-4=no,odd,even,mark,space)"+Chr(10)
SomeResults = SomeResults + "Stop Bits = "+Str(devCB\StopBits)+" (0,1,2 = 1, 1.5, 2)"+Chr(10)
MessageRequester("COM Port Ist",SomeResults,0)
EndIf
FlushFileBuffers_(r)
Return
Else
MessageRequester("COM Port Test","Couldn't get COM port settings",0)
EndIf
; CloseFile_(0)
Else
MessageRequester("COM Port Test","Couldn't open COM port",0)
EndIf
Return
; ExecutableFormat=Windows
; Executable=D:\Programme\PureBasic\ComTest.exe
Siggi
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Don.
Hi Rings
One thing that doesn't look right in your call to ReadFile_ is the way you are using RetBytes as your 4th argument. As I understand it, ReadFile_ used this argument to return the number of bytes actually read. So you'd need:
... and so it also doesn't have any purpose setting RetBytes to 10 before this call, as you do in your code (but it will need to be declared of course).
Hope this might help.
Don
Hi Rings
One thing that doesn't look right in your call to ReadFile_ is the way you are using RetBytes as your 4th argument. As I understand it, ReadFile_ used this argument to return the number of bytes actually read. So you'd need:
Code: Select all
retval = ReadFile_(r, MyString, 255, @RetBytes, 0)
Hope this might help.
Don