PDA

View Full Version : Comm Module Comm_Recv



GKeely
12-03-2008, 12:12
Hi
Just started using thinbasic to communicate with an oled display connected through a serial port.When a command is sent to the display, if everything is ok, it carries out the command and responds with an ACK(&h06).I have tried to use the COMM_RECV to catch this but without success.It seems as if COMM_RECV does not wait for the character to be received.The problem is that the next command is sent and the display hangs up. I've worked around this for now by including a delay but was just wondering if I am missing something ?

Basic code is
Comm_Send(hComm, "command" & chr$(val1) & chr$(val2) & etc)
NBytes = COMM_Get(hComm, %COMM_RXQUE)
COMM_Recv(hComm, NBytes, VariableBuffer)

Regards

Gerry

Michael Clease
12-03-2008, 14:01
Welcome to thinbasic Gerry.

If you look at the example in the samplescript directory it uses a delay.

good luck

GKeely
12-03-2008, 14:28
Hi Abraxas

Thank you for your reply.The reason I asked the question is that the help section for COMM-RECV states "Program execution will halt until NBytes bytes are available" so this suggests there should be no need for a time delay ?

Regards

Gerry

Petr Schreiber
12-03-2008, 15:11
Hi GKeely,

maybe there is a typo in docs, I am sure Eros will correct it.

To wait until right number of bytes arrive you could maybe use something like:


' -- Wait till we get number of bytes we want

while COMM_Get(hComm, %comm_RXQUE) <> NumberOfBytesWeWant
Sleep 0 ' -- To not hog CPU
wend

' -- Now we got the bytes we need so we can process them




Bye,
Petr

Petr Schreiber
12-03-2008, 15:21
Or maybe use following function:


function WaitForNBytesWithTimeout( cHandle as long, wantedBytes as long, timeOutInMS as long ) as long
local timeStart as long = GetTickCount
Local ReturnValue as long = 1

while GetTickCount-timeStart < timeOutInMS
if COMM_Get(cHandle, %comm_RXQUE) = wantedBytes then
ReturnValue = 0
exit while
end if
wend

function = ReturnValue

end function


Returns 1 if timeouted, 0 if number of requested bytes is ready
Or something like that, did not had opportunity to test the code in real battle :)

Bye,
Petr

ErosOlmi
12-03-2008, 15:54
Ciao Gerry and welcome here.

COMM RECV should halt execution until requested number of bytes are present in the queue.
As Petr suggested, use COMM_Get(hComm, %comm_RXQUE) to check the size of the queue before using COMM RECV, than get the bytes and check if return buffer is what you expect.

I've checked thinBasic_COMM.dll source code module and it should work as expected.

Function WaitForNBytesWithTimeout suggested by Petr should do the job. Maybe passing one more param (BYREF sBuff AS STRING) can be used to return the queue buffer once the size is what expected. Maybe I can add this function as native COMM module function.

Let me know. If you still have problems I will check deeply in source code.
Ciao
Eros

GKeely
12-03-2008, 16:31
Petr & Eros

Thank you ;D both for your replies, Petr will give your function a go and let you know the results.

Regards

Gerry

ErosOlmi
12-03-2008, 18:48
Gerry,

if you can, it would be nice for us to see a picture of your oled display.
Maybe better if working with thinBasic script ;)

Thanks a lot.
Ciao
Eros

kryton9
13-03-2008, 01:14
Gerry sounds like a cool project. Eros beat me to the request, but would love to see photos or even a video clip of it working! Good luck and looking forward to being WOW'd.

GKeely
13-03-2008, 14:25
Thanks to all for your support

It turns out that the COMM_RECV works correctly and that a time delay is necessary after all. ???
I'm not using the OLED for anything particular but just thought that controlling it from a PC would be fun(like using a sledgehammer to crack a nut).Hopefully I can get an image to you later,i don't have a camera available.Thanks again and now back to studying more of thinbasic. :D
For those interested details of the display are here
http://www.4dsystems.com.au/prod.php?id=11

regards

Gerry

ErosOlmi
13-03-2008, 15:05
Well, it's not important if you are working on a little experiment or on the next Robotic Rover going to Mars. For us is important to see if we can improve thinBasic for whatever need a thinBasic user can have. That's why I asked ;)
You know, intelligence is a mix of open mind, curiosity and perseverance :D
Something good will come out.

Thanks a lot for your feedback.
Ciao
Eros

Michael Clease
13-03-2008, 15:26
Thanks for the link Gerry, I followed it to a uk supplier who does some very interesting toys. ;D

http://www.milinst.com/

Petr Schreiber
13-03-2008, 18:36
Hi GKeely,

that is fantastic device looking at the features!

Just one note from my side, reacting to your first post - instead of:


Comm_Send(hComm, "command" & chr$(val1) & chr$(val2) & ... )


you can use directly this shorter form:


Comm_Send(hComm, "command" & chr$(val1, val2, ...) )


or even the ultrasmallone:


Comm_Send(hComm, CHR$("command",val1, val2, ...) )


Isn't thinBASIC great :D ?

Have fun with the display,
Petr

ErosOlmi
13-03-2008, 19:01
Thanks a lot Petr about CHR$ (http://www.thinbasic.com/public/products/thinBasic/help/html/chr$.htm) power.
Sometimes even I forget about the hard work we did together to tune up some language keyword.

Ciao
Eros