Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help reading serial data on Arduino

Status
Not open for further replies.
Hi
one more problem need help from you guys

no more scale to test on So would you please help to make a small arduino sketch to emulate the scale data ?

i need to write N + 1 2 3 . 4 5 6 g

N = 0x4E
+ = 0x2B
1 = 0x31
2 = 0x32
3 = 0x33
. = 0x2E
4 = 0x34
5 = 0x35
6 = 0x36
g = 0x67

can i simply use this ?

Serial.begin(1200,SERIAL_7N1); //this is the settings i have used on Terminal Program to read the value

Serial.write(0x4E);
Serial.write(0x2B);
Serial.write(0x31);
Serial.write(0x32);
Serial.write(0x33);
Serial.write(0x2E);
Serial.write(0x34);
Serial.write(0x35);
Serial.write(0x36);
Serial.write(0x67);


or i should add delays or what ?



best regards
Johnny
 
Last edited:

That should work fine.
Delays are not needed, when you use the Serial.write() instruction all it does is pass the value to the UART transmit register. The UART then adds the start, parity and stop bit and sends it one bit at a time out of the TX pin. The UART has a status bit that tells the program whether it is still sending the previous data or if it is free to accept some more. The Serial.write() checks that bit and makes sure it is safe to feed it the next character to be sent.

The only thing you might have to do is add another character or two to the end. When you used the scales, your terminal probably showed something like this:
N+123.456g
N+654.321g
N+001.234g
with each result on it's own line rather than N+123.456gN+654.321gN+001.234g with no gaps between readings.
The reason would be that there is probably a carriage return and line feed character after the characters you can see. They are not visible in a terminal program because all they do is move the cursor down to the beginning of the following line. If you need them, just add two more lines to your commands:
Serial.write(0x0A);
Serial.write(0x0D);
and that should fix it.

Brian.
 
That should work fine.
Delays are not needed, when you use the Serial.write() instruction all it does is pass the value to the UART transmit register. The UART then adds the start, parity and stop bit and sends it one bit at a time out of the TX pin. The UART has a status bit that tells the program whether it is still sending the previous data or if it is free to accept some more. The Serial.write() checks that bit and makes sure it is safe to feed it the next character to be sent.

The only thing you might have to do is add another character or two to the end. When you used the scales, your terminal probably showed something like this:
N+123.456g
N+654.321g
N+001.234g
with each result on it's own line rather than N+123.456gN+654.321gN+001.234g with no gaps between readings.
The reason would be that there is probably a carriage return and line feed character after the characters you can see. They are not visible in a terminal program because all they do is move the cursor down to the beginning of the following line. If you need them, just add two more lines to your commands:
Serial.write(0x0A);
Serial.write(0x0D);
and that should fix it.

Brian.

it works Thanks Brian
 

Here is the Code to emulate the scale Data sent

Code:
void setup() {
  Serial.begin(1200, SERIAL_7N1); // opens serial port, sets data rate to 1200bps 7Bits no Parity 1 Stop Bit 
}
void loop() {

  Serial.write(0x4E);//N
  Serial.write(0x20);//Space
  Serial.write(0x20);//Space
  Serial.write(0x20);//Space
  Serial.write(0x20);//Space
  Serial.write(0x20);//Space
  Serial.write(0x2B);//+ sign
  Serial.write(0x20);//Space
  Serial.write(0x20);//Space
  Serial.write(0x31);//1
  Serial.write(0x32);//2
  Serial.write(0x33);//3
  Serial.write(0x2E);//.
  Serial.write(0x34);//4
  Serial.write(0x35);//5
  Serial.write(0x36);//6
  Serial.write(0x20);//Space
  Serial.write(0x67);//g
  Serial.write(0x20);//Space
  Serial.write(0x20);//Space
  Serial.write(0x0A);//LF
  Serial.write(0x0D);//CR
}

here i send N - - - - - + - - 1 2 3 . 4 5 6 - g - - LF CR (- = Space)

I want something like trim & convert this characters to value so i can compare it with saved values & display it only on LCD (+ 123.456 g)

thanks
Johnny
 

ASCII means "American Standard Code for Information Interchange" so it is used for almost all text applications, including LCD displays. Passing the characters directly to your LCD display routine should work.

Doing it one character at a time is inefficient and possibly too slow, your best strategy is to capture the characters into an array before displaying them. I would have to go back and read the Arduino programming reference but I remember there being a command something like "readSerialUntil()" which should let you read all the characters until the LF/CR is found. Once you have the characters stored, you can position the cursor on the LCD and use the normal LCD writing routine to display the text.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top