Kawarimi
Posts: 4
Joined: Tue Sep 18, 2018 1:05 pm

RPI3 RxTx Connection with a weight machine [HELP!]

Tue Sep 18, 2018 1:46 pm

Hello,

I am actually on a project, and to start I have to send a "t" in ASCII to the weight machine from my Raspberry.

Actually I wired my raspberry using RxTx with RS232 linked to the weight machine but now I don't really know what to do on my raspberry, I followed some tutorials to use the ports.

I understood that I have to use the /dev/ttyAMA0 because it is the RxTx port but there is also ppl saying that it is the port for bluetooth if anyone can clarify me this please.

Let me explain you all I did until now on my Raspberry :

I've gone in : sudo nano /boot/config.txt and added : dtoverlay=pi3-disable-bt

and then i've gone in : sudo nano /boot/cmdline.txt and changed the file like this :

dwc_otg.lpm_enable=0 console=tty1 console=ttyAMA0,9600 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

And then I used Java to use the serial connection, for this I downloaded the JAVA IDE : Eclipse (sudo apt-get install eclipse) and also the jdk (sudo apt-get install oracle-java8-jdk)

To use RxTx on Java I downloaded the library rxtx on java (sudo apt-get install librxtx-java)

My WeightMachine(Kern & Sohn FCB Version 1.1) has a baud rate of 9600 bauds, 8bit rate , 1 stop bit and no parity bit

There is the code I use :

Code: Select all

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TwoWaySerialComm {

  void connect( String portName ) throws Exception {
    CommPortIdentifier portIdentifier = CommPortIdentifier
        .getPortIdentifier( portName );
    if( portIdentifier.isCurrentlyOwned() ) {
      System.out.println( "Error: Port is currently in use" );
    } else {
      int timeout = 2000;
      CommPort commPort = portIdentifier.open( this.getClass().getName(), timeout );

      if( commPort instanceof SerialPort ) {
        SerialPort serialPort = ( SerialPort )commPort;
        serialPort.setSerialPortParams( 9600,
                                        SerialPort.DATABITS_8,
                                        SerialPort.STOPBITS_1,
                                        SerialPort.PARITY_NONE );

        InputStream in = serialPort.getInputStream();
        OutputStream out = serialPort.getOutputStream();

        ( new Thread( new SerialReader( in ) ) ).start();
        ( new Thread( new SerialWriter( out ) ) ).start();

      } else {
        System.out.println( "Error: Only serial ports are handled by this example." );
      }
    }
  }

  public static class SerialReader implements Runnable {

    InputStream in;

    public SerialReader( InputStream in ) {
      this.in = in;
    }

    public void run() {
      byte[] buffer = new byte[ 1024 ];
      int len = -1;
      try {
        while( ( len = this.in.read( buffer ) ) > -1 ) {
          System.out.print( new String( buffer, 0, len ) );
        }
      } catch( IOException e ) {
        e.printStackTrace();
      }
    }
  }

  public static class SerialWriter implements Runnable {

    OutputStream out;

    public SerialWriter( OutputStream out ) {
      this.out = out;
    }

    public void run() {
      try {
        int c = 0;
        while( ( c = System.in.read() ) > -1 ) {
          this.out.write( c );
        }
      } catch( IOException e ) {
        e.printStackTrace();
      }
    }
  }

  public static void main( String[] args ) {
    try {
      ( new TwoWaySerialComm() ).connect( "/dev/ttyAMA0" );
    } catch( Exception e ) {
      e.printStackTrace();
    }
  }
}
Code Source : https://eclipsesource.com/blogs/2012/10 ... -and-rxtx/

And I added the RXTXcomm.jar in the buildpath as a library in my Eclipse.

There is the errors I get when i compile it on eclipse :

gnu.io.NoSuchPortException
at gnu.io.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:269)
at TwoWaySerialComm.connect(TwoWaySerialComm.java:15)
at TwoWaySerialComm.main(TwoWaySerialComm.java:79)

For the first error it redirects me on an attachment source that failed but I give the good path for my RXTXcomm.jar and I have read in a tutorial that it can give me this error if the port doesn't exist.

For the second error it redirects me on the code line : CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);

For the third error it redirect me on the code line : (new TwoWaySerialComm() ).connect("/dev/ttyAMA0");

So I think I just missed some steps forgot to do some things in different folders/files, if anyone can help me about this and guide me it would be very kind and cool, you can ask me any questions I'll answer as fast as I can.

Thanks for your attention!

dgordon42
Posts: 764
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: RPI3 RxTx Connection with a weight machine [HELP!]

Wed Sep 19, 2018 3:45 pm

Lots of questions here, I'll have a go at some of them.
Kawarimi wrote:Actually I wired my raspberry using RxTx with RS232 linked to the weight machine but now I don't really know what to do on my raspberry, I followed some tutorials to use the ports.
First of all you must be aware that the RaspberryPi's Serial Port pins work at 3.3 Volts. Putting more than about 3.5V on any of these pins, even momentarily, can destroy a Pi. You state that you are connecting your Pi to a Kern & Sohn FCB Version 1.1 weighing scales. A quick google search returns a Manual, and a quick look at the circuit diagrams appear to show 5 Volt serial port signals being used. Maybe I have the wrong manual, can you check that the Serial Port on your scales works at 3.3V? If not you will need some sort of level converter between the Pi and the scales.
Kawarimi wrote:I understood that I have to use the /dev/ttyAMA0 because it is the RxTx port but there is also ppl saying that it is the port for bluetooth if anyone can clarify me this please.
This is no longer the case, things changed in the months after the launch of the RaspberryPi 3 . Both Bluetooth and the Serial Port will work together on all models of Pi running an up to date Raspbian OS. (Up to date in this case means later than about May 2017 from memory, but I could be wrong)

You posted the contents of your '/boot/cmdline.txt' file and it contained the phrase "console=ttyAMA0,9600". This phrase is no longer used in this file so I believe you are running a very old OS on your Pi, probably the original OS it came with. Could you report the output of these commands:

Code: Select all

uname -a
cat /etc/os-release
It's best to be running an up to date OS, there have been many improvements made since the launch of the Pi 3B.

After you get your OS up to date, there are 2 ways to get the Serial Port working.
1. Use the 'Raspi-config' Utility from the command line, Select "5, Interfacing Options", then "P6 Serial", and answer "No" to "Would you like a login shell to be accessible over serial", and "Yes" to "Would you like Serial Hardware to be enabled"

Code: Select all

sudo raspi-config
2. If you use the GUI, Select Raspi-config, I think it's in the Preference Menu and work through the options.
You will need a reboot to complete the changes.

If you wish, you can setup the Serial Port by adding the line "enable_uart=1" to '/boot/config.txt', and removing the phrase "console=serial0" from '/boot/cmdline.txt' and rebooting, that's what the above procedures do without the risk of errors.

After you are all up to date, refer to the Pi's Serial Port in code as "/dev/serial0", that's SerialZero, not SerialOh. I don't speak Java, but I guess changing all instances of "/dev/ttyAMA0" to "/dev/serial0" should get it working.

If for some reason you cannot update your Pi's OS, try changing all instances of "/dev/ttyAMA0" to "/dev/ttyS0" in your Java code, and see what happens.

Make sure you sort out the voltage levels on your scales serial port first, remember 5Volts can destroy a Pi.

A final option available is to buy a Serial to USB converter. These devices only cost a few Euro's. You plug the USB end into the Pi and the serial port end into your scales. Normally they take care of the different voltage levels. Your Pi's Serial Port then becomes "/dev/ttyUSB0", and you don't have to change any configuration files or make any other changes to get things to work. They will probably work with old OS's as well.

Hope this helps,
Dave.

Return to “General discussion”