简体   繁体   中英

jni serial port issues

I am using jni to slap a gui over some serial port code written in c++ (which is a descendent of the RS232 class). the original c++ works fine and continuously outputs data packets to the terminal. i wrote some really straight forward jni code as a starting point for my application. specifically, i made a boolean method on the c++ side of jni which simply calls the class constructor and returns whether the connection was successful. it looks like this:

    const string &port_name = "COM7";
const long baud_rate = 19200;
const char parity = 'N';
const int word_size = 8;
const int stop_bits = 1;
const int xon_xoff = 0;
const int rts_cts = 0;
const int dtr_dsr = 0;    

    SerialPort *m_pPort = new SerialPort( port_name,
                            baud_rate,
                                    parity,
                    word_size, 
                stop_bits,
                UNCHANGED,
                UNCHANGED,
                xon_xoff,
                rts_cts,
                dtr_dsr );

if ( m_pPort->ErrorStatus() == RS232_SUCCESS ) {
    int check_state = BST_INDETERMINATE;
    switch ( m_pPort->Dtr() ) {
        case 0 : check_state = BST_UNCHECKED; break;
        case 1 : check_state = BST_CHECKED; break;
    }
    check_state = BST_INDETERMINATE;
    switch ( m_pPort->Rts() ) {
        case 0 : check_state = BST_UNCHECKED; break;
        case 1 : check_state = BST_CHECKED; break;
    }
    return true;
} else {
    return false;
}

this code was essentially copied from main() of the original c++ program. however, the connection always fails when called from java (but always works when called from c++ main()). while debugging, i traced back the error thrown from the ancestor RS232 class to "RS232_NEXT_FREE_ERROR" which apparently never occurs under normal circumstances (according to my google searches). i am currently using vc++ express to generate the dll. previously, i had established a connection generating the .dll using wxdev, but abandoned it due to numerous other issues.

i know this is probably some strange edge case, but if anyone can shed some light on what is going on here, i would really appreciate it.

Strong suggestion: take a look at Rxtx

Here are some links:

http://rxtx.qbang.org/wiki/index.php/FAQ

http://en.wikibooks.org/wiki/Serial_Programming/Serial_Java

Also: here's a (somewhat old!) link on JavaComm, if you wish:

http://edn.embarcadero.com/article/31915

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM