I'm trying to read data from a usb device. I have code that works for monitoring the device's output, but it's adding an extra character at the end of each line.
The answer is to use the sys.stdout.write function. The syntax for this command is as follows:
sys.stdout.write("")
If you want to include a newline character in your output, then use the following syntax:
sys.stdout.write('' + '\r
In a friendly tone')
If you're trying to write output to a file, but you're getting extra characters at the end of your string (a newline character), this is because the write function only writes data if it's a newline character. To fix this, add a call to fputs() before your write() call in order to add an empty line after each time there is output from the file:
To prevent the write function from working before an event happens, you can use a flag to indicate when you want to read from the file and/or device:
Use a flag to indicate when you want to read from the file. For example, if the user clicks on an icon in their taskbar, use a flag that indicates when they are ready for data (for example, by setting it equal to 1).
Use another flag that indicates what kind of event happened (for example, 1 = click). That way your program knows whether or not there was a click before reading from or writing into anything.
You can also create other flags as well—one for opening and closing files/devices; one for closing all processes related with this device (if applicable);
These flags will help prevent any errors from happening because no one wants those!
The first thing you will need to do is import the usb module. Next, you will want to import the sys module. After that, import the time module and then the os module. The string and random modules are next on your list of imports, followed by math and ctypes. Finally, if you are using Python 3.3 or higher with threads enabled for your system (or if you are using a version of Python that does not support threads), then import the threading package now before continuing with importing datetime .