Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
Using COM3

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
victorf



Joined: 01 Jan 2006
Posts: 342
Location: Schenectady, New York

Posted: 14 January 2006, 21:51 PM    Post subject: Using COM3 Reply with quote

As a first time user of ZX-24 I am feeling my way along with help from this forum. I need to implement COM3.

I have initialized COM3 as follows:


Code:

Public const RXD as byte = 7     'pin for com3 RXD
Public const TXD as byte = 6     'pin for com3 TXD
Public const qsize as byte = 50  'com3 queue Size
Dim txq3(1 to qsize) as Byte
Dim rxq3(1 to qsize) as Byte


'open Com3 for I/O
Call OpenQueue(rxq3, SizeOf(rxq3))
Call OpenQueue(txq3, SizeOf(txq3))
Call DefineCom(3, RXD, TXD, &H08)
Call OpenCom(3, 9600, rxq3, txq3)


Does this send 1 byte on COM3 TXD line? This byte requests a return string on the RXD line of COM3

Code:
 
Call PutQueue(txq3, mybyte, 1)


The string returned can be 10 to 40 bytes Will this do the trick?

Code:
   
Call GetQueue(rxq3, mystring, GetQueueCount(rxq3))

I need to insure that I get data for mystring.

Am I doing this right or is there a better strategy? OR am I all wet Confused

Any enlightenment will be appreciated.

Vic
Back to top
DH*
Guest





Posted: 15 January 2006, 0:57 AM    Post subject: Reply with quote

First, each queue has 9 bytes of overhead so I always size the queues to the longest message + 9.

Second, your receive approach might get you in trouble. If nothing comes in. GetQueue will hang forever.

It's best to use Status Queue to see if there is data in the queue before calling GetQueue.

If you know in advance how many bytes will be in a message you can use the version of GetQueue that has a timeout.

When I don't know how many bytes to expect I use this approach...

Code:
    If StatusQueue(iq3) Then
      timeout = Timer + 0.05   'adjust as needed for length of messages
      i = 1
      Do While StatusQueue(iq3)
        'can reset timeout here
        Call GetQueue(iq3, data(i), 1)
        If Timer > timeout Then
          Exit Do
        End If
        'can test here for EOL or len of message
        i = i + 1
     Loop
    End If
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 15 January 2006, 5:43 AM    Post subject: Reply with quote

Code:
Call GetQueue(rxq3, mystring, GetQueueCount(rxq3))


This won't do what you might be expecting. This call will retrieve bytes from the queue and place them in the space allocated to the string variable. Unfortunately, the first few bytes of the string variable's storage space contains the control information for the string so this call will overwrite the control information rather than populate the string as you probably wish it did.

The issue is that GetQueue() doesn't know, and can't know, that the variable that you're passing by reference happens to be a string variable. If you want to populate a string using data from a queue you'll have to fetch the data one byte at a time and append each to the string.

If you need a specific example of code to do this let me know.
Back to top
victorf



Joined: 01 Jan 2006
Posts: 342
Location: Schenectady, New York

Posted: 15 January 2006, 13:09 PM    Post subject: Reply with quote

Don and dhouston,

Thank you both for setting me straight. Using COM3 is a bit more complicated than I expected.

Don; I would like to see code to populate my string. I need to retrieve string data from a PIC sensor board I developed. the string is essentially:
Quote:

$,wwXww,ppXpp,wwYww,ppYpp,hhhhh,@


A comma delimited string of data from two sensors. This string is currently 33 bytes in length. The '$' and '@' are just delimiters that help me parse the string. I think I understand how to populate the string. I could have an array of bytes and just have the data from the sensor board populate that array and then copy the data out of the array to the string. However, I would like to see your code.Exclamation

Any enlightenment will be appreciated.

Vic
Back to top
victorf



Joined: 01 Jan 2006
Posts: 342
Location: Schenectady, New York

Posted: 15 January 2006, 14:41 PM    Post subject: Reply with quote

First, let me comment on my hardware setup. I have a controller
board with the ZX-24 and some minor switches, LEDs and the
like along with a MAX232 "attached' to the pins I want to use for
COM3. I have a 4 port multiplexer board that accepts the RS232 data
from the controller board. This multiplexer can switch the RS232
RXD and TXD from the controller to one of 4 I/O ports under software
control of its two address line. On one port connects to a PIC driven
sensor board that sends data when requested by the controller. Another
port is attached to a LCD display and a third port connects to a
system printer. The fourth port is currently undedicated. The sensor
baord and the LCD talk at 9600b but the printer requires 2400b

I wish to write two general purpose routines to read and write to COM3
in somewhat the same manner that is done with COM1 with console.read(ln) and console.write(ln). Is it possible to write such general purpose routines?

dhouston's code snippet might be modifiable to work on the read side. If I
understand the code correctly, there is some uncertainty about receiving
all the data as it is time dependent. If a character takes a bit more time
than anticipated and the loop breakout is taken then what happens?
Perhaps the timing can be handled by the caller supplying the necessary
time incrementor. Does reading from the queue clear the queue or do I have to do that myself? Writing to COM3 does not appear to be too difficult.

Any enlightenment will be appreciated.


Vic
Back to top
DH*
Guest





Posted: 15 January 2006, 16:07 PM    Post subject: Reply with quote

If you always have @ on the end of an input you can modify my snippet in this manner...

Code:
    If StatusQueue(iq3) Then
      timeout = Timer + 0.05   'adjust as needed for length of messages
      i = 1
      Do While StatusQueue(iq3)
        Call GetQueue(iq3, data(i), 1)
        If Timer > timeout Then
          Exit Do
        End If
        If data(i) = 64 Then   '@
          Exit Do
        End If 
        i = i + 1
      Loop
    End If
    If data(i) = 64 Then
      'use BlockMove as needed to move data() to strings
      'using StrAddress(str) for the destination address
    Else
      'timed out
    End If

Data remains in the receive queue until you Call GetQueue to empty it or until it's overwritten by new data.

Use the timeout as a failsafe in case there is no @.

You could also use the version of GetQueue that has a timeout built-in. I think it's a bit more clear the way I've coded it. Also, the built-in timeout will apply to each byte which isn't what we really want here.

Also, you could populate the string as each byte is read from the queue using...
Code:
str = str & Chr(data(i))

If there are long gaps in the data, you might pop out of the Do loop prematurely. You could solve this by using...
Code:
Do While data(i) <> 64

and eliminate the If/Then test for @ like...
Code:
    If StatusQueue(iq3) Then
      timeout = Timer + 0.05   'adjust as needed for length of messages
      i = 0
      Do
        i = i + 1
        Call GetQueue(iq3, data(i), 1)
        If Timer > timeout Then
          Exit Do
        End If
      Loop Until data(i) = 64
    End If
    If data(i) = 64 Then
      'use BlockMove as needed to move data() to strings
      'using StrAddress(str) for the destination address
    Else
      'timed out
    End If
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 15 January 2006, 17:54 PM    Post subject: Reply with quote

Code:
  'use BlockMove as needed to move data() to strings
  'using StrAddress(str) for the destination address


It is simpler to use
MakeString to populate a string from an array of bytes.

Note, particularly, that if allocated strings are being used (the default in ZBasic native mode) StrAddress() will return zero if the string is empty. Using MakeString() obviates the need to fuss with such details.
Back to top
DH*
Guest





Posted: 15 January 2006, 18:11 PM    Post subject: Reply with quote

Coming from BasicX, I hadn't noticed MakeString.

How do you use it if you want to copy from a byte array to multiple strings? I think that's what Vic wants to do.
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 16 January 2006, 3:21 AM    Post subject: Reply with quote

Quote:
How do you use [MakeString()] if you want to copy from a byte array to multiple strings?


Lets say that you have a byte array named sensorData that you have confirmed contains a string in the form:

Code:
$,wwXww,ppXpp,wwYww,ppYpp,hhhhh,@


Assume that you want to create 5 strings from the five comma separated parts of this array. You can accomplish that using this code:

Code:
s1 = MakeString(sensorData.DataAddress(3), 5)
s2 = MakeString(sensorData.DataAddress(9), 5)
s3 = MakeString(sensorData.DataAddress(15), 5)
s4 = MakeString(sensorData.DataAddress(21), 5)
s5 = MakeString(sensorData.DataAddress(27), 5)


There are alternate ways to specify the address from which to begin copying characters to the constructed string.

Code:
s1 = MakeString(sensorData.DataAddress(3), 5)
s1 = MakeString(sensorData.DataAddress + 2, 5)
s1 = MakeString(MemAddress(sensorData) + 2, 5)


These are all equivalent. Which you use depends on personal preferences.
Back to top
spamiam



Joined: 13 Nov 2005
Posts: 664

Posted: 16 January 2006, 13:32 PM    Post subject: Reply with quote

If you want to see some serial port handling (serial LCD in particular) take a quick look at the 2 API variants I have posted in the FILES section. These can use different COM ports, but functionally the ports are all the same to the ZX user aside from the opening of the ports.

I have found the MakeString function to be quite powerful, but when pulling data off a serial queue, the character-by-character method works just fine.

-Tony
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group