I have two questions regarding serial communication between my arduino Uno and a FX-120i balance:
When ever I read serial data from the balance the first line is always rubbish. After I press the print button the second time, my data is correct. Why can this be?
Please display your image(s) in your post so we can see it(them) without downloading it(them). See this Simple Image Upload Guide
If the images are just text from your PC then please just copy and paste the text rather than posting an image of it.
A possible reason for the initial garbage is that the Arduino will have been running and producing output before the Serial Monitor started and that output will have been collected in the PC's serial buffer.
If you want advice about controlling the balance please post a link to its user manual or datasheet.
The first thing that strikes me is that the balance communicates at RS232 signal levels which are not compatible with the Arduino's TTL signal levels. The RS232 signals might damage an Arduino. You need a MAX232 (or similar) to convert between the TTL and RS232 signal levels.
One thing I notice is that I don't think you're handling is the handshaking in the serial protocol.
The datasheet is not terribly clear on serial messages other than commands, I think that after you send "Q" the scale first sends back an ACK (<06>) and then a second message follows with the scale data.
I wouldn't use String objects for this. Consider instead using a char arrays to send and receive data and implementing a state machine to implement the protocol.
I don't see anyplace in your code where you reverse the bits in each character. The documentation shows all characters are sent LSB first. So, you need to reverse the bits before you do anything with the message characters, even look for the CR/LF at the end.
Paul
PS. That applies to the messages you send, as well.
I don't see how I can be any more clear. The bytes sent from the scale have 8 bits per character sent. You assume the bytes are normal ASCII characters or binary numbers sent as bits 7,6,5,4,3,2,1,0. That is what would be sent and received by your Arduino.
The scale sends the bytes as bits 0,1,2,3,4,5,6,7.
Whatever the scale was attached to was programmed to accept serial bits that way and automatically set them the normal way. Perhaps it was an attempt to secure the data.
So you need code to reverse the order of the received bits before processing them as ASCII characters or as numeric values.
The older UART/USART serial chips had a programming bit that allowed data to be serialized output either way. The old IBM bisync could use ASCII and it was sent/received that way.
thanks for the feedback. I get what you say but what about the fact that if I run the code below, and press the print button on my scale I get the correct output, after some rubbish on the first print, on my serial monitor as shown in the picture below?