{"action":"created","issue":{"url":"https://api.github.com/repos/Fazecast/jSerialComm/issues/7","labels_url":"https://api.github.com/repos/Fazecast/jSerialComm/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/Fazecast/jSerialComm/issues/7/comments","events_url":"https://api.github.com/repos/Fazecast/jSerialComm/issues/7/events","html_url":"https://github.com/Fazecast/jSerialComm/issues/7","id":72516590,"number":7,"title":"When using the InputStream of a port, it seems to buffer hundreds of lines","user":{"login":"farrellf","id":3859104,"avatar_url":"https://avatars.githubusercontent.com/u/3859104?v=3","gravatar_id":"","url":"https://api.github.com/users/farrellf","html_url":"https://github.com/farrellf","followers_url":"https://api.github.com/users/farrellf/followers","following_url":"https://api.github.com/users/farrellf/following{/other_user}","gists_url":"https://api.github.com/users/farrellf/gists{/gist_id}","starred_url":"https://api.github.com/users/farrellf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/farrellf/subscriptions","organizations_url":"https://api.github.com/users/farrellf/orgs","repos_url":"https://api.github.com/users/farrellf/repos","events_url":"https://api.github.com/users/farrellf/events{/privacy}","received_events_url":"https://api.github.com/users/farrellf/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-05-01T20:31:14Z","updated_at":"2015-05-01T20:44:57Z","closed_at":null,"body":"Hello,\r\n\r\nWhen reading a port's InputStream, it seems to buffer for a few hundred lines before allowing any text to be read out. After the buffer fills (or whatever is causing the problem) all of the text gets immediately dumped, so nothing seems to be lost.\r\n\r\nFor example, if I have a micocontroller sending a constant stream of lines (ASCII text) over a USB UART, appromixately 250 lines seem to get \"buffered\" before Scanner.hasNextLine() returns. My code is as follows:\r\n\r\n...\r\nSerialPort port = SerialPort.getCommPort(\"COM4\");\r\nport.setBaudRate(115200);\r\nport.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING | SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);\r\nport.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);\r\nif(port.openPort() == false) {\r\n\tSystem.err.println(\"Unable to open the port.\");\r\n\treturn;\r\n}\r\nScanner s = new Scanner(port.getInputStream());\r\nwhile(s.hasNextLine()) {\r\n\tString line = s.nextLine();\r\n\tSystem.out.println(\"Line of text from serial port: \" + line);\r\n}\r\n...\r\n\r\nLines of text are being sent by the microcontroller several times per second, but they get buffered and all dumped in a fraction of a second, in bursts of approximately 250 lines at a time.\r\n\r\nI'm not sure if this is due to an incorrect setting on my part, or maybe a bug in the library.\r\n\r\nIn my testing I have tried/verified the following:\r\n1. This problem occurs with two different computers and microcontrollers. Same behavior in Windows Vista and Windows 10 Technical Preview.\r\n2. The hardware and computers seem to be fine, as I can read the lines of text perfectly with Tera Term and other terminal programs.\r\n3. The RXTX library for Java works fine for me, but is less convent to use than your library.\r\n\r\nAny ideas?\r\n\r\nThanks,\r\n\r\n-Farrell"},"comment":{"url":"https://api.github.com/repos/Fazecast/jSerialComm/issues/comments/98231604","html_url":"https://github.com/Fazecast/jSerialComm/issues/7#issuecomment-98231604","issue_url":"https://api.github.com/repos/Fazecast/jSerialComm/issues/7","id":98231604,"user":{"login":"hedgecrw","id":11224689,"avatar_url":"https://avatars.githubusercontent.com/u/11224689?v=3","gravatar_id":"","url":"https://api.github.com/users/hedgecrw","html_url":"https://github.com/hedgecrw","followers_url":"https://api.github.com/users/hedgecrw/followers","following_url":"https://api.github.com/users/hedgecrw/following{/other_user}","gists_url":"https://api.github.com/users/hedgecrw/gists{/gist_id}","starred_url":"https://api.github.com/users/hedgecrw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hedgecrw/subscriptions","organizations_url":"https://api.github.com/users/hedgecrw/orgs","repos_url":"https://api.github.com/users/hedgecrw/repos","events_url":"https://api.github.com/users/hedgecrw/events{/privacy}","received_events_url":"https://api.github.com/users/hedgecrw/received_events","type":"User","site_admin":false},"created_at":"2015-05-01T20:44:57Z","updated_at":"2015-05-01T20:44:57Z","body":"Hey Farrell!\r\n\r\nI think this is actually just a configuration problem.  You're currently setting your port timeouts to BLOCKING mode which means that a read() won't return until ALL of the requested bytes are read.  The Java Scanner class implicitly uses an internal buffer and calls one of the read methods using that buffer, so the program is functioning correctly by not returning until the entire Scanner buffer has been filled.\r\n\r\nI think what you're aiming for here is to have the Scanner handle your buffering, in which case it needs to be able to read data as soon as any amount is available to be read.  This is enabled by the SEMI_BLOCKING timeout mode.  Try calling:\r\n\r\nport.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING | SerialPort.TIMEOUT_WRITE_SEMI_BLOCKING, 0, 0);\r\n\r\nand see if that works for you!"}}