Monday, May 9, 2011

Serial Comm RS232 port Interface (Gambas2)


Introductions:

Serial communication is very important tools required in programming, its serial functions must be learned by a programmer too. In some electronic interfacing we frequently use serial interfacing RS232,RS485 and etc.

Requirements:
Gambas 2 interpreter/compiler
gb.net project component

Objectives:
1) To discuss the use of serial communications
2) To discuss the function of serial interface using gambas2
3) To demonstrate a simple program in gambas2


Methodology:
0) check the availability of serial port device " ls -l /dev/ttyS* "
1) check the ownership of "uccp" -default is owned by "root"
2) Open Gambas2
3) Add gb.net in the project component
3) Create a form
4) Specify setting in COM port:
Data bits
Flow control
Parity
Portname
Speed
Stopbits


Detail(1) Project properties to choose serial components


Detail(2) Icon for serial component


Detail(3) Serial Comm properties


Detail(4) Cut and paste source code
PRIVATE SComm AS SerialPort CONST None AS Integer = 0

PRIVATE Rx AS String  PUBLIC SUB Form_Open()
SComm = NEW SerialPort AS "SComm"
SComm.PortName = "/dev/ttyUSB0"
SComm.Speed = "19200"
SComm.Parity = 0
SComm.DataBits = "8"
SComm.StopBits = "1"
SComm.FlowControl = 0
TRY
SComm.Open()

IF ERROR THEN

TRY
SComm.PortName = "/dev/ttyS0"
SComm.Speed = "19200"
SComm.Parity = 0
SComm.DataBits = "8"
SComm.StopBits = "1"
SComm .FlowControl = 0

SComm.Open()

  IF ERROR THEN    
Message("No Serial Come Exiting...")
QUIT
ENDIF  

ENDIF

END  'Private RX

'******** Incoming serial data ********
PUBLIC SUB SComm _Read()
SLEEP 0.025
TRY READ #SComm , Rx, Lof(SComm )
IF ERROR THEN
Message.info(No received data!)
ENDIF

IF Len(Rx) > 8 THEN
TextBox1.Text= Cstr(Rx)
END IF

END

PUBLIC SUB Form_Close()
SComm.Close()
QUIT
END


Detail(5) Error message if serial port is not available


Detail(6): Checking serial port "ls -l /dev/ttyS*"


Detail(7) chown -R group.user /dev/ttyS0

Detail(8) Running serial interface -gambas2 example





Remarks:
Hints:
Problem: Can't open serial port#

1) Create a user and group to add it uucp(if you don't have a login-name)
root@localhost#groupmod –-add-user user_name uucp

2) change ownership (if you t have a login-name)
root@localhost# chown -R your_group.your_user /dev/ttyS0
Still on evaluations, and we hope that gambas2 developers
would add more features in the serial functions.

Problem: Can't have a faster read via serial port?
1) Try to empty first the buffer before reading the next data
buffer =""
buffer=read_serial()

2) of course you can adjust the faster baud rate your host device is capable too.


Conclusions:
A simple serial interfacing

E^3

14 comments:

  1. Hello folks!


    Hoping that we've solved all your problem in Gambas2 serial interfacing

    ReplyDelete
  2. Hi, I'm writing a program to support GPSa logger. The program
    correctly handles reading data from the GPS device via bluetooth. But
    when I try to read data through USB is unfortunately the program does
    not receive anything. And here I am in the place because I do not know
    if it's my fault configuration (maybe more-capable port control
    settings) or the fault lies with the Lubuntu (and maybe my laptop?).
    By the console command: cat / dev/ttyUSB0 I get the whole sequence of
    NMEA data from GPS devices. My program connects to the USB port but it
    does not get any data. I removed the code limits the size of data
    downloaded. It did not help. Reading from / dev/rfcomm0 (bluetooth)
    works perfectly. Maybe someone has an idea where I could deal with it.

    Below the code to connect with the port and receive data:

    PUBLIC SUB button_polacz_Click()
    WITH SerialPort1
    .PortName = Trim(tbPortName.Text)
    .Speed = vb_baudrate.Value
    END WITH
    IF SerialPort1.Status = 0 THEN
    SerialPort1.Open
    Timer1.Delay = vb_timerdelay.Value
    Timer1.Start
    status_portu()
    Timer2.Start
    button_polacz.Enabled = FALSE
    Button1.Enabled = TRUE
    ENDIF
    CATCH
    message.Error((Error.Code) & (Error.text) & (Error.Where))
    END

    PUBLIC SUB odbierz_dane()
    SLEEP 0.025
    dane_port = ""
    READ #SerialPort1, dane_port, Lof(SerialPort1)
    PRINT dane_port
    IF dane_port <> "" AND Len(dane_port) > 300 AND Left$(dane_port,
    1) = "$" THEN
    rozdziel_dane()
    ENDIF
    CATCH
    PRINT (Error.Code) & (Error.text) & (Error.Where)
    END

    ReplyDelete
  3. Hello Sir Leszek,

    Actually when I read your problem you were interfacing it to a USB port and you are reading it to a serial port(?).

    My advise ; you need to have a virtual COM port for your USB interface.It will catch data from USB protocol and convert it to a RS232 protocol (Serial port) -then you will read something(provided also with correct port config)

    1) Pyserial port can give you the idea
    2) Pseudo Terminal in Linux
    http://en.wikipedia.org/wiki/Pseudo_terminal
    3) or you need a USB to serial adapter(this is easy and cheap also.

    Thanks

    E^3

    ReplyDelete
  4. Problem solved. I typed too little value baudrate. Thanks for your interest and ideas. PySerial particularly intrigued me.

    ReplyDelete
  5. Hi Sir LeszekK,

    Wow,that's good for you.

    You can try the Gambas2 example code on serial port application if you want.

    Cheers!


    E^3

    ReplyDelete
  6. Hi dijalilosse,

    Good Day.

    Please give me your dummy e-mail , so that I can give you the gambas-example source code in using serial port.

    Let's see then what errors will come.

    Thanks

    -Christopher

    ReplyDelete
  7. Hi Sir adelghouari,

    I've already send you the serial port source code found in a gambas-example..try to run that code , that was tested.

    If ever there will be a bugs just post the infos here.

    Cheers!

    -E^3

    ReplyDelete
  8. hi djalilosse,

    You're Welcome...

    I will try to add some more Gambas tutorials in my free time..

    E^3
    Philippines

    ReplyDelete
  9. Hi Adelghouari,

    You can try this..
    =====================================================
    1) Make a function to return the Rx string
    PUBLIC SUB SComm _Read() as STRING
    SLEEP 0.025
    TRY READ #SComm , Rx, Lof(SComm )
    IF ERROR THEN
    Message.info(No received data!)
    ENDIF

    RETURN Rx
    END

    2) Insert TextBox in Rx a string
    PUBLIC SUB SComm _Read() as String
    SLEEP 0.025
    TRY READ #SComm , Rx, Lof(SComm )

    IF ERROR THEN
    Message.info(No received data!)
    ELSE
    Textbox1.Text=Cstr(Rx)
    ENDIF

    END
    =====================================================
    Cheers!

    E^3

    ReplyDelete
  10. Hi djalilosse,

    Good Day.
    You can use the string manipulation of Gambas2
    ex: Comp(), Letf$(),Right$()

    In this code example I am decoding the string which contains a "SRF04" word then following the condition after

    (1)
    IF (Comp(Left$(s, 5), "SRF04") = 0) THEN

    'then here is what you will do??

    ENDIF

    (2) Or replace some word contain in the string ,etc.
    distance=Replace$(s,"distance","")

    Hope this helps..

    Cheer!

    -E^3

    ReplyDelete
  11. Hi dijalilosse,

    You may subscribe this blog by joining this site above.

    And please let us know the progress in your project so that we can post it here..

    Your welcome and cheers!

    -E^3
    Philippines

    ReplyDelete
  12. nice, it's work. how about if i connect my arduino to raspi with rx tx wire ? i think it will be different program, but i don't know, please give me the answer ^^

    ReplyDelete
  13. Hi Aldi,

    Yes it will work,just follow the same procedure
    and issue this command in the cli "ls-lst /dev/ttyU* " to know the available serial ports.

    cheers!

    -E^3

    ReplyDelete
  14. http://gambaslinux.fr/articles.php?lng=en&pg=2022

    ReplyDelete