Note that there are some explanatory texts on larger screens.

plurals
  1. POOSX Serial read freeze / hang
    primarykey
    data
    text
    <p>I'm writing a serial communication wrapper class in Objective-C. To list all serial available modems and setup the connection I'm using pretty much the same code as used in <a href="https://developer.apple.com/library/mac/documentation/DeviceDrivers/Conceptual/WorkingWSerial/WWSerial_SerialDevs/SerialDevices.html#//apple_ref/doc/uid/TP30000384-TPXREF119" rel="nofollow">this example project by Apple</a>.</p> <p>I could read and write the ways apple does it. But I want to implement a loop on a second thread and write to the stream if a <code>NSString *writeString</code> longer 0 and read after write if bytes are available.</p> <p>I got writing working quite straight forward. I just used the <code>write</code> function declared in <code>unistd.h</code>.</p> <p>Reading will not work. Whenever I call <code>read()</code>, the function hangs and my loop does not proceed.</p> <p>Here is the code used in my loop:</p> <pre><code>- (void)runInCOMLoop { do { // write } while (bytesWritten &lt; strlen([_writeString UTF8String])); NSMutableString *readString = [NSMutableString string]; ssize_t bytesRead = 0; ssize_t readB = 0; char buffer[256]; do { readB = read(_fileDescriptor, &amp;buffer, sizeof(buffer)); // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this function hangs bytesRead += readB; if (readB == -1 { // error } else if (readB &gt; 0) { if(buffer[bytesRead - 1] == '\r' ]] buffer[bytesRead - 1] == '\n') { break; } [readString appendString:[NSString stringWithUTF8String:buffer]]; } } while (readB &gt; 0); </code></pre> <p>What am I doing wrong here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload