Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Comm Rs232 activation

  1. #1

    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...




    [code=thinbasic]' 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,
    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[/code]

  2. #2

    Re: Comm Rs232 activation

    Hi Rob,

    try
    [code=thinbasic]

    comm_open("COM2", hComm)

    [/code]

    The first parameter has to be a string.

  3. #3

    Re: Comm Rs232 activation

    Also I'm not sure if you have to set the port parameters BEFORE you open it.

  4. #4

    Re: Comm Rs232 activation

    In the samples COMM folder, there is a script for getting data form a modem. Maybe that can help also.
    [code=thinbasic]
    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
    [/code]

  5. #5

    Re: Comm Rs232 activation

    Thanks Michael !

    I'll give it a try

    Setting parameters before opening the port ? ...hmmm Aha..!



    many thanks !!!
    & best regards
    Rob
    -o-o-




  6. #6

    Re: Comm Rs232 activation

    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,
    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




  7. #7
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Comm Rs232 activation

    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:
    [code=thinbasic]RetValue = COMM_TRecv(hComm, NBytes, VariableBuffer, TimeOutInMS)
    COMM_Recv(hComm, NBytes, VariableBuffer)[/code]

    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
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  8. #8

    Re: Comm Rs232 activation

    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


    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

  9. #9
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Comm Rs232 activation

    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

    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  10. #10

    Re: Comm Rs232 activation

    Quote Originally Posted by giuspage
    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,
    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. RS232 Control with Window Box
    By oldpapa49 in forum thinBasic General
    Replies: 1
    Last Post: 24-03-2009, 09:08

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •