November 28, 2020, 01:24:43 PM

Author Topic: [bb] COM-Port II by Vertex [ 1+ years ago ]  (Read 698 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] COM-Port II by Vertex [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : COM-Port II
Author : Vertex
Posted : 1+ years ago

Description : File% = OpenCommPort%(0-255) ; -1 = Error
Succes% = CloseCommPort%(File%)
Size% = WriteComm%(File%, Bank%, Size%)
Size% = ReadComm%(File%, Bank%, Size%)
Succes% = SetComm%(File%, "Settings")
Succes% = SetCommTimeouts%(File%, ReadMillisecs%, WriteMillisecs%)

SetComm: <a href="http://msdn.microsoft.com/library/en-us/devio/base/buildcommdcb.asp?frame=true" target="_blank">http://msdn.microsoft.com/library/en-us/devio/base/buildcommdcb.asp?frame=true[/url]
For example "baud=9600 parity=N data=8 stop=1":
Sets baudrate to 9600 bits per second, no paritybit, 8 databits and 1 stopbit.

Example:
Code: [Select]
File% = OpenCommPort(1)
SetComm(File%, "baud=9600 parity=N data=8 stop=1")
SetCommTimeouts(File%, 500, 500)

Repeat
Message$ = Input("Message: ")
If Message$ = "end" Then
Exit
ElseIf Message$ = "cls" Then
Cls
Locate 0, 0
Message$ = ""
EndIf

SendMessage(File%, Message$+Chr$(13)+Chr$(10))

Message$ = RecvMessage(File%)
If Message$ <> "" Then Print Message$
Forever

CloseCommPort(File%)
End

Function SendMessage%(File%, Command$)
Local Buffer%, Offset%, Succes%

Buffer% = CreateBank(Len(Command$))
For Offset% = 0 To Len(Command$)-1
PokeByte Buffer%, Offset%, Asc(Mid$(Command$, Offset%+1, 1))
Next

Success% = WriteComm(File%, Buffer%, Len(COmmand$))
FreeBank Buffer%

Return Success%
End Function

Function RecvMessage$(File%)
Local Buffer%, Offset%, Count%, Message$

Buffer% = CreateBank(1024)
Count% = ReadComm(File%, Buffer%, 1024)
If Count% > 0 Then
For Offset% = 0 To Count-1
Message$ = Message$+Chr$(PeekByte(Buffer%, Offset%))
Next
Return Message$
Else
FreeBank Buffer%
Return ""
EndIf
End Function


Just put your old modem on port 1, write "AT" (AT = Attention) press Return. Now you can communicate with your modem. For example "ATDT1234567" (D = Dail, T = Tone) or Hangup with ATH0.

You just need the following comport.decls :
<div class="quote"> .lib "kernel32.dll"
apiCreateFile%(FileName$, DesiredAccess%, ShareMode%, pSecurrityAttribute%, CreationDistribution%, FlagsAndAttributes%, TemplateFile%) : "CreateFileA"
apiCloseHandle%(Object%) : "CloseHandle"

apiWriteFile%(File%, pBuffer*, NumberOfBytesToWrite%, pNumberOfBytesWritten*, pOverlapped%) : "WriteFile"
apiReadFile%(File%, pBuffer*, NumberOfBytesToRead%, pNumberOfBytesRead*, pOverlapped%) : "ReadFile"
apiGetCommState%(File%, pDCB*) : "GetCommState"
apiSetCommState%(File%, pDCB*) : "SetCommState"
apiBuildCommDCB%(Def$, pDCB*) : "BuildCommDCBA"
apiGetCommTimeouts%(File%, pCommTimeouts*) : "GetCommTimeouts"
apiSetCommTimeouts%(File%, pCommTimeouts*) : "SetCommTimeouts"
</div>

cu olli [/i]

Code :
Code: BlitzBasic
  1. Const GENERIC_READ          = $80000000
  2. Const GENERIC_WRITE         = $40000000
  3. Const OPEN_EXISTING         = 3
  4. Const FILE_ATTRIBUTE_NORMAL = $80
  5. Const INVALID_HANDLE_VALUE  = -1
  6.  
  7. Function OpenCommPort%(Port%)
  8.         Local File%
  9.  
  10.         If (Port < 0) Or (Port > 255) Then Return -1
  11.        
  12.         ; Open CommPort
  13.         File% = apiCreateFile("COM"+Port%, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
  14.         If File% = INVALID_HANDLE_VALUE Then
  15.                 Return -1
  16.         Else
  17.                 Return File%
  18.         EndIf
  19. End Function
  20.  
  21. Function CloseCommPort%(File%)
  22.         Return apiCloseHandle(File%)
  23. End Function
  24.  
  25. Function WriteComm%(File%, Buffer%, Size%)
  26.         Local Count%, Count2%
  27.  
  28.         If Size% > BankSize(Buffer%) Then Return 0
  29.        
  30.         Count% = CreateBank(4)
  31.        
  32.         If apiWriteFile(File%, Buffer%, Size%, Count%, 0) = 0 Then
  33.                 FreeBank Count%
  34.                 Return 0
  35.         Else
  36.                 Count2% = PeekInt(Count%, 0)
  37.                 FreeBank Count%
  38.                 Return Count2%
  39.         EndIf
  40. End Function
  41.  
  42. Function ReadComm%(File%, Buffer%, Size%)
  43.         Local Count%, Count2%
  44.        
  45.         If Size% > BankSize(Buffer%) Then Return 0
  46.        
  47.         Count% = CreateBank(4)
  48.        
  49.         If apiReadFile(File%, Buffer%, Size%, Count%, 0) = 0 Then
  50.                 FreeBank Count%
  51.                 Return 0
  52.         Else
  53.                 Count2% = PeekInt(Count%, 0)
  54.                 FreeBank Count%
  55.                 Return Count2%
  56.         EndIf
  57. End Function
  58.  
  59. Function SetComm%(File%, Settings$)
  60.         Local DCB%
  61.    
  62.         DCB% = CreateBank(28)
  63.    
  64.         ; Get States
  65.         If apiGetCommState(File%, DCB) = 0 Then
  66.                 FreeBank DCB%
  67.                 Return False
  68.         ElseIf PeekInt(DCB%, 0) <> 28
  69.                 FreeBank DCB%
  70.                 Return False
  71.         EndIf
  72.        
  73.         ; Build DCB
  74.         If apiBuildCommDCB(Settings$, DCB%) = 0 Then
  75.                 FreeBank DCB%
  76.                 Return False
  77.         EndIf
  78.    
  79.         ; Set States
  80.         If apiSetCommState(File%, DCB%) = 0 Then
  81.                 FreeBank DCB%
  82.                 Return False
  83.         Else
  84.                 FreeBank DCB%
  85.                 Return True
  86.         EndIf
  87. End Function
  88.  
  89. Function SetCommTimeouts%(File%, ReadTime%, WriteTime%)
  90.         Local TimeOuts%
  91.        
  92.         ; Get timeouts
  93.         Timeouts% = CreateBank(40)
  94.         If apiGetCommTimeouts(File%, Timeouts%) = 0 Then
  95.                 FreeBank Timeouts
  96.                 Return False
  97.         EndIf
  98.        
  99.         PokeInt Timeouts%,  8, ReadTime%  ; ReadTotalTimeoutConstant
  100.         PokeInt Timeouts%, 16, WriteTime% ; WriteTotalTimeoutConstant
  101.        
  102.         ; Set Timeouts
  103.         If apiSetCommTimeouts(File%, Timeouts%) = 0 Then
  104.                 FreeBank Timeouts
  105.                 Return False
  106.         Else
  107.                 FreeBank Timeouts
  108.                 Return True
  109.         EndIf
  110. End Function


Comments :


Nigel Brown(Posted 1+ years ago)

 Have done a MAX module of this if anyone interested? Havenot tested it yet seems to be ok.


Neochrome(Posted 1+ years ago)

 hey this works!!!!!even works with home brew RS232 buffers!


Lane(Posted 1+ years ago)

 Thanks this seems to work, after adding the API stuff to my Kernal32.declsome of the declarations already existed with api_ as show belowYours : apiSetCommState%(File%, pDCB*) : "SetCommState"Mine: api_SetCommState% (hCommDev%, lpDCB*) : "SetCommState"


Mr. Bean(Posted 1+ years ago)

 I was searching for a Terminal, to test my At89S8252-processor, found a lot, but nothing good. Then I saw this, compiled and everything works now.Thanks


schilcote(Posted 1+ years ago)

 Exactly how do we use it? (I'm not great with advanced concepts like memory banks and stuff)


Litobyte(Posted 1+ years ago)

 Hey Vertex, thanks for sharing.I tried and it wooooorks!


Danny(Posted 1+ years ago)

 @Nigel Brown, yes I'm very much interested in a BMax version. Do you still have it?Cheers,Danny


markcw(Posted 1+ years ago)

 Nigel Brown's com module is <a href="../Community/posts8195-2.html?topic=60067#857456" target="_blank">here[/url].Also see Brucey's <a href="../Community/postsde74-3.html?topic=75614" target="_blank">wxCTB[/url].


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal