PutQueueStr Question

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
iamajoat
Posts: 4
Joined: 25 February 2008, 11:31 AM
Location: Louisville, KY

PutQueueStr Question

Post by iamajoat »

I've checked the docs & can't find an answer to this.

When using PutQueueStr, if the string contains Chr(13) (Carriage Return) will that automagically terminate the output, even though there are more characters in the string? My testing indicates that this is true, but I might be missing something.

If so, what method or work around is there? The application I am working on requires me to be able to include Carriage Return, Linefeed, and Null in the strings (it's a long story).

Thanks.

Mike
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: PutQueueStr Question

Post by dkinzer »

iamajoat wrote:When using PutQueueStr, if the string contains Chr(13) (Carriage Return) will that automagically terminate the output, even though there are more characters in the string?
No. The entire string is sent to the queue irrespective of its content.
iamajoat wrote:My testing indicates that this is true.
I have never observed such behavior nor has it been reported here. Can you produce a simple test case that demonstrates the problem you have observed?

The test case below demonstrates that CR, LF and null are treated like any other character.

Code: Select all

Sub Main()
  Dim q(1 to 40) as Byte
  Dim s as String

  Call OpenQueue(q, SizeOf(q))
  Debug.Print "Before, the queue has "; GetQueueCount(q); " bytes."
  s = "abc" & Chr(&H0d) & Chr(&H0a) & Chr(0) & "def"
  Call foo(q, s)
  Debug.Print "After, the queue has "; GetQueueCount(q); " bytes."
  Debug.Print "The string has "; Len(s); " bytes."
End Sub

Private Sub foo(ByRef q() as Byte, ByVal s as String)
  Call PutQueueStr(q, s)
End Sub
- Don Kinzer
iamajoat
Posts: 4
Joined: 25 February 2008, 11:31 AM
Location: Louisville, KY

Re: PutQueueStr Question

Post by iamajoat »

Don,
Thanks for the reply.
I ran the code you provided (with a few mods)

Code: Select all

Sub Main()
  Dim q(1 to 40) as Byte
  Dim s as String

  Call OpenQueue(q, SizeOf(q))
  Debug.Print "Before, the queue has "; CStr(GetQueueCount(q)); " bytes."
  s = "abc" & Chr(&H0d) & Chr(&H0a) & Chr(0) & "def"
  Call foo(q, s)
  Debug.Print "After, the queue has "; CStr(GetQueueCount(q)); " bytes."
  Debug.Print "The string has "; CStr(Len(s)); " bytes."
End Sub

Private Sub foo(ByRef q() as Byte, ByVal s as String)
	Call OpenCom(1,19200,0,q)
	Call PutQueueStr(q, s & Chr(13))
End Sub
and got the following results:
Before, the queue has 0 bytes.
abc
[NUL]def
After, the queue has 0 bytes.
The string has 9 bytes.

So it is working in your example. Here's a piece of what I'm working on:

Code: Select all

Public Const velSuffix as String = Chr(13) & Chr(10) & Chr(0) & Chr(0) & Chr(62)
Public Const EndVelHdr as String = Chr(13) & Chr(10) & Chr(0) & Chr(0)
Public Const strMT101 as String = "UT" & EndVelHdr & "MEASURE UNIT  FEET/SEC  " & velSuffix

Sub Main()
	Dim strGMCP as String 'holder for messages from/to GCS OP
	Dim queueCount as Integer 'counter to check for input
	Dim n as Integer 'counter to get out of the no comms loop
	Dim inQueue(1 to 16) as Byte 'queue for input, max of 7 plus 9 for overhead
	Dim outQueue(1 to 44)as Byte 'queue for output, max of 35 plus 9 for overhead
'	make sure everything is empty/zeroed out
	strGMCP = ""
	queueCount = 0
	n = 0
	Call OpenQueue(inQueue, SizeOf(inQueue))
	Call OpenQueue(outQueue, SizeOf(outQueue))
	Call ClearQueue(inQueue)
	Call ClearQueue(outQueue)
	Call OpenCom(1,19200,inQueue,outQueue) 'change to 9600 later
	Do While queueCount = 0
'if we are here, we are standalone mode
		queueCount = GetQueueCount(inQueue)
		If queueCount > 0 Then
			Exit Do
		End If
		Delay(1.0)'change to ten seconds later
		strGMCP = strMT101
		Call PutQueueStr(outQueue,strGMCP)
		Call ClearQueue(outQueue)
		strGMCP = ""
		n = n + 1
		if n = 10 Then 'change to higher value later
			Exit Do
		End If
	Loop
'		If  queueCount > 0 Then
'if we are here the GCS OP sent us something
'			strGMCP = GetQueueStr(inQueue,queueCount)
'			Call PutQueueStr(outQueue,strGMCP)
'			strGMCP = ""
'			queueCount = 0
'			Call ClearQueue(inQueue)
'		Else
'			strGMCP = "No Input" & Chr(13)
'			Call PutQueueStr(outQueue,strGMCP)
'			Call ClearQueue(outQueue)
'		End If

	Call CloseCom(1,inQueue, outQueue)
	Debug.Print Chr(13) & "Velocimeter Simulator Complete"
End Sub
and the output I get:
ZBasic v2.3
UTUTUTUTUTUTUTUTUTUT
Velocimeter Simulator Complete

It appears that my code should be working, but I can't figure out why it isn't.

Mike
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Re: PutQueueStr Question

Post by mikep »

iamajoat wrote:

Code: Select all

	Call OpenQueue(inQueue, SizeOf(inQueue))
	Call OpenQueue(outQueue, SizeOf(outQueue))
	Call ClearQueue(inQueue)
	Call ClearQueue(outQueue)
	Call OpenCom(1,19200,inQueue,outQueue) 'change to 9600 later
COM1 is opened by default before a program starts to run. If you want to change the baudrate or attach your own queues as you have done, then you need to first close the port using CloseCom. See this thread for an example.
Mike Perks
iamajoat
Posts: 4
Joined: 25 February 2008, 11:31 AM
Location: Louisville, KY

Re: PutQueueStr Question

Post by iamajoat »

mikep wrote: COM1 is opened by default before a program starts to run. If you want to change the baudrate or attach your own queues as you have done, then you need to first close the port using CloseCom. See this thread for an example.
Mike,
Thanks for the reply. I tried doing that, but it makes no difference in the output of my code.

I still can't see why Don's example works and mine doesn't.

Mike
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Re: PutQueueStr Question

Post by spamiam »

iamajoat wrote:Mike,
Thanks for the reply. I tried doing that, but it makes no difference in the output of my code.

I still can't see why Don's example works and mine doesn't.

Mike
One suggestion I have is sort of painful. It is what I do when I can't find the source of an error. Start with the functioning code, and add your own code to it until either you get it fully functioning or until you make it fail. If you get it to do everything, then the prtoblem is solved (but not necessarily answered), and if it fails, then the last piece of added code must the be culprit.
-Tony
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: PutQueueStr Question

Post by dkinzer »

iamajoat wrote:It appears that my code should be working, but I can't figure out why it isn't.
Your code is executing correctly - it's probably not what you intended, though. Perhaps you can explain your intent for the sequence below:

Code: Select all

  Call PutQueueStr(outQueue,strGMCP) 
  Call ClearQueue(outQueue)
The ClearQueue() call deletes whatever is in the queue, including what you just added (except for the characters already transmitted).

By the way, you don't need the ClearQueue() calls after the OpenQueue() calls (early in the program). After OpenQueue(), a queue is guaranteed to be empty.
- Don Kinzer
iamajoat
Posts: 4
Joined: 25 February 2008, 11:31 AM
Location: Louisville, KY

Re: PutQueueStr Question

Post by iamajoat »

dkinzer wrote:Your code is executing correctly - it's probably not what you intended, though. Perhaps you can explain your intent for the sequence below:

Code: Select all

  Call PutQueueStr(outQueue,strGMCP) 
  Call ClearQueue(outQueue)
The ClearQueue() call deletes whatever is in the queue, including what you just added (except for the characters already transmitted).

By the way, you don't need the ClearQueue() calls after the OpenQueue() calls (early in the program). After OpenQueue(), a queue is guaranteed to be empty.
Don,
I was being careful. I assumed (there I go again) that the clear function wouldn't run until the put was done. Now thinking about it, I realize that I didn't take into account the delay in serial transmission.

Thanks for the responses and clarifications.

Mike
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: PutQueueStr Question

Post by dkinzer »

iamajoat wrote:I didn't take into account the delay in serial tansmission.
If you want to wait until all of the characters have been removed from the queue (for transmission) you can use this:

Code: Select all

Do While StatusQueue()
Loop
Note that this does not wait for the last character to be completely transmitted. If that is your intention you can use this:

Code: Select all

Do While CBool(StatusCom(1) And &H04)
Loop
If you use either of these, the ClearQueue() call will still be superfluous since the queue is already empty after the loop completes.
- Don Kinzer
Post Reply