Skip to content

WriteBytes does not return how many bytes were not written #129

Open

Description

@danielgruber

Hello,
At the moment we encountered that writeBytes does not return how many bytes were written. For a CTS / RTS flow, this is absolutly necessary. Is there a reason why this is not needed / or how we use this library correctly with hardware CTS / RTS without losing data in the middle?

Thanks a lot!

Best

Daniel

Activity

  1. tresf commented on Sep 21, 2022

    @tresf

    Hi,

    I don't believe this is possible currently. To support this without breaking backwards compatibility, a new function (e.g. write(bytes[])) would have to be written and the old boolean functions would have to be update to check the return length.

    This would require Windows and Unix C++ code to be updated to return jint and the current functions would be wrapped to still return a boolean.

    Disclaimer, this library is mostly in maintenance mode, so any features would have to be authored by the vested/interested parties.

    Relevant functions:

    Unix:

    JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_writeBytes
    (JNIEnv *env, jobject, jlong portHandle, jbyteArray buffer){
    jbyte* jBuffer = env->GetByteArrayElements(buffer, JNI_FALSE);
    jint bufferSize = env->GetArrayLength(buffer);
    jint result = write(portHandle, jBuffer, (size_t)bufferSize);
    env->ReleaseByteArrayElements(buffer, jBuffer, 0);
    return result == bufferSize ? JNI_TRUE : JNI_FALSE;
    }

    Windows:

    JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_writeBytes
    (JNIEnv *env, jobject, jlong portHandle, jbyteArray buffer){
    HANDLE hComm = (HANDLE)portHandle;
    DWORD lpNumberOfBytesTransferred;
    DWORD lpNumberOfBytesWritten;
    OVERLAPPED *overlapped = new OVERLAPPED();
    jboolean returnValue = JNI_FALSE;
    jbyte* jBuffer = env->GetByteArrayElements(buffer, JNI_FALSE);
    overlapped->hEvent = CreateEventA(NULL, true, false, NULL);
    if(WriteFile(hComm, jBuffer, (DWORD)env->GetArrayLength(buffer), &lpNumberOfBytesWritten, overlapped)){
    returnValue = JNI_TRUE;
    }
    else if(GetLastError() == ERROR_IO_PENDING){
    if(WaitForSingleObject(overlapped->hEvent, INFINITE) == WAIT_OBJECT_0){
    if(GetOverlappedResult(hComm, overlapped, &lpNumberOfBytesTransferred, false)){
    returnValue = JNI_TRUE;
    }
    }
    }
    env->ReleaseByteArrayElements(buffer, jBuffer, 0);
    CloseHandle(overlapped->hEvent);
    delete overlapped;
    return returnValue;
    }

  2. added
    help wantedExtra attention is needed
    enhancementNew feature or request
    language:cxxNeeds changes to c/cpp sources.
    language:javaNeeds changes to java sources.
    on Sep 21, 2022
  3. danielgruber commented on Sep 21, 2022

    @danielgruber
    Author

    Hi @tresf,

    Thanks for your update! I’m happy to implement the changes, still I would first want to know if there was a reason behind it not to implement it like this from the beginning?
    So is this at all possible with the C stack being used? Or do I have to try?
    Thanks!
    Best
    Daniel

  4. tresf commented on Sep 21, 2022

    @tresf

    I would first want to know if there was a reason behind it not to implement it like this from the beginning?

    Hi, this project is a fork of the original from https://github.com/scream3r/java-simple-serial-connector, so the history behind this decision likely lies there.

    So is this at all possible with the C stack being used? Or do I have to try?

    I think the Unix code makes it relatively easy, but I'm not sure about the win32 API. Perhaps the answer lies in GetOverlappedResult returning a zero? I'm not very familiar with hardware flow control and only partially familiar with the C++ portions of the codebase. We do have some other volunteers here as well as a Slack channel if interested. @hiddenalpha has been very helpful with the C++ portions as well.

  5. hiddenalpha commented on Sep 22, 2022

    @hiddenalpha

    Looks related to issue #96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is neededlanguage:cxxNeeds changes to c/cpp sources.language:javaNeeds changes to java sources.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions