Title Communicating with the Serial Port Author Organization: Wolfram Research, Inc. Address: 100 Trade Center Dr. Champaign, IL 61820 Phone: (217) 398-0700 Fax: (217) 398-0747 Email: URL: Description With the release of J/Link, almost all the capabilities of Java are now available directly from Mathematica. Now you can perform many tasks that used to require writing a MathLink program by simply loading the appropriate classes. Communication with the serial port in Java is handled by a standard extension package for Java called javax.comm, otherwise known as the Java Communications API.
I have a StarTech PCI2S950 PC serial port card which supports 9-data bits, but am not sure how to code my monitoring app & have googled a lot to no great avail. I would prefer to code in C (or Delphi, or C++). It goes from the 9 pin serial port on my computer to a female socket on the top of the tool that looks like a big telephone connector. I probably didn't have the terminology right. All I've been able to find doing an internet search is 9 pin serial port to USB adapter cables and those won't work on this thing.
This functionality is not yet part of the standard Java 2 platform, although it is a standard extension. Implementations are freely available for Solaris, Windows, and various Unix platforms. Most other platforms will soon be supported. To use J/Link to control a serial port, all you need to do is use the classes and methods that are available from the Java Communications API. Note that J/Link neither has nor requires any special knowledge about these Java classes.
It works this way with any Java class. Subject URL Downloads JavaSerial.nb (41.7 KB) - Mathematica Notebook.
It's not entirely clear what you mean by 'write and read at the same time' because that can refer to different contexts. It can be confusing because people tend to mix the meanings of full-duplex/half-duplex when switching back-and-forth in discussion of the hardware and software layers. True full-duplex serial communications requires hardware level support. Separately, to make use of full-duplex communications, your serial library and program itself must be written to support full-duplex.
![Port Port](/uploads/1/2/5/4/125448816/731637534.png)
Most simpler two-way serial devices will only respond after receiving a command. For example, you send a command (write), and then expect a response (read). This is sometimes referred to as half-duplex and this is what your current program example is doing. However, you shouldn't be closing and reopening the serial port. If the serial device you are working with is such that you can write at any time and you can independently expect to receive something from the device at any time, then you are working with a full duplex serial device. In that case, your options are either multi-threading or doing writing/reading sequentially without the reads blocking your program indefinitely. Multithreaded programming with python is possible on most arduinos.
(Again, this gets confusing. Another answer here indicated that arduinos do not support multithreading. That is true for most arduinos at the hardware level. But there are ways to simulate multithreading at the software level, and python does this.) A multi-threaded solution to your problem can get complex. Here are some examples though:.
If multi-threading is not available to you, then you can use a single thread where the writes are before reads, and the reads are done only if bytes are available or with a short timeout. Psuedo program loop:. Is there data to write? If so, write it. Attempt to read anything that has been received since last time tried to read. If serial library/hardware can provide 'bytes available' on the read line, check that to determine if you should try reading anything.
If library/hardware can not provide 'bytes available' on the read line, then set a short time out for the read. This will block program until timeout waiting for anything to come in. If bytes do come in, start processing them. Loop (or if nothing to do, sleep a little to prevent 100% cpu) The single thread approach works as long as your hardware read buffers are large enough to hold the inbound data until you are able to read it and clear the buffer. Examples:.
![Mathematica 9 serial portal Mathematica 9 serial portal](/uploads/1/2/5/4/125448816/572334009.gif)
If going with this approach, be aware that while is probably safe, using can be non trivial when using a timeout, because it's possible that timeout will occur before a complete multi-byte 'message' has been received. $ begingroup$ 'Simulated multithreading' doesn't do anything different than sequentially calling the read/write; you're just concealing/obscuring the fact that it's done sequentially. Your 'pseudo program loop' is also what OP posted in their question. If OP posted code showing they understand that they can write then read from a serial port, then ask how they can do both at the same time, then it looks clear to me that they're asking for full duplex (simultaneous) read/write capability, which isn't possible on the Arduino.
$ endgroup$ – Mar 20 '17 at 14:19.