View Full Version : Comm Rs232 activation
Hi all !
When opening a Comm port, I am not sure of the exact Timing.
1- To test Thinbasic I opened a port with another com program.
open com 2, 4800, 8,0,1
and kept the port open. Then I opened the same port with below code and got no error reporting that the port was already open. What normally happens- in this circumstance. (I also got no data etc... in any configuration:))
So I was wondering ...
a- should/does Thinbasic lock/hog an open port - like other programs ?
b- the error I do get has to do with a bracket ")" line 26 ? ... so I probably use the wrong method/code...
What is the regulard way to open the port and when does it lock other programs out- which might want to use the same
port ?
Is thete any -cast-iron ways to work with ports .
Thanks
Rob
-o-o-
code below...
' Basic Template for custom dialog
' Start Date 01-29-2009
' Created by by
USES "UI"
uses "COMM"
'----------------------------------------------------------------------
Dim hComm as long
Dim GetPortData as String
Dim Retvalue as String
hComm = comm_freefile
comm_open(2, hComm)
COMM_Set(hComm, %COMM_BAUD, 4800)
COMM_Set(hComm, %COMM_PARITY, 0)
COMM_Set(hComm, %COMM_BYTE, 8)
COMM_Set(hComm, %COMM_STOP, 1)
COMM_Set(hComm, %COMM_RTSFLOW, 0)
'RetValue = COMM_Recv(hComm, NBytes, VariableBuffer, TimeOutInMS)
RetValue = COMM_Recv(hComm, 20, GetPortData, 5000)
' COMM_Send(sComPort, sBuffer)
msgbox 0, "hi: " + hComm + " " + RetValue,,,50000
Michael Hartlef
29-01-2009, 10:49
Hi Rob,
try
comm_open("COM2", hComm)
The first parameter has to be a string.
Michael Hartlef
29-01-2009, 10:52
Also I'm not sure if you have to set the port parameters BEFORE you open it.
Michael Hartlef
29-01-2009, 10:56
In the samples COMM folder, there is a script for getting data form a modem. Maybe that can help also.
uses "Console"
uses "COMM"
dim nModems as long = 4
dim Count as long
dim CountATI as long
dim hComm as long
dim nBytes as long
dim sBuffer as string
for count = 1 to nModems
hComm = comm_freefile
console_writeline("Opening COM" & Count & " as hComm=" & hComm)
comm_open("COM" & Count, hComm)
if err = 0 then
console_writeline("...open ok.")
for CountATI = 1 to 5
console_writeline("Now sending ATI" & CountATI)
comm_print(hComm, "ATI" & CountATI)
SLEEP 1000 ' delay for modem to respond
nBytes = COMM_Get(hComm, %comm_RXQUE)
COMM_recv(hComm, nBytes, sBuffer)
console_writeline(sBuffer)
next
console_writeline("...closing port COM" & Count)
comm_close(hComm)
else
console_writeline("Error: " & err)
end if
next
console_writeline("----------------------------------------")
console_writeline("End testing. Press any key to finish.")
console_writeline("----------------------------------------")
console_waitkey
Thanks Michael !
I'll give it a try :)
Setting parameters before opening the port ? ...hmmm :shock: :violent: Aha..! ;)
:)
many thanks !!!
& best regards
Rob
-o-o-
giuspage
27-05-2009, 10:02
Hello, even if I don’t like ‘object oriented programming’, I'm a new friend of thinbasic and
I'm trying to interface a pic sistem to my PC
I took your suggestions about ioptic's code
as follow:
USES "UI"
uses "COMM"
Dim hComm as long
Dim GetPortData as String
Dim Retvalue as String
COMM_Set(hComm, %COMM_BAUD, 4800)
COMM_Set(hComm, %COMM_PARITY, 0)
COMM_Set(hComm, %COMM_BYTE, 8)
COMM_Set(hComm, %COMM_STOP, 1)
COMM_Set(hComm, %COMM_RTSFLOW, 0)
hComm = comm_freefile
comm_open("COM2", hComm)
RetValue = COMM_Recv(hComm, 20, GetPortData, 5000)
msgbox 0, "hi: " + hComm + " " + RetValue,,,50000
but when i test the code I receive an error about
this line:
RetValue = COMM_Recv(hComm, 20, GetPortData, 5000)
In few words it notifies that the close bracket ')' is omitted (but actually it is not omitted).
What can I do it to overcome the problem ?
Thank you for helping me
Giuseppe
ErosOlmi
27-05-2009, 10:18
Ciao Giuseppe,
I think we made an error in documentation.
There are 2 RECV functions, one with 3 parameters and one with 4 called COMM_TRECV
COMM_TRECV accepts a timeout parameters as 4th parameter while COMM_RECV has just 3 parameters.
So the two functions have the following syntax:
RetValue = COMM_TRecv(hComm, NBytes, VariableBuffer, TimeOutInMS)
COMM_Recv(hComm, NBytes, VariableBuffer)
In your case you should use COMM_TRecv I suppose.
Sorry for the error, I will fix documentation in next thinBasic release.
Let me know if my reply solves the problem.
Eros
giuspage
31-05-2009, 11:48
i did your corrections and i don't receive that error anylonger
but the code is not working as i wanted:
i alway obtain this result
RetValue = -1
and GetPortData is empty
this code is not able to read the port :unguee:
I used the windows hyperterminal to test the connection and I conferm that the computer can receive perfectly the data from the pic
what can i do it ? please help me
ciao
Giuseppe
ErosOlmi
31-05-2009, 11:59
Ciao Giuseppe,
have you tried \thinBasic\SampleScripts\COMM\TestModems.tBasic script which try to connect to a standard modem from port 1 to 4 and get modem info?
Maybe that can give you more info on how to communicate with serial devices using thinBasic COMM module.
Do you need to send a command to your serial device before getting data?
Do you give enough time to device in order to reply?
Do you test how many bytes are present in buffer before getting them?
Not easy for me to help on serial communications because, apart standard modems, every device has its own way.
Let me know.
Eros
Michael Hartlef
31-05-2009, 15:37
Hello, even if I don’t like ‘object oriented programming’, I'm a new friend of thinbasic and
I'm trying to interface a pic sistem to my PC
I took your suggestions about ioptic's code
as follow:
USES "UI"
uses "COMM"
Dim hComm as long
Dim GetPortData as String
Dim Retvalue as String
COMM_Set(hComm, %COMM_BAUD, 4800)
COMM_Set(hComm, %COMM_PARITY, 0)
COMM_Set(hComm, %COMM_BYTE, 8)
COMM_Set(hComm, %COMM_STOP, 1)
COMM_Set(hComm, %COMM_RTSFLOW, 0)
hComm = comm_freefile
comm_open("COM2", hComm)
RetValue = COMM_Recv(hComm, 20, GetPortData, 5000)
msgbox 0, "hi: " + hComm + " " + RetValue,,,50000
but when i test the code I receive an error about
this line:
RetValue = COMM_Recv(hComm, 20, GetPortData, 5000)
In few words it notifies that the close bracket ')' is omitted (but actually it is not omitted).
What can I do it to overcome the problem ?
Thank you for helping me
Giuseppe
Hi Guiseppe,
welcome here on the board. I noticed in your posted code that you set the parameters before got a Hcomm value back from COMM_FREEFILE. I think that has to be the first step before oyu can set the parameters. Maybe you even to open it before you can set the params.
Michael
Ps.: After reading the helpfile I am pretty sure that you have to open the port first too before the use of COMM_SET.
giuspage
31-05-2009, 18:20
[/quote]
Hi Guiseppe,
welcome here on the board. I noticed in your posted code that you set the parameters before got a Hcomm value back from COMM_FREEFILE. I think that has to be the first step before oyu can set the parameters. Maybe you even to open it before you can set the params.
Michael
Ps.: After reading the helpfile I am pretty sure that you have to open the port first too before the use of COMM_SET.
[/quote]
Hi Michael,
I did your correction but it didn't work
Anyway thank you !
Now I'm trying the 'TestModems.tBasic script' and it seems to work very well.
giuspage
31-05-2009, 18:31
Ciao,
i'v just tried the TestModems.tBasic with some modifications. Fantastic !! it is working perfectly !! :)
is it possible to obtain the same result with a different graphic without using a 'dos' windows ?
Forever thank you !! :D
Giuseppe
have you tried \thinBasic\SampleScripts\COMM\TestModems.tBasic script which try to connect to a standard modem from port 1 to 4 and get modem info?
Maybe that can give you more info on how to communicate with serial devices using thinBasic COMM module.
Do yo
u need to send a command to your serial device before getting data?
Do you give enough time to device in order to reply?
Do you test how many bytes are present in buffer before getting them?
Not easy for me to help on serial communications because, apart standard modems, every device has its own way.
Let me know.
Eros
[/quote]
Michael Hartlef
31-05-2009, 18:49
I'm glad you got it working. It would nice to post your script inside the samples area as I will have a use for it sooner or later. I want to get a PIC too to buidl a lap counter for my home slotracing track.
Michael Hartlef
31-05-2009, 18:50
And of course, you can use TBGL or the CANVAS module. There are several graphical options for you to use in thinBasic.
Petr Schreiber
31-05-2009, 21:52
ThinBasic has many ways to present your data visually:
console - you are already familiar with
dialogs - check SampleScripts/UI for samples of Windows look-and-feel applications
2D graphics window or 2D graphic control inside dialog ( Canvas )
2D/3D graphics window or 2D/3D hardware accelerated graphics inside dialog - see SampleScripts/TBGL
...
Petr