jorsam wrote:Which command is used to send and receive serial communication using this open port?
The serial channels in ZBasic are all buffered; the transmit and receive queues that you specify when a channel is opened are used for the transmit and receive buffers. Consequently, you don't send and receive characters directly to and from the channel's rx and tx pins. Rather, you put characters to be transmitted into the channel's transmit queue and you read the received characters from the channel's receive queue. Although this design may seem complicated, it has the significant advantage that data transmission and reception can be done in the background.
jorsam wrote:Example: how to code a simple program to send and ASCII character to txPin and how to read a string from the rxPin?
Here is a simple example program showing how to set up a serial channel and then send a character and a string of characters to a serial channel's output queue.
Code: Select all
Const txPin as Byte = 5
Const rxPin as Byte = 6
Const CR as Byte = &H0d
Const LF as Byte = &H0a
Dim iq(1 to 25) as Byte
Dim oq(1 to 40) as Byte
Sub Main()
' initialize the queues
Call OpenQueue(iq)
Call OpenQueue(oq)
' define the characteristics of the serial channel
Call DefineCom(3, rxPin, txPin, &H08)
' open the serial channel
Call OpenCom(3, 9600, iq, oq)
' send a carriage return and line feed as individual bytes
Call PutQueueByte(oq, CR)
Call PutQueueByte(oq, LF)
' send a string with a CRLF
Call PutQueueStr(oq, "Hello, world!" & Chr(CR) & Chr(LF))
End Sub
Reading data received via the serial channel is slightly more complicated because you don't know when the data you want is going to start arriving and when it will have been completely received. Whether you're wanting to read single characters or sequences of characters from the receive queue, the
StatusQueue() and
GetQueueCount() functions will be useful to determine when enough data has arrived. The difference between these functions is that
StatusQueue() will return True if the queue contains one or more characters. On the other hand,
GetQueueCount() returns the number of characters available in the queue. The example below is the Main() subroutine from above with the addition of a loop that echos the received characters to the debug output.
Code: Select all
Sub Main()
' initialize the queues
Call OpenQueue(iq)
Call OpenQueue(oq)
' define the characteristics of the serial channel
Call DefineCom(3, rxPin, txPin, &H08)
' open the serial channel
Call OpenCom(3, 9600, iq, oq)
' send a carriage return and line feed as individual bytes
Call PutQueueByte(oq, CR)
Call PutQueueByte(oq, LF)
' send a string with a CRLF
Call PutQueueStr(oq, "Hello, world!" & Chr(CR) & Chr(LF))
' echo all received characters to the debug console
Do
If (StatusQueue(iq)) Then
Dim c as Byte
Call GetQueue(iq, c, 1)
If (c = CR) Then
Debug.Print
ElseIf (c <> LF) Then
Debug.Print Chr(c);
End If
End If
Loop
End Sub
Here is an alternate implementation of the echo loop using
GetQueueStr().
Code: Select all
' echo all received characters to the debug console
Do
If (StatusQueue(iq)) Then
Dim str as String
Dim strLen as Integer, idx as Integer
str = GetQueueStr(iq)
strLen = Len(str)
For idx = 1 to strLen
Dim c as Byte
c = Asc(str, idx)
If (c = CR) Then
Debug.Print
ElseIf (c <> LF) Then
Debug.Print Chr(c);
End If
Next idx
End If
Loop
One of the challenges related to reading from a queue is you can't tell ahead of time whether or not it contains a specific character or sequence of characters (e.g. a CR/LF). If your application requires that you wait for receipt of such a termination character, it may be best to read characters from the queue into an array or string until you get the termination character(s).