In order for the serial port object to not be null, it must store the object for the open serial port.This post also assumes that Java is already properly set up with RXTX.
Serial Port In Java Serial Port ObjectThis set up is pretty much using my computer as a remote control for whatever device is on the Arduino end. I also added key bindings which I can use to control the throttle. Once a successful connection is made the controls are enabled. This is done through the use of the setConnected(true) and the toggleControls() methods shown in the example code that follows. That first import is specific to RXTX, and all its library methodsclasses are in there. This is a class in RXTX and is required in order to receive incoming data. What the variables are for is in the comments but a more detailed explanation will follow. What that means is that I can associate ( put() method) the name of a serial port, say a string that says COM1, to an object in the code. Later, I can access the name COM1 from the HashMap by using the get() method and it will return the object that it was associated with previously. When stuff happens in the program, the program stores a string in this variable and it will be appended to a text area in the GUI. Code adapted from Discovering Available Comm Ports from the Reference Material. The code can iterate through each element inside the Enumeration and determine whether or not it is a serial port. The method getPortType() can identify what kind of port it is. If it is a serial port, then the code will add its name to a combo box in the GUI (so that users can pick what port to connect to). ![]() This is helpful because the names listed in the combo box are the actual names of the object (COM1, COM2, etc), and so we can use these names to identify the actual object they are tied to. See How to Open A Serial Port in the Reference Material for more information. The object must also be casted as a CommPortIdentifier because the get() method has a return type of Object. This instructs the program to open the port, and this method will return the object for the opened port, which I store in in the previously initialized CommPort object. I then cast this object as a SerialPort and store it as well. This is helpful for accessing the methods and variables specific to the SerialPort class. The next catch block is just for the generic exceptions that occur. Serial Port In Java How To Replicate ThatI never came across that during testing, nor do I know how to replicate that.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |