writing to serial port (COMM)

A

Andreas

Greetings,

I am just starting to use the COMM package to communicate with a serial
device.
I have found all ports and opened the correct one without problem.
According to the serial device's manual I am supposed to "send AT plus a CR
($41, $54, $0D) and await the same answer". The problem is that I am not
sure how to transfer "AT plus a CR".
I have seen in a few examples (in this group's archives) how you can write
to a serial port, and I am doing the following:

CommPortIdentifier portId =
CommPortIdentifier.getPortIdentifier("COM2");
SerialPort serialPort = (SerialPort)portId.open( "MySerialApp", 2000 );
serialPort.getOutputStream().write("AT <CR>".getBytes());

I am guessing this is not how it should be done (the "AT <CR>" I mean).
After you all have recovered from your laughing attacks, maybe someone
could help me understand this basic stuff?
It would be greatly appreciated.

Best,
Andreas
 
K

Knute Johnson

Andreas said:
Greetings,

I am just starting to use the COMM package to communicate with a serial
device.
I have found all ports and opened the correct one without problem.
According to the serial device's manual I am supposed to "send AT plus a CR
($41, $54, $0D) and await the same answer". The problem is that I am not
sure how to transfer "AT plus a CR".
I have seen in a few examples (in this group's archives) how you can write
to a serial port, and I am doing the following:

CommPortIdentifier portId =
CommPortIdentifier.getPortIdentifier("COM2");
SerialPort serialPort = (SerialPort)portId.open( "MySerialApp", 2000 );
serialPort.getOutputStream().write("AT <CR>".getBytes());

I am guessing this is not how it should be done (the "AT <CR>" I mean).
After you all have recovered from your laughing attacks, maybe someone
could help me understand this basic stuff?
It would be greatly appreciated.

Best,
Andreas

"AT\r".getBytes() or byte[] buf = { 0x41, 0x54, 0x13 };

CR is a nmenonic for carriage return.
 
A

Andreas

Knute said:
"AT\r".getBytes() or byte[] buf = { 0x41, 0x54, 0x13 };

CR is a nmenonic for carriage return.

Thank you Knute.
It actually struck me when I looked up the ASCII-codes for the hex-numbers.
(duh..)

Best,
Andreas
 
T

Thomas Weidenfeller

Andreas said:
Thank you Knute.
It actually struck me when I looked up the ASCII-codes for the hex-numbers.
(duh..)

But some of the hex codes for ASCII are easy to remember:

A few easy to remember characters are:

0x00: NUL
0x20: space
0x30: '0'
0x40: '@'
0x7f: DEL

Knowing these then help for other characters:

Digits 0-9:

0x30 is '0', 0x39 is '9'. digit + 0x30 = ASCII code of digit.

Uppercase letters:

0x40 is '@', after that the uppercase letters start, and are easy to
count. 0x41 = 'A', 0x42 = 'B' ... Position of letter in alphabet + 0x40
= uppercase letter's ASCII code.

Lowercase letters:

+ 0x20 offset from uppercase letters. Easy to count.


Some things are even easier to remember in octal. 0100 is '@'.

/Thomas
 
Joined
Nov 16, 2010
Messages
2
Reaction score
0
Hi guys, i know this is old and all but i'm trying to read input from a weight scale and output it to a gui i've created. I'm very new to java and i'm using this package (javax.comm)for the first time. pls help with any code that you might have that reads from a serial port. My scale connects to the pc using a usb-serial converter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top