How to get data from serial port to GUI textArea

From Serial Port.....

Messages from base (always 5 bytes):

0xAA, 0, 0, 0, 0xBB - IDLE Mode

0xAA, 0, 0x80, 0x80, 0xBB - Question Mode

messages from terminal (always 6 bytes):

0xAA, a, b, c, chksum, 0xBB

where chksum = a+b+c;

a = address (0-250)

the 2 MSB's of b represenet the answer:

0 0 - A

0 1 - B

1 0 - C

1 1 - D

the 6LSB's of b and 8bits of c is the time for answer, in milliseconds.

(0-16384 milliseconds)

I have VB Code for that but I want the same thing in Java ...

Can any one help me?

Here's VB Code..

VERSION 5.00

Object ="{648A5603-2C6E-101B-82B6-000000000014}#1.1#0";"MSCOMM32.OCX"

Begin VB.Form Form1

BorderStyle=1'Fixed Single

Caption ="Aakar GUI"

ClientHeight=4665

ClientLeft=60

ClientTop=375

ClientWidth=6105

LinkTopic="Form1"

MaxButton=0'False

MinButton=0'False

ScaleHeight=311

ScaleMode=3'Pixel

ScaleWidth=407

StartUpPosition =3'Windows Default

Begin VB.CommandButton cmdPort

Caption ="Open Port"

Height =375

Left=120

TabIndex=4

Top =600

Width=1455

End

Begin VB.ComboBox cmbPort

Height =315

ItemData="Form1.frx":0000

Left=120

List="Form1.frx":0016

Style=2'Dropdown List

TabIndex=3

Top =120

Width=2895

End

Begin VB.CommandButton cmdEnd

Caption ="End"

Height =495

Left=1560

TabIndex=2

Top =1320

Width=1215

End

Begin VB.CommandButton cmdStart

Caption ="Start"

Height =495

Left=120

TabIndex=1

Top =1320

Width=1215

End

Begin VB.TextBox txtMessage

Height =2040

Left=119

MultiLine=-1'True

ScrollBars=3'Both

TabIndex=0

Top =2475

Width=5848

End

Begin VB.Timer tmrRead

Enabled =0'False

Interval=1

Left=2040

Top =600

End

Begin MSCommLib.MSComm MSComm1

Left=3120

Top =360

_ExtentX=1164

_ExtentY=1164

_Version=393216

DTREnable=0'False

ParityReplace=45

SThreshold=1

End

Begin VB.Label Label5

Caption ="Result Data:"

Height =375

Left=120

TabIndex=5

Top =2115

Width=1320

End

End

Attribute VB_Name ="Form1"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

Option Explicit

Dim PortIsOpen As Boolean

Dim Answers(4) As String

Dim RejectKeystroke As Boolean

Private Sub cmbPort_Change()

Debug.Print cmbPort.ListIndex

End Sub

Private Sub cmbPort_Validate(Cancel As Boolean)

'Cancel = True

End Sub

Private Sub cmdEnd_Click()

MSComm1.Output ="e"

txtMessage.Text =""

End Sub

Private Sub cmdPort_Click()

On Error GoTo ErrorHandler

If cmbPort.ListIndex < 0 Then Exit Sub

If PortIsOpen Then

cmbPort.Enabled = True

MSComm1.PortOpen = False

PortIsOpen = False

cmdPort.Caption ="Open Port"

cmdStart.Enabled = False

cmdEnd.Enabled = False

Else

MSComm1.CommPort = cmbPort.ListIndex + 1

cmbPort.Enabled = False

MSComm1.PortOpen = True

PortIsOpen = True

cmdPort.Caption ="Close Port"

cmdStart.Enabled = True

cmdEnd.Enabled = True

End If

Exit Sub

ErrorHandler:

Debug.Print Err.Number

Debug.Print Err.Description

MsgBox Err.Description, vbExclamation Or vbOKOnly,"Error opening port"

cmbPort.Enabled = True

PortIsOpen = False

cmdPort.Caption ="Open Port"

cmdStart.Enabled = False

cmdEnd.Enabled = False

End Sub

Private Sub cmdStart_Click()

MSComm1.Output ="s"

txtMessage.Text =""

End Sub

Private Sub Form_Initialize()

Dim tmp As Variant

tmp = InitCommonControls

End Sub

Private Sub Form_Load()

Answers(0) ="A"

Answers(1) ="B"

Answers(2) ="C"

Answers(3) ="D"

'MSComm1.Settings = "9600,n,8,1"

''MSComm1.Settings ="115200,n,8,1"

'MSComm1.PortOpen = True

On Error GoTo ErrorHandler1

MSComm1.CommPort = 1' comm port 1

MSComm1.RThreshold = 1' use 'on comm' event processing

MSComm1.Settings ="9600,n,8,1"' baud, parity, data bits, stop bits

MSComm1.SThreshold = 1' allows us to track Tx LED

MSComm1.InputMode = comInputModeText'comInputModeBinary ' binary mode, you can also use

' comInputModeText for text only use

PortIsOpen = False

cmbPort.ListIndex = 0

' open the port

MSComm1.PortOpen = True

cmbPort.Enabled = False

PortIsOpen = True

cmdPort.Caption ="Close Port"

cmdStart.Enabled = True

cmdEnd.Enabled = True

Exit Sub

ErrorHandler1:

Debug.Print Err.Description

PortIsOpen = False

cmbPort.Enabled = True

cmdPort.Caption ="Open Port"

cmdStart.Enabled = False

cmdEnd.Enabled = False

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

If MSComm1.PortOpen Then MSComm1.PortOpen = False

End Sub

Private Sub lblOption_Click()

End Sub

Private Sub MSComm1_OnComm()

'******************************************************************************

' Synopsis:Handle incoming characters, 'On Comm' Event

'

' Description: By setting MSComm1.RThreshold = 1, this event will fire for

'each character that arrives in the comm controls input buffer.

'Set MSComm1.RThreshold = 0 if you want to poll the control

'yourself, either via a TImer or within program execution loop.

'

'In most cases, OnComm Event processing shown here is the prefered

'method of processing incoming characters.

'

'******************************************************************************

Dim i As Long

Dim sBuffAs String' buffer for holding incoming characters

Const MTCAs String = vbCrLf' message terminator characters (ususally vbCrLf)

Const LenMTCAs Long = 2' number of terminator characters, must match MTC

Dim iPtrAs Long' pointer to terminatior character

' OnComm fires for multiple Events

' so get the Event ID & process

Select Case MSComm1.CommEvent

' Received RThreshold # of chars, in our case 1.

Case comEvReceive

' read all of the characters from the input buffer

' StrConv() is required when using MSComm in binary mode,

' if you set MSComm1.InputMode = comInputModeText, it's not required

'sBuff = sBuff & StrConv(MSComm1.Input, vbUnicode)

'If Len(txtMessage.Text) > 4096 Then txtMessage.Text = ""

sBuff = MSComm1.Input

Dim ch As String

Dim PacketStart As Boolean

Dim PacketLength As Integer

Dim Packet() As String

PacketStart = False

PacketLength = 0

While (Len(sBuff) > 0)

ch = Left(sBuff, 1)

If (ch = Chr(&HAA)) Then PacketStart = True

If (ch = Chr(&HBB)) Then PacketStart = False

If (ch <> Chr(&HAA) And ch <> Chr(&HBB)) Then

PacketLength = PacketLength + 1

ReDim Preserve Packet(PacketLength)

Packet(PacketLength) = ch

End If

'txtMessage.Text = txtMessage.Text + Format(Hex(Asc(ch)), " @@")

sBuff = Right(sBuff, Len(sBuff) - 1)

Wend

If (PacketLength = 3) Then

Debug.Print"Command packet recieved"

'txtMessage.Text = txtMessage.Text + vbCrLf + "Address =" + Str(Asc(Packet(1)))

'txtMessage.Text = txtMessage.Text + vbCrLf + "Address =" + Str(Asc(Packet(2)))

End If

If (PacketLength = 4) Then

Debug.Print"Response packet recieved"

txtMessage.Text = txtMessage.Text +"Address =" + Str(Asc(Packet(1))) + _

" Answer = " + Answers((Asc(Packet(2)) And &HC0) / 64) + _

" Time =" + Str((Asc(Packet(2)) And &H3F) * 256 + (Asc(Packet(3)))) +"mS" + vbCrLf

'txtMessage.Text = txtMessage.Text + vbCrLf + "Address =" + Str(Asc(Packet(1)))

'txtMessage.Text = txtMessage.Text + vbCrLf + "Option =" + Str((Asc(Packet(2)) And &HC0) / 64)

'txtMessage.Text = txtMessage.Text + vbCrLf + "Time =" + Str((Asc(Packet(2)) And &H3F) * 256 + (Asc(Packet(3))))

End If

If (PacketLength <> 4 And PacketLength <> 3) Then Debug.Print"Unknown packet of length" + Str(PacketLength) +" recieved"

txtMessage.Text = txtMessage.Text + vbCrLf

' An EOF charater was found in the input stream

Case comEvEOF

DoEvents

' There are SThreshold number of characters in the transmit buffer.

Case comEvSend

DoEvents

' A Break was received.

Case comEventBreak

DoEvents

' Framing Error

Case comEventFrame

DoEvents

' Data Lost.

Case comEventOverrun

DoEvents

' Receive buffer overflow.

Case comEventRxOver

DoEvents

' Parity Error.

Case comEventRxParity

DoEvents

' Transmit buffer full.

Case comEventTxFull

' Unexpected error retrieving DCB]

Case comEventDCB

DoEvents

End Select

End Sub

Private Sub tmrRead_Timer()

'MSComm1.Output = vbCrLf + vbCrLf

'MSComm1.Output = Chr(128)

End Sub

Private Sub txtMessage_KeyDown(KeyCode As Integer, Shift As Integer)

If Shift = 2 Or Shift = 4 Then RejectKeystroke = False Else RejectKeystroke = True

End Sub

Private Sub txtMessage_KeyPress(KeyAscii As Integer)

If RejectKeystroke Then

KeyAscii = 0

End If

End Sub

Thanks in advance..

[12528 byte] By [G.S.V.N.PRAVEENa] at [2007-11-27 10:50:24]
# 1

What exactly is your problem?

CeciNEstPasUnProgrammeura at 2007-7-29 11:25:04 > top of Java-index,Java Essentials,Java Programming...
# 2

I want to replicate the entire VB program as Java Program.

This has to be included in my project which i am doing in java.

G.S.V.N.PRAVEENa at 2007-7-29 11:25:04 > top of Java-index,Java Essentials,Java Programming...