Está en la página 1de 7

1.

How to use GiovynetSerialPort


1. How to setting the port?
2. How to send data?
1. sendArrayChar method.
2. sendSingleData method (overloaded).
3. sendString method.
3. How to receive data?
1. receiveSingleChar method.
2. receiveSingleDataInt method.
3. receiveSingleString method.
4. How to send control characters?
5. How to receive control characters?

How to use GiovynetSerialPort

The following document presents the most important


methods for working with GiovynetSerialPort 1.1, 1.2 and
1.3 versions.

How to setting the port?

The first thing you should do to start working with GivovynetSerialPort is to create
an object of class "Parameters" of the package "app." This class has set the
following default parameters:

port = COM1
baudRate = 9600
byteSize = 8
stopBits = 1
parity = N (no)

Then create an object "Com" of the package "app." This object require as
parameter the object of type "Parameters" created earlier, then it uses the
methods of the "Com" to send or receive data, as required.
If you need to change the default settings, use the methods setPort, setBaudRate,
setByteSize, setStopBits and setParity, to more clearly see the following
examples:

Example 1: Open port COM1 with baud rate 9600, byte size 8, 1 stop bits and no
parity.

public static void main(String[] args)throws


Exception{

Parameters param = new Parameters();


Com com1 = new Com(param);
com1.sendSingleData('A'); //send character
A.
}
Example 1: Open port COM2 with baud rate 57600, byte size 8, 1 stop bits and
even parity.
public static void main(String[] args)throws
Exception{

Parameters param = new Parameters();


param.setPort("COM2");
param.setBaudeRate("57600");
param.setParity("E");
Com com1 = new Com(param);
com1.sendSingleData(7); //send integer 7.

How to send data?

sendArrayChar method.

This method is used to send the elements from array chart


type. Each element is sent with interval time specified in
the second parameter in milliseconds.
by example, somebody wants to send the stored
characters in an array, every 500 milliseconds, therefore...

public static void main(String[] args)throws


Exception{

Parameters param = new Parameters();


param.setPort("COM1");
param.setBaudRate("9600");
Com com1 = new Com(param);

char[] data = {'1','A','2','B'};


com1.sendArrayChar(data, 500);

}
Will be send '1' after 500 milliseconds will be send '2', this
way until to send 'B'.

sendSingleData method (overloaded).

This method is used to send one char data or one int data
or one String data or Hex data.
By example, somebody wants to send: 'a' (char type),
then 41 (int type), then "S" (String type), then 0xff (Hex
type), therefore...

public static void main(String[] args)throws


Exception{

Parameters param = new Parameters();


param.setPort("COM1");
param.setBaudRate("9600");
Com com1 = new Com(param);

com1.sendSingleData('a');
com1.sendSingleData(41);
com1.sendSingleData("S");
com1.sendSingleData(0xff);

sendString method.

This method is used to send every symbol (one by one) of


string data composed, by intervals time. By example,
somebody wants to send one by one the symbols of the
string "hello", at intervals 800 milliseconds, therefore...

public static void main(String[] args)throws


Exception{

Parameters param = new Parameters();


param.setPort("COM1");
param.setBaudRate("9600");
Com com1 = new Com(param);
com1.sendString("hello",800);

Will be send "h" after 800 milliseconds wil be send "e",


this way until completing "hello".

How to receive data?

receiveSingleChar method.

This method is used to read one date like a char type. It is


not recommended to read control characters like <ACK>,
<NACK>,<LF>,...etc.
By example, somebody wants to read the data and show it
by console.

public static void main(String[] args)throws


Exception{

Parameters param = new Parameters();


param.setPort("COM1");
param.setBaudRate("9600");
Com com1 = new Com(param);
char data;
while(true){
data=com1.receiveSingleChar();
System.out.println(data);
}

receiveSingleDataInt method.

This method is used to read one date like a integer type. It


is recommended to receive control characters like <ACK>,
<NACK>,<LF>,...etc.
By example, somebody wants to get data as integer form
and show it by console.

public static void main(String[] args)throws


Exception{

Parameters param = new Parameters();


param.setPort("COM1");
param.setBaudRate("9600");
Com com1 = new Com(param);
int data;
while(true){
data=com1.receiveSingleDataInt();
System.out.println(data);
}

receiveSingleString method.

This method is used to read one or more data, like a type


String.
By example, somebody wants to read one data and stored
in String var, and show this by console.

public static void main(String[] args)throws


Exception{

Parameters param = new Parameters();


param.setPort("COM1");
param.setBaudRate("9600");
Com com1 = new Com(param);
String data;
data=com1.receiveSingleString();
System.out.println(data);
}

How to send control characters?


To send control characters are recommended to send their
decimal or hexadecimal representation. The method used
is sendSingleData, for example someone wants to send an
ACK (acknowledge), then:

public static void main(String[] args)throws


Exception{

Parameters param = new Parameters();


param.setPort("COM1");
param.setBaudRate("9600");
Com com1 = new Com(param);
int ack=6; //The integer representation of ACK
in ASCII code is 6.
data=com1.sendSingleData(ack);
}
}

How to receive control characters?

To receive control characters are recommended to receive


their in decimal or hexadecimal representation. The
method used is receiveSingleDataInt, for example
somebody wants to receive an NAK (negative
acknowledge), and show in console, then:

public static void main(String[] args)throws


Exception{

Parameters param = new Parameters();


param.setPort("COM1");
param.setBaudRate("9600");
Com com1 = new Com(param);
int nck=15; //The integer representation of
NCK in ASCII code is 15.
int data=0;
while(true){
data=com1.receiveSingleDataInt();
if (data==nck){
System.out.println(data);
}
}
}

También podría gustarte