• Win32编程串口超时结构体的一般性设置


      Win32串口结构体有5个成员变量:读数据拼接超时、读数据总超时常数项、读数据总超时一次项、写数据总超时常数项、写数据总超时一次项。

      拼接超时即是指两个字节之间不大于这个毫秒数,底层即将这两个字节视为同一组报文。仅读数据有拼接超时。这个参数通常都是正值,不应设置为0或负数。

      总超时常数项是指调用函数开始时进行计时,如果没有收到新数据,则等待这个时间。

      总超时一次项是每接收一个字节,则在常数项的基础上增加一份时间。

      一般我们把写超时都设置为0,读数据总超时一次项也设置为0,只使用读数据拼接超时和读数据总超时常数项。

      这两个超时参数可以配置成简单数据拼接模式、全阻塞拼接模式、定时阻塞拼接模式。模式与是否设置了OVERLAPPED无关。

    非阻塞模式:

      有一个特殊的组合:-1,0,0,0,0,也就是数据拼接超时为-1,其它4个参数全为0。这是一个特殊的配置,效果是非阻塞接收。非阻塞模式下底层的数据拼接不起作用。在Win32的串口编程中会说到一个MAXDWORD,也就是0xffffffff,和-1是同一个值,不用纠结叫什么名字。这个模式适合新手学习使用,可以在一个线程中处理多个串口通信,适合那些不擅长多线程的开发者(多线程里面有很多坑,如果没把握的话还是宁可不要乱用)。当然,一个线程处理所有串口也可以节省内存,性能上不会差很多,就是CPU占用变高一点点。

    无限阻塞模式:

      无限阻塞模式的组合是:1,0,0,0,0,与非阻塞模式的区别在于数据拼接超时变成正数。这里用的是1,其实也可以换一个拼接超时的值,以增强数据拼接的功能。使用1是我个人的SDK中对通信工具代码的要求——要尽可能弱化底层超时的作用。这后面有一套复杂的机制来保证数据的完整性和响应性。如果不想搞那么麻烦,就用个大点的数字,牺牲掉一点响应性来保证数据的完整性也是可以的。这个模式适合开发从机线程。

    定时阻塞模式:

      有限阻塞模式的组合是:1,1000,0,0,0,这个组合是最大阻塞1000ms,这个根据需要来写,不要照抄,有数据时立即返回。读数据总超时常数项就是限制等待的最长时间。同样的,如果不想搞一大堆代码来同时保证数据完整性和响应性,还是把数据拼接超时设置一个大点的数字。这个模式适合开发主机线程。

    串口读超时设置试验

       以下按拼接超时、常数项、一次项的顺序命名配置参数。发送的方式为隔100ms发送一个'a',发送20个时延迟2000ms。

    1, 0, 0

      几乎没有延时,每传输一个字节就立即返回这个字节。没有收到传输的字节时就保持等待。命令行显示如下:

    send: a
    a
    send: a
    a

    0, 1, 0

      几乎没有阻塞,接收线程一直在循环,如果没有收到数据就返回null(实际上是Win32的ReadFile返回长度为0,在Java中我把它转换成了null),如果有收到数据就返回这个字节。命令行显示如下:

    null
    null
    send: a
    a
    null
    null

    0, 0, 1

      几乎不按套路出牌,接收线程无视数据发送的时机等待,突然返回一串东西。命令行显示如下:

    send: a
    send: a
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    send: a
    send: a

      这个数字会影响一次接收到的字符串长度,但规律很复杂。一次项的数字越大,接收的长度倾向于变得更长。字节发送越频繁,这个长度也倾向于变得更长。

    0, 1000, 1

      在上一节的基础上增加了一个1s的延迟。收到的东西多了一点,没什么结构性的变化。

    0, 1000, 0

      调用receive后固定一个时间后返回。等价于sleep一秒后调用无阻塞的receive。命令行显示如下:

    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    aaaaaaaaa
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    aaaaaaaaa
    send: a
    a
    null
    send: a
    send: a

    200, 0, 0

      接收到数据后200ms内无数据后返回。如果没有接收到数据,则等待。命令行显示如下:

    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    aaaaaaaaaaaaaaaaaaaa

    200, 1000, 0

      接收到数据后200ms内无数据返回,如果有数据但离函数调用的时间超过了1000ms也会返回。命令行显示如下:

    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    aaaaaaaaa
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    send: a
    aaaaaaaaaa
    null
    send: a
    a
    send: a

      从以上试验可以看出第一个参数控制的是对报文连续性的判断,第二个参数控制的是函数强制返回的时间,第三个参数则是每接收一个字节就按比例延长函数强制返回的时间。返回的逻辑可以写作:

    1. if(当前时间 - 最后一个字节接收的时间 > 数据拼接超时
    2. || 当前时间 - 第一个字节接收的时间 > 读数据总超时常数项 + 读数据总超时一次项 * 已接收的字节数 * 一个未知的系数)
    3. break;

      非阻塞有一个特殊的配置组合,只有一种模式,配置不是唯一的,但效果都一样,没有可以深入的地方。下文介绍的是阻塞模式下的一些配置方法。

      如果想要快速反应,则数据拼接超时就要尽可能短,也就是应为1(非阻塞配置下为-1,那个是特殊情况)。此时总超时实质上有很大的概率不起作用,多数报文都是破碎的,需要在应用层处理数据拼接。

      如果不需要快速反应,希望充分发挥系统底层的数据拼接能力,则数据拼接超时应在合理的范围选择尽可能大的值,读数据总超时常数项要够大,读数据总超时一次项可以取1,或不怎么大的数字。一次项对后期的影响很大,不要取太大的数值。

      可以给这三个参数换个叫法,依次为:引力常数、耐性常数、希望系数。拼接超时其实就像是碎片之间的引力大小,设置越高则碎片越容易聚合。读数据总超时常数项就是接收函数的初始耐心值,它越大则这个函数越有耐心可以在没有数据的情况下等下去。读数据总超时一次项就像是接收函数在成功后对后面还有更多数据的希望,就像炒股的人越涨越买一样,收到的数据越多,它越会继续等下去。

      引力是客观的,如果碎片吸不到一起,则必然破碎。耐性和希望是主观的,它的作用建立在碎片能够吸到一起,也就是客观存在决定主观意识。另外耐性和希望只是决定了数据的量涨到多少可以抛出,而引力决定了数据的断崖。比方说如果股价突然跳楼了,那么散户就有大概率会割肉,而如果数据碎片确定吸不到一起了,这个函数也会立即像散户割肉一样抛出已经持有的数据,不会再留有任何的耐心和希望。

    附上一段Win32 Java代码示范:

    1. @Override
    2. public SerialPortMessage receive(long blockTimeout) {
    3. if (!open()) {
    4. return null;
    5. }
    6. try {
    7. if (blockTimeout == 0) {
    8. // 非阻塞
    9. if (!setTimeout(m_handle,
    10. -1,
    11. 0,
    12. 0,
    13. 0,
    14. 0)) {
    15. throw new IOException("receive com" + m_comPort + " failed. last error at setTimeout: " + getLastErrorCode());
    16. }
    17. } else if (blockTimeout < 0) {
    18. // 无限阻塞
    19. if (!setTimeout(m_handle,
    20. 1,
    21. 0,
    22. 0,
    23. 0,
    24. 0)) {
    25. throw new IOException("receive com" + m_comPort + " failed. last error at setTimeout: " + getLastErrorCode());
    26. }
    27. } else {
    28. // 定时阻塞
    29. if (!setTimeout(m_handle,
    30. 1,
    31. blockTimeout,
    32. 0,
    33. 0,
    34. 0)) {
    35. throw new IOException("receive com" + m_comPort + " failed. last error at setTimeout: " + getLastErrorCode());
    36. }
    37. }
    38. SerialPortMessage ret = read();
    39. if (!setTimeout(m_handle, 1, 0, 0, 0, 0)) {
    40. throw new IOException("receive com" + m_comPort + " failed. last error at setTimeout: " + getLastErrorCode());
    41. }
    42. return ret;
    43. } catch (Throwable e) {
    44. getOnCatch().accept(e);
    45. close();
    46. }
    47. return null;
    48. }

    附上JNI代码:

    pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI.h

    1. /* DO NOT EDIT THIS FILE - it is machine generated */
    2. #include "JNIInclude/jni.h"
    3. /* Header for class pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI */
    4. #ifndef _Included_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    5. #define _Included_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    6. #ifdef __cplusplus
    7. extern "C" {
    8. #endif
    9. /*
    10. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    11. * Method: getLastError
    12. * Signature: ()J
    13. */
    14. JNIEXPORT jlong JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_getLastError
    15. (JNIEnv *, jclass);
    16. /*
    17. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    18. * Method: createFile
    19. * Signature: ([BZ)J
    20. */
    21. JNIEXPORT jlong JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_createFile
    22. (JNIEnv *, jclass, jbyteArray, jboolean);
    23. /*
    24. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    25. * Method: closeHandle
    26. * Signature: (J)Z
    27. */
    28. JNIEXPORT jboolean JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_closeHandle
    29. (JNIEnv *, jclass, jlong);
    30. /*
    31. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    32. * Method: setupComm
    33. * Signature: (JJJ)Z
    34. */
    35. JNIEXPORT jboolean JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_setupComm
    36. (JNIEnv *, jclass, jlong, jlong, jlong);
    37. /*
    38. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    39. * Method: setCommTimeouts
    40. * Signature: (JJJJJJ)Z
    41. */
    42. JNIEXPORT jboolean JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_setCommTimeouts
    43. (JNIEnv *, jclass, jlong, jlong, jlong, jlong, jlong, jlong);
    44. /*
    45. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    46. * Method: setCommState
    47. * Signature: (JJJJJ)Z
    48. */
    49. JNIEXPORT jboolean JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_setCommState
    50. (JNIEnv *, jclass, jlong, jlong, jlong, jlong, jlong);
    51. /*
    52. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    53. * Method: purgeComm
    54. * Signature: (J)Z
    55. */
    56. JNIEXPORT jboolean JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_purgeComm
    57. (JNIEnv *, jclass, jlong);
    58. /*
    59. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    60. * Method: readFile
    61. * Signature: (J[B)I
    62. */
    63. JNIEXPORT jint JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_readFile
    64. (JNIEnv *, jclass, jlong, jbyteArray);
    65. /*
    66. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    67. * Method: writeFile
    68. * Signature: (J[BI)I
    69. */
    70. JNIEXPORT jint JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_writeFile
    71. (JNIEnv *, jclass, jlong, jbyteArray, jint);
    72. #ifdef __cplusplus
    73. }
    74. #endif
    75. #endif

     pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI.cpp

    1. #include
    2. #include
    3. #include "pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI.h"
    4. #ifdef __cplusplus
    5. extern "C" {
    6. #endif
    7. /*
    8. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    9. * Method: getLastError
    10. * Signature: ()J
    11. */
    12. JNIEXPORT jlong JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_getLastError
    13. (JNIEnv*, jclass) {
    14. return (unsigned long long) GetLastError();
    15. }
    16. /*
    17. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    18. * Method: createFile
    19. * Signature: ([BZ)J
    20. */
    21. JNIEXPORT jlong JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_createFile
    22. (JNIEnv *jEnv, jclass jCls, jbyteArray jPortName, jboolean isOverlapped) {
    23. int len = jEnv->GetArrayLength(jPortName);
    24. char *buf = (char*) malloc(len);
    25. jEnv->GetByteArrayRegion(jPortName, 0, len, (jbyte*) buf);
    26. HANDLE handle = CreateFile(buf, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
    27. isOverlapped ? (FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED) : 0, NULL);
    28. free(buf);
    29. return (jlong) handle;
    30. }
    31. /*
    32. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    33. * Method: closeHandle
    34. * Signature: (J)Z
    35. */
    36. JNIEXPORT jboolean JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_closeHandle
    37. (JNIEnv *jEnv, jclass jCls, jlong handle) {
    38. return CloseHandle((HANDLE) handle);
    39. }
    40. /*
    41. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    42. * Method: setupComm
    43. * Signature: (JJJ)Z
    44. */
    45. JNIEXPORT jboolean JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_setupComm
    46. (JNIEnv *jEnv, jclass jCls, jlong handle, jlong inBuffer, jlong outBuffer) {
    47. return SetupComm((HANDLE) handle, inBuffer, outBuffer);
    48. }
    49. /*
    50. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    51. * Method: setCommTimeouts
    52. * Signature: (JJJJJJ)Z
    53. */
    54. JNIEXPORT jboolean JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_setCommTimeouts
    55. (JNIEnv *jEnv, jclass jCls, jlong handle, jlong ReadIntervalTimeout, jlong ReadTotalTimeoutConstant,
    56. jlong ReadTotalTimeoutMultiplier, jlong WriteTotalTimeoutConstant, jlong WriteTotalTimeoutMultiplier) {
    57. COMMTIMEOUTS timeouts;
    58. timeouts.ReadIntervalTimeout = ReadIntervalTimeout;
    59. timeouts.ReadTotalTimeoutConstant = ReadTotalTimeoutConstant;
    60. timeouts.ReadTotalTimeoutMultiplier = ReadTotalTimeoutMultiplier;
    61. timeouts.WriteTotalTimeoutConstant = WriteTotalTimeoutConstant;
    62. timeouts.WriteTotalTimeoutMultiplier = WriteTotalTimeoutMultiplier;
    63. return SetCommTimeouts((HANDLE) handle, &timeouts);
    64. }
    65. /*
    66. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    67. * Method: setCommState
    68. * Signature: (JJJJJ)Z
    69. */
    70. JNIEXPORT jboolean JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_setCommState
    71. (JNIEnv *jEnv, jclass jCls, jlong handle, jlong BaudRate, jlong ByteSize, jlong Parity, jlong StopBits) {
    72. DCB dcb;
    73. GetCommState((HANDLE) handle, &dcb);
    74. dcb.BaudRate = BaudRate;
    75. dcb.ByteSize = ByteSize;
    76. dcb.Parity = Parity;
    77. dcb.fBinary = TRUE;
    78. dcb.fParity = TRUE;
    79. dcb.StopBits = StopBits;
    80. return SetCommState((HANDLE) handle, &dcb);
    81. }
    82. /*
    83. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    84. * Method: purgeComm
    85. * Signature: (J)Z
    86. */
    87. JNIEXPORT jboolean JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_purgeComm
    88. (JNIEnv *jEnv, jclass jCls, jlong handle) {
    89. return PurgeComm((HANDLE) handle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
    90. }
    91. /*
    92. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    93. * Method: readFile
    94. * Signature: (J[B)I
    95. */
    96. JNIEXPORT jint JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_readFile
    97. (JNIEnv *jEnv, jclass jCls, jlong handle, jbyteArray jBuffer) {
    98. int bufLen = jEnv->GetArrayLength(jBuffer);
    99. char *buf = (char*) malloc(bufLen);
    100. //jEnv->GetByteArrayRegion(jBuffer, 0, bufLen, (jbyte*)buf);
    101. DWORD recvLen;
    102. DWORD dwErrorFlags;
    103. COMSTAT ComStat;
    104. OVERLAPPED ov;
    105. ClearCommError((HANDLE) handle, &dwErrorFlags, &ComStat);
    106. memset(&ov, 0, sizeof(ov));
    107. if (!ReadFile((HANDLE) handle, buf, bufLen, &recvLen, &ov)) {
    108. if (GetLastError() == ERROR_IO_PENDING) {
    109. GetOverlappedResult((HANDLE) handle, &ov, &recvLen, TRUE);
    110. jEnv->SetByteArrayRegion(jBuffer, 0, recvLen, (jbyte*) buf);
    111. } else {
    112. recvLen = -1;
    113. }
    114. } else {
    115. jEnv->SetByteArrayRegion(jBuffer, 0, recvLen, (jbyte*) buf);
    116. }
    117. free(buf);
    118. return recvLen;
    119. }
    120. /*
    121. * Class: pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_JNI
    122. * Method: writeFile
    123. * Signature: (J[BI)I
    124. */
    125. JNIEXPORT jint JNICALL Java_pers_laserpen_util_communication_serialPort_NativeSerialPortUtils_1JNI_writeFile
    126. (JNIEnv *jEnv, jclass jCls, jlong handle, jbyteArray jBuffer, jint length) {
    127. int bufLen = jEnv->GetArrayLength(jBuffer);
    128. char *buf = (char*) malloc(bufLen);
    129. jEnv->GetByteArrayRegion(jBuffer, 0, bufLen, (jbyte*) buf);
    130. DWORD sendLen;
    131. DWORD dwErrorFlags;
    132. COMSTAT ComStat;
    133. OVERLAPPED ov;
    134. ClearCommError((HANDLE) handle, &dwErrorFlags, &ComStat);
    135. memset(&ov, 0, sizeof(ov));
    136. if (!WriteFile((HANDLE) handle, buf, length, &sendLen, &ov)) {
    137. if (GetLastError() == ERROR_IO_PENDING) {
    138. GetOverlappedResult((HANDLE) handle, &ov, &sendLen, TRUE);
    139. } else {
    140. sendLen = -1;
    141. }
    142. }
    143. //jEnv->SetByteArrayRegion(jBuffer, 0, recvLen, (jbyte*)buf);
    144. free(buf);
    145. return sendLen;
    146. }
    147. #ifdef __cplusplus
    148. }
    149. #endif

    JNI的Java部分:

    1. package pers.laserpen.util.communication.serialPort;
    2. /**
    3. * javac -h 需要像编译class一样把所有引用的java文件都编译,为了防止引用java文件,把jni部分单独放在一个类中。
    4. *
    5. * @author Laserpen
    6. */
    7. abstract class NativeSerialPortUtils_JNI {
    8. native static final long getLastError();
    9. native static final long createFile(byte[] portName, boolean isOverlapped);
    10. native static final boolean closeHandle(long handle);
    11. native static final boolean setupComm(long handle, long inBuffer, long outBuffer);
    12. native static final boolean setCommTimeouts(long handle, long readIntervalTimeout, long readTotalTimeoutConstant,
    13. long readTotalTimeoutMultiplier, long writeTotalTimeoutConstant, long writeTotalTimeoutMultiplier);
    14. native static final boolean setCommState(long handle, long baudRate, long byteSize, long parity, long stopBits);
    15. native static final boolean purgeComm(long handle);
    16. native static final int readFile(long handle, byte[] buffer);
    17. native static final int writeFile(long handle, byte[] buffer, int length);
    18. }
    1. package pers.laserpen.util.communication.serialPort;
    2. import pers.laserpen.util.string.ENCODING;
    3. public abstract class NativeSerialPortUtils extends NativeSerialPortUtils_JNI {
    4. public static final long getLastErrorCode() {
    5. return getLastError();
    6. }
    7. public static final long openCom(int com, boolean isOverlapped) {
    8. String portStr = "\\\\.\\COM" + com + "\0";// C和C++无法识别byte[]的长度,需要添加一个0字符。
    9. return createFile(portStr.getBytes(ENCODING.US_ASCII()), isOverlapped);
    10. }
    11. public static final boolean closeCom(long handle) {
    12. return closeHandle(handle);
    13. }
    14. public static boolean setBuffer(long handle, int inBuffer, int outBuffer) {
    15. return setupComm(handle, inBuffer, outBuffer);
    16. }
    17. public static boolean setSimpleTimeout(long handle) {
    18. return setCommTimeouts(handle, -1, 0, 0, 0, 0);
    19. }
    20. public static boolean setTimeout(long handle, long readIntervalTimeout, long readTotalTimeoutConstant,
    21. long readTotalTimeoutMultiplier, long writeTotalTimeoutConstant, long writeTotalTimeoutMultiplier) {
    22. return setCommTimeouts(handle, readIntervalTimeout, readTotalTimeoutConstant, readTotalTimeoutMultiplier,
    23. writeTotalTimeoutConstant, writeTotalTimeoutMultiplier);
    24. }
    25. public static final boolean setState(long handle, long baudRate, ByteSize byteSize, Parity parity,
    26. StopBits stopBits) {
    27. return setCommState(handle, baudRate, byteSize.getFlag(), parity.getFlag(), stopBits.getFlag());
    28. }
    29. public static final boolean ready(long handle) {
    30. return purgeComm(handle);
    31. }
    32. public static final int read(long handle, byte[] buffer) {
    33. return readFile(handle, buffer);
    34. }
    35. public static final int write(long handle, byte[] buffer, int length) {
    36. return writeFile(handle, buffer, length);
    37. }
    38. }

    OVERLAPPED有什么作用?

      对我而言,OVERLAPPED已经没有作用了。上层封装使这个标志存在与否都表现出一样的特性。不过如果没有上层封装,OVERLAPPED可以把阻塞的通信变成非阻塞的流程。关键在于ReadFile。如果设置成非阻塞,ReadFile会返回当时得到的所有数据,这有可能是一些碎片。而使用OVERLAPPED设置超时结构体为阻塞,则ReadFile仍然是非阻塞的。使用ReadFile可以得到当前的状态,而不是当前的缓存字节。那么在合适的状态下取出缓存字节,就更有可能得到一个整体。

      我仍然建议做一个上层封装。因为即使能够一定程度上保证数据完整性,并使ReadFile非阻塞,OVERLAPPED仍然存在尾部超时,尾部超时是Win32 API层面上无法解决的。

    为什么使用malloc和free?

      JNI代码用于Win32 API的基本操作,它只需要编写一次,因此即使出现内存溢出,故障也是有限的,总能把故障清理干净。而它的性能会影响所有用到它的应用。使用malloc和free可以达到尽可能高的性能。

  • 相关阅读:
    含文档+PPT+源码等]精品微信小程序springboot在线考试系统小程序+后台管理系统|前后分离VUE[包运行成功]程序设计源码计算机毕设
    什么是长轮询
    git push超过100MB大文件失败(remote: fatal: pack exceeds maximum allowed size)
    安卓实训作孽之Linux命令手册
    交换机堆叠与集群
    解决报错:模块“react-redux“没有导出的成员“TypedUseSelectorHook”
    Hbuilderx:Vue项目打包apk打开首页空白问题
    WEB安全问题
    【java学习—八】单例设计模式(5)
    力扣刷题day33|509斐波那契数、70爬楼梯、746使用最小花费爬楼梯
  • 原文地址:https://blog.csdn.net/rediculous/article/details/128196818