SERIAL RS232 VB NET - Bag1 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

Menggunakan

TRAINNING AT PT MIKUNI INDONESIA


MM2100
DAFTAR ISI
TEMA

1 Dasar Serial RS-232


2 Dasar pemrograman Serial Port dgn
VB Net
3 Mengirim data string ke Serial Port
4 Menerima data string dari Serial port
5 Parsing Data String

6 Praktikum : komunikasi Vb.net dgn


modem
COMPARISON OF POPULAR COMPUTER INTERFACES

Interface Format Number of Distance Speed Typical Use


Devices

RS-232 asynchronous 2 30m 20K modem


serial

RS-485 asynchronous 32 1200m 10M Control system


serial

Ethernet serial 1024 500m 10G Computer


Network

GPIB parallel 15 18m 8M Instrumentation

I2C synchronous 40 5m 3.4M Microcontroller


Serial (w/ clock line) comm

Parallel Port parallel 2 9m 8M printer

SPI synchronous 8 3m 2.1M Microcontroller


serial comm

USB asynchronous 127 5m 12M PC peripheral


serial

Bluetooth 100m 30M


1
Mobile transfer
data
ZigBee 50m 250K Sensor network
The electrical specifications of the serial port is contained in the EIA
(Electronics Industry
Association) RS232C standard. It states many parameters such as -
1. A "Space" (logic 0) will be between +3 and +25 Volts.
2. A "Mark" (Logic 1) will be between -3 and -25 Volts.
3. The region between +3 and -3 volts is undefined.
4. An open circuit voltage should never exceed 25 volts. (In Reference to
GND)
5. A short circuit current should not exceed 500mA. The driver should be
able to handle this without damage.
male on the back of the PC
DTE
KOMUNIKASI
SERIAL
DCE

DATA TERMINAL
EQUIPMENT DATA
COMMUNICATION
EQUIPMENT

Devices which use serial cables for their communication are split into two
categories. These are
DCE (Data Communications Equipment) and DTE (Data Terminal Equipment.)
Data Communications
BAGIAN SYSTEM KOMUNIKASI SERIAL
PROGRAM

C:/>_

PROGRAM PROGRAM

WIRELESS
C:/>_

WIRE PHISICAL LINK


PHISICAL LINK

PROTOCOL : FORMAT DAN ATURAN KOMUNIKASI ANTARA


2 ATAU LEBIH DEVICE ELECTRONIK
Contoh Media transmisi

1. Wire :
2. Wireless : infrared, RF, zigbee,

CONTOH PROTOKOL KOMUNIKASI SERIAL


AT commands protocols for modems

NMEA 0183 protocol for global positioning systems(GPS)

Modbus protocol for industrial control and monitoring

FORMAT DATA

1. BYTE : 1010010 (angka 0 -255)


2. TEXT/STRING: TEST

Dim text As String = "hello"


Dim ascii As New System.Text.ASCIIEncoding()
myComPort.Encoding = ascii
myComPort.Write(text)
RS-232 PIN COFIGURATION
RS-232 D-sub connectors
5.GND GND
4.DTR DTR
3.TX TX
2.RX RX
1.DCD DCD

5 1
1 5
6 9
9 6

6.DSR
DSR
7.RTS RTS
8.CTS
9.RI
PIN Full Name Function

TD Transmit Data Serial Data Output (TXD)

RD Receive Data Serial Data Input (RXD)

CTS Clear to Send Modem Siap

DCD Data Carrier Modem mendeteksi carrier


Detect
DSR Data Set Ready Memebritahu bahwa modem siap

DTR Data Terminal Memeberi tahu modem bahwa terminal siap


Ready
RTS Request To Send Memberitahu modem bahwa UART siap.

RI Ring Indicator Goes active when modem detects a ringing signal from the
PSTN.
RS-232 Specifications

Cabling Single-ended
Number of Devices 1 transmit, 1 receive
Communication Mode Full duplex
Distance (max) 50 feet at 19.2kbps
Data Rate (max) 1Mbps
Signaling Unbalanced
Mark (data 1) -5V (min) -15V (max)
Space (data 0) 5V (min) 15V (max)
Input Level (min) 3V
Output Current 500mA (driver Ics in PCs are limited to 10mA)
Impedance 5kW (Internal)
Bus Architecture Point-to-Point
WORD FORMAT

START
BIT
STOP
BIT
0 1 0 1 0 0 0 1 0 0 1
D0 D7

DATA
BYTE
LEVEL TEGANGAN RS232

-15

5V

-5V

15 V
RS-232 Flow Control

1. Hardware Flow control


-melalui pin RTS dan CTS
-melalui pin DSR dan DTR
2. Sotware Flow control
- Xon dan Xoff
3. No flow control
RS-232 hardware flow control

REQUEST TO
CLEAR TO SEND
SEND
RTS CTS

CTS RTS

RX TX
DATA
TX RX

COM PORT COM PORT


PC PC
RS-232 hardware loop back flow control

COM PORT
COM PORT wiring a Null Modemb
PC
PC

Above is my preferred method of wiring a Null Modem. It only requires 3 wires (TD, RD & SG)
to be wired straight through thus is more cost effective to use with long cable runs. The theory of
operation is reasonably easy. The aim is to make to computer think it is talking to a modem rather than
another computer. Any data transmitted from the first computer must be received by the second thus TD
is connected to RD. The second computer must have the same set-up thus RD is connected to TD. Signal
Ground (SG) must also be connected so both grounds are common to each computer.
The Data Terminal Ready is looped back to Data Set Ready and Carrier Detect on both
computers. When the Data Terminal Ready is asserted active, then the Data Set Ready and
Carrier
Detect immediately become active. At this point the computer thinks the Virtual Modem to
which it is
connected is ready and has detected the carrier of the other modem.
All left to worry about now is the Request to Send and Clear To Send. As both computers
communicate together at the same speed, flow control is not needed thus these two lines are
also linked
together on each computer. When the computer wishes to send data, it asserts the Request
to Send high
and as it's hooked together with the Clear to Send, It immediately gets a reply that it is ok
to send and
does so.
software flow control

REQUEST TO
CLEAR TO SEND
SEND
RTS CTS

CTS RTS

RX TX

TX Xon(11h)
RX
Xoff (13h)

COM PORT COM PORT


PC Xon code(11h): silahkan kirim data PC
Xoff code(13h): stop pengiriman data
Microcontrollers kebanyakan tidak ada CTS & RTS lines

REQUEST TO
SEND
RTS

CTS CLEAR TO SEND

RX TX
DATA
TX RX

COM PORT Microcontrollers


PC
Penyesuaian tegangan serial RS 232 PC dgn
Microcontroller

+15V
+5V
0 0

-15V

RS-232 Microcontrollers
Penyesuaian tegangan serial RS 232 PC dgn
Microcontroller

+15V
+5V
0 0

-15V

RS232
RS-232 DRIVER Microcontrollers
Penyesuaian tegangan serial RS 232 PC dgn
Microcontroller

+15V
+5V
0 0

-15V

RS232
RS-232 DRIVER Microcontrollers
VB Net Applications can access direct to physical serial ports

USB to RS232 converter is a USB devices function as virtual COM ports.


Applications can access in the same way as physical serial ports
Need usb driver
usbser.sys driver is a bus driver that manages communications between
the operating systems COM-port driver and USB drivers

USB TO RS232 CONVERTER


each COM port, an operating-system driver assigns a symbolic link name
such as COM1, COM2 and so on, which applications use to detect and access
the port.

The Windows Device Manager shows information about each COM port
The serial.sys driver controls communications with COM-port devices.

For .NETs programmers, the SerialPort class provides a convenient way to


access COM-port devices.
MICROCONTROLLER
MICROCONTROLLER
VB NET MENGGUNAKAN SEBUAH CLASS UNTUK
MEMPERMUDAH PEMROGRAMAN SERIAL PORT

APLIKASI VB
NET

CLASS
SERIALPORT
Class SerialPort
Proverty BaudRate (9600, 19200, 38400 dst)
(data) DataBits ( 7 bit , 8 bit)
Parity (ODD, EVEN,NONE)
StopBits ( 1, 2 )
ReadTimeOut
PortName
BytesToRead
dll
Methode Open()
(fungsi) Close()
isOpen()
Write()
WriteByte()
ReadByte()
DtrEnable()
GetPortNames()
dll
SerialPort Methode / (fungsi)
SerialPort Proverty (data)
MENERIMA DATA DENGAN POOLING DAN INTERUPSI

standby
Aktif

interupsi
polling Bufer
serial
Bufer
serial

POLLING : PROGRAM MEMERIKSA SECARA PERIODIK BUFFER


APAKAH ADA DATA YG MASUK.
.

INTERUPSI : KETIKA ADA DATA MASUK AKAN ADA


PEMBERITAHUAN KE CPU/PROGRAM
(pada Vb.net menggunakan fitur event driven)
CONTOH APLIKASI SERIAL PORT
TAHAP MEMPROGRAM Serial Port

1. BUAT NEW PROJECT

2. TAMBAHKAN NAMESPACE IO.PORT

3. BUAT OBJEK SERIALPORT

4. SET PROPERTY

5. OPEN PORT

6. SEND / RECEIVE DATA

7. CLOSE PORT
Sertakan namespace dimana
Class serialport berada

Namespace tempat
class SerialPort berada

Imports System.IO.Ports
BUAT OBJEK SERIALPORT

Imports System.IO.Ports

Nama class
SerialPort

Friend SerialPort1 As New SerialPort

Nama objek
Yg dibuat
BUAT OBJEK dengan DRAG and DROP Componen

SerialPort1 Drag and drop


2. SET PROPERTY
SerialPort1.BaudRate = 9600

SerialPort1.Parity = IO.Ports.Parity.None

SerialPort1.StopBits = IO.Ports.StopBits.One

SerialPort1.PortName = COM1

SerialPort1.DataBits = 8

SerialPort1.ReadTimeout =100 milidetik


3. OPEN PORT
SerialPort1.Open()

Private Sub openPort_Click()

If SerialPort1.IsOpen Then Exit Sub

On Error GoTo pesan_eror


SerialPort1.Open()
Exit Sub

Pesan_eror:
MsgBox( port tdk bisa dibuka")

End Sub
4. SEND DATA
Methode Type data
Serialport1.Write() Array byte, Array Char &
String,
SerialPort1.WriteLine String + New Line
SerialPort1.WriteByte Byte
Contoh1:

SerialPort1.Write(TextBox1.text)

Contoh2:

Dim data(3) as byte


data(0)=O
data(1)=K
data(3)=10
SerialPort1.WriteByte(data,0,3)
4. Read DATA
Methode Type data
Serialport1.Read() Array byte, Array Char

SerialPort1.ReadLine String sampai Newline


SerialPort1.ReadByte Byte
SerialPort1.ReadChar char
SerialPort1.ReadExsiting string
SerialPort1.ReadTo String sampai char pembatas
Contoh1:

SerialPort1.Write(TextBox1.text)

Contoh2:

Dim data(3) as byte


data(0)=O
data(1)=K
data(3)=10
SerialPort1.WriteByte(data,0,3)
5. CLOSE PORT
SerialPort1.Close()

Private Sub closePort_Click()

If SerialPort1.IsOpen Then

SerialPort1.Closed()

End if

End Sub
Contoh KODE MENERIMA DATA DENGAN INTERUPSI

Dim Tampung(100) As Char


Dim RXCount As Integer
Dim ReceiveByte As Byte
Dim WithEvents SerialPort1 As New SerialPort

Private Sub Receiver() Handles SerialPort1 DataReceived


Do
RXCount=0

Do
ReceiveByte = SerialPort1.ReadByte
Tampung(RXcount) = ReceiveByte
RXCount++
Loop Until(SerialPort1.BytesToRead=0)

Me.Invoke(New MethodInvoker( AddressOf TampilkanData))


Loop Until(SerialPort1.BytesToRead=0)
End Sub

Private Sub TampilkanData()


txtDataDiterima.AppendText(New String(Tampung, 0, RXcount))
End Sub
Contoh KODE MENERIMA DATA DENGAN POOLING

Dim RXcount As Integer

Private Sub getResponse()


Dim RXByte As Byte
Do
RXCnt = 0
Do
RXByte = SerialPort1.ReadByte
RXArray(RXCnt) = Chr(RXByte)
RXCnt = RXCnt + 1
Loop Until (SerialPort1.BytesToRead = 0)
responseText = New String(RXArray, 0, RXCnt)
Exit Sub

Loop Until (SerialPort1.BytesToRead = 0)

End Sub
Konversi type data

Konversi data string ke byte 0xA -> 10

Dim databyte As Byte


databyte= Convert.ToByte(dataString, 16)

Konversi data byte ke string 10-> 0xA


Dim datastring As string atau char
datastring= HEX$(databyte)

Konversi kode ascii ke karakter chr(97) -> a


Dim datastring As string atau char
datastring= Chr(databyte)

Konversi karakter ke kode ASCII asc(a) -> 97


Dim datastring As string atau char
datastring= Asc(databyte)
Mengirim data string & menerima Response

Dim RXArray(2047) As Char


Dim RXcount As Integer
Dim ResponseText As String
Private Sub Button1_Click( )
SerialPort1.Write(commandString.Text )
Call getResponse()
responseBox = responseText
End Sub

Private Sub getResponse()


Dim RXByte As Byte
RXcount = 0
Do
RXByte = SerialPort1.ReadByte
RXArray(RXcount ) = Chr(RXByte)
RXcount = RXcount + 1
Loop Until (SerialPort1.BytesToRead = 0)
responseText = New String(RXArray, 0, RXcount )
End Sub
Mengirim data byte & get respon
(bahasa pemrograman C#)
public Send()
{
byte[] message = new byte[8];
byte[] response = new byte[8];

SerialPort1.Write(message, 0, message.Length);
GetResponse(ref response);
}

private void GetResponse(ref byte[] response)


{
for (int i = 0; i < response.Length; i++)
{
response[i] = (byte)(SerialPort1.ReadByte());
}
}
PARSING DATA STRING

DataString.Substring(awalchar , jumlah_char)
Contoh

ResponseText = 1A200

TextBox1.Text = responseText.Substring(0, 1)
TextBox2.Text = responseText.Substring(1, 1)
TextBox3.Text = responseText.Substring(2, 3)

TextBox1.Text = 1
TextBox2.Text = A
TextBox3.Text = 200
Imports vb = Microsoft.VisualBasic

Dim datasplit() As String


Dim dataserial As String = 123#456#789#777

datasplit = vb.Split(dataserial, #)

TextBox1.Text = datasplit(0)
TextBox2.Text = datasplit(1)
TextBox3.Text = datasplit(2)
TextBox4.Text = datasplit(3)

TextBox1.Text = 123
TextBox2.Text = 456
TextBox3.Text = 789
TextBox4.Text = 777
Praktikum :
Komunikasi Serial VB.Net dgn Modem

AT COMMAND
SMS
MODEM CENTER

HANDPHONE
TAHAPAN
1. MENCARI NOMOR COM PORT

2. BUAT PROJECT BARU

3. BUAT OBJEK SERIALPORT

4. SET PROPERTY

5. OPEN PORT

6. SEND AT COMMAND / RECEIVE RESPON MODEM

7. CLOSE PORT
Cara Mengetahui Nomor Com Port Modem
AT COMMAND DAN RESPON MODEM
NO AT COMMAND RESPONSE contoh
1 AT <CR><LF>OK<CR><LF> OK, ERROR
2 AT+CGMM Model UW100, E153
3 AT+CGMI Manufacturer huawei
4 AT+CGSN Product Serial Number 352375041161282
5 AT+CMGF=1 OK Set ke text mode

6 AT+CMGS AT+CMGS=+62871234
567

AT COMMAND

MODEM
RESPONSE
PENGUJIAN KOMUNIKASI DENGAN
MODEM MENGGUNAKAN APLIKASI
HYPERTERMINAL

ANDA KETIK
AT COMMAND
RESPON DARI
MODEM
KETIK LALU
AT TEKAN
ENTER
AT RESPON DARI
OK MODEM
KETIK LALU
AT TEKAN
OK ENTER
AT+CGMM
AT RESPON DARI
OK MODEM
AT+CGMM
E153
Komunikasi Aplikasi VB.net dgn Modem

KETIK
AT+CGMM AT COMMAND
DISINI
E153

RESPON DARI
MODEM
Kode VB.Net mengirim string AT COMMAND
Private Sub Button1_Click( ) Handles Button1.Click

Dim TextString As String

If SerialPort1.IsOpen Then
TextString = commandString.Text + Chr(13)

Try
If SerialPort1.IsOpen = False Then
openCommPort()
End If

SerialPort1.Write(TextString)

Call getResponse() ' get a response within timeout.


responseBox.Text = responseBox.Text & responseText
Catch ex As Exception
MsgBox(ex.Message & CEK KONEKSI")
End Try
Else
MsgBox("COM port is closed.")
End If
End Sub
Kode VB.Net membaca RESPON dari MODEM

Dim RXArray(2047) As Char


Dim RXCnt As Integer

Private Sub getResponse()


Static responseBuffer As String
Dim RXByte As Byte
RXCnt = 0

Do
RXByte = SerialPort1.ReadByte
RXArray(RXCnt) = Chr(RXByte)
RXCnt = RXCnt + 1
Loop Until (SerialPort1.BytesToRead = 0)

responseText = New String(RXArray, 0, RXCnt)

End Sub

You might also like