zbasicandy
Joined: 26 Jan 2006
Posts: 193
|
|
Posted: 12 May 2006, 14:02 PM Post subject: The "7-11 One Stop" Communications Port Setup |
|
|
The "7-11 One Stop" Communications Port Setup
Somewhere on this Website there was talk of having 12 software UARTS. Having this many of serial ports could be extremely confusing if there are not provisions made to reduce the software complexity of setting up and communicationing to the serial ports.
If you look at the code (Far below) to setup ZBasic's multiple comports, you will see that for a "new user" or newbee it could be very problematic at best.
I suggest the following to make this setup as simple/transparent as possible without having a CIS degree in computer science.
InitCom contains the combination of "Comchannels(),DefineCom(),OpenSerialPort() in one command. <--- software impact unknown?
InitCom Port referencing are by Portx e.g. Port3(Com3),Port4(Com4) ... <---- global across all modules
'==============================================
==============================================
| Code: | InitCom(portx,rxPin,txPin,baud,flags,stopBits,portxinQueue, _
portxoutQueue) '* <--- Just one command in the main sub |
'==============================================
==============================================
'----------------------------------------------------------
'Type Subroutine or function ?
'portx ByVal Byte The serial channel to open. (Port3-6) <---- global constant(s) across all modules
'rxPin ByVal Byte The pin which will serve as the receive line.
'txPin ByVal Byte The pin which will serve as the transmit line.
'baud ByVal Long The desired baud rate.
'flags ByVal Byte Configuration flags.
'stopBits ByVal Byte The desired number of stop bits.
'portxinQueue ByRef array of Byte The queue for incoming characters. <--- global across all modules
'portxoutQueue ByRef array of Byte The queue for outgoing characters. <--- global across all modules
'-------------------------------------------------------------
* Notes: The serial hardware input/output pins, pullup/pulldowns, RS232 converters/inverters should be wired before serial port initilization.
* All queue referencing is as follows: Port3InQueue, Port3outQueue .... global across all modules
* InitCom allowed only once per user program?
* Other "support" subroutines needed along with InitCom() ---> ReOpenSerialPort(), ClearPortxInputBuffer() & ClearPortxOutputBuffer()?
| Code: | '-----------------------------------------------------
'Code needed to setup 4 comports with individual port
numbers and input/output queues.
'-----------------------------------------------------
'-------------------------------------------
'Set number of com channels and max baud rate
'-------------------------------------------
Call ComChannels(4, 9600)
'----------------------------------------------------
'Define "Super Comm Z" ZX-40 Commuications Serial I/O
'----------------------------------------------------
Call DefineCom(3, 39, 40, &H37) ' com 3 ,rec pin 39 tran pin 40 - 7,E,1
Call DefineCom(4, 37, 38, &H08) ' com 4 ,rec pin 37 tran pin 38 - 8,N,1
Call DefineCom(5, 17, 19, &H08) ' com 5 ,rec pin 17 tran pin 19 - 8,N,1
Call DefineCom(6, 16, 18, &H08) ' com 6 ,rec pin 16 tran pin 18 - 8,N,1
'------------------------------
'Software Serial ports "opened"
'------------------------------
Call OpenSerialPort(3,9600) '9600
Call OpenSerialPort(4,9600) '9600
Call OpenSerialPort(5,9600) '9600
Call OpenSerialPort(6,9600) '9600
'===============================================
===============================
'Constants
'[Public | Private] Const <name> As <type> = <value>
(default private)
'==========================================================
===============================
private const InBufSize as INTEGER = 50 ' 41-byte buffer.
private const OutBufSize as INTEGER = 50 ' 41-byte buffer.
private const minLogicalPort as Byte = 3
private const maxLogicalPort as Byte = 6
'-----------------------------
' Port Constants for all ports
'-----------------------------
' Do not modify!
Private Const Port3 as byte = 3 'keep private
Private Const Port4 as byte = 4 'keep private
Private Const Port5 as byte = 5 'keep private
Private Const Port6 as byte = 6 'keep private
'-----------------------------
'
'===============================================
'===============================================
'Variables
'{Public | Private | Dim} <name> As <type> (default private)
'===============================================
public InBuf(1 To InBufSize, minLogicalPort to maxLogicalPort) as BYTE
public OutBuf(1 To OutBufSize, minLogicalPort to maxLogicalPort) as BYTE
'===============================================
'===============================================
===============================
public Sub OpenSerialPort(ByVal port as Byte, ByVal
BaudRate as Long)
If ((port >= minLogicalPort) And (port <= maxLogicalPort)) Then
' Opens serial port n at the specified baud rate.
Call OpenQueue(CByteArray(Inbuf(1, port).DataAddress), InBufSize)
Call OpenQueue(CByteArray(Outbuf(1, port).DataAddress)OutBufSize)
Call OpenCom(port, BaudRate, CByteArray(Inbuf
(1,port).DataAddress), _
CByteArray(Outbuf(1, port).DataAddress))
End If
End Sub
'===============================================
|
|
|
victorf
Joined: 01 Jan 2006
Posts: 342
Location: Schenectady, New York
|
|
Posted: 13 May 2006, 10:37 AM Post subject: |
|
|
I havew done something similar to this. I am building an application that uses 3 comports. I have the luxury of needing to use only one port at a time. Therefore I use com3 for each, opening and closing them as needed. I have only one queue for transmit and one for receive. Here is the applicable code and I would appreciate comments:
| Code: |
'for the sense byte of the DefineCom call
Public Const NOINVERT as byte = &H08
Public Const INVERT as byte = &H88
'Define serial comm and queue sizes
Public Const QSIZE as Byte = 50 'com queue Size 41 + 9 bytes
Public Const BAUD12 as Long = 1200
Public Const BAUD24 as Long = 2400
Public Const BAUD96 as Long = 9600
'define queues
Public tq(1 to QSIZE) as Byte 'transmit queue
Public rq(1 to QSIZE) as Byte 'receive queue
Public Sub OpenCom3(ByVal device as Byte, _
ByVal rpin as Byte, _
ByVal tpin as Byte, _
ByVal sense as byte, _
ByVal baud as Long)
'this subroutine opens a comm connection as com3 based on the parameters. The routine first
'waits for any pending transmission to be complete the flushes the transmit queue. Then any
'data in the receive queue is cleared. The routine then reopens the com3 with the required
'parameters
Do While CBool(StatueCom(3) And &H04) 'wait til xmission complete
Loop
Call ClearCom(3, rq, tq) 'throw away all received data
Call CloseCom(3, rq, tq) 'close port
Call DefineCom(3, rpin, tpin, sense) 'define comport
CALL OpenCom(3, baud, rq, tq) 'open it
Call Delay(0.25) 'wait a sec
End Sub 'OpenCom3
|
I didn't include stop bit information as I did not need it.
I wish to acknowledge all the help from this forum in helping me formulate this concept and supplying snippets that I have used. Thank you
Vic |
|
victorf
Joined: 01 Jan 2006
Posts: 342
Location: Schenectady, New York
|
|
Posted: 13 May 2006, 13:47 PM Post subject: |
|
|
I uploaded the wrong snippet of code in my previous reply. Here is the current version:
| Code: |
'for the sense byte of the DefineCom call
Public Const NOINVERT as byte = &H08
Public Const INVERT as byte = &H88
'Define serial comm and queue sizes
Public Const QSIZE as Byte = 50 'com queue Size 41 + 9 bytes
Public Const BAUD12 as Long = 1200
Public Const BAUD24 as Long = 2400
Public Const BAUD96 as Long = 9600
'define queues
Public tq(1 to QSIZE) as Byte 'transmit queue
Public rq(1 to QSIZE) as Byte 'receive queue
Public Sub OpenCom3(ByVal rpin as Byte, _
ByVal tpin as Byte, _
ByVal sense as byte, _
ByVal baud as Long)
'this subroutine opens a comm connection as com3 based on the parameters. The routine first
'waits for any pending transmission to be complete the flushes the transmit queue. Then any
'data in the receive queue is cleared. The routine then reopens the com3 with the required
'parameters
Do While CBool(StatusCom(3) And &H04) 'wait til xmission complete
Loop
Call ClearQueue(rq) 'throw away all received data
Call CloseCom(3, rq, tq) 'close port
Call DefineCom(3, rpin, tpin, sense) 'define comport
CALL OpenCom(3, baud, rq, tq) 'open it
Call Delay(0.25) 'wait a sec
End Sub 'OpenCom3
'===============================================================================
|
|
|