Showing posts with label Interfacing DB9. Show all posts
Showing posts with label Interfacing DB9. Show all posts

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