Communicate with a port
Perhaps the title may seem dubious, but in this post I will try to expose an overview on how to read the data coming from a USB, serial, or even by an Arduino board.
Before you start it is better to understand what good is all this ?
Imagine having to deal with something that is beyond just reading and writing files, imagine having to know the value of a sensor: the need to know how much has been rotated the wheel of a car, the temperature with a thermometer, the intensity light, etc. In this context we must read an input that we can get to the computer in various ways, the easiest way was analyzed and digested and comes to us as a string of information on the USB port.
By connecting the USB port to the system that reads the data, as such as Arduino, our computer does not see almost nothing, there is a file to be read; there is a communication port that shoots continuously and data that can also accept data
will not go into programming Arduino, which is beyond the scope of this post, there are also other sensors with USB port in addition to Arduino; I will mention to indicate
http://bitlash.net/ as one of the easiest to program it.
The command to communicate with a door is open driver device , depending on the operating system the string device is different. On Windows systems all ports are COM, then you can write:
Code: Select all
open driver "COM2:" for binary update
while on Linux ports are tty (or something similar) into the folder / dev , then:
Code: Select all
open driver "/ dev / tty2" for binary update
Mac is a bit more complicated, because the name and position changes with each version, however, for the rest it is like on Linux:
Code: Select all
# Try one of these: /dev/cu.usbmodem2, /dev.tty.usbmodem2, /dev.cu.usbserial2,
open driver "/dev/cu.usbmodem2" for binary update
mode "for binary" (dialogue in binary) is the most used, but you can open it in plain text.
Now comes the tricky part, reading and writing. And 'complicated because you have to understand when they begin and end the data.
If your signal is single channel (then receive information from a single sensor), usually the end character information is a carriage return. In this case, just: (by replacing usbserial with your device )
Code: Select all
read from driver usbserial for 1 line in 100 milliseconds
put it into theInput
As you see, you must specify a maximum time in which the program starts to listen, otherwise if there is a transmission error the program stops because it does not get why the end of the data (carriage return).
This command can repeat it every 30 milliseconds to update the value read.
If you want to send a command to your sensor / transducer, such as "I am ready to receive data", just use write , here's the code:
Code: Select all
write "run lsd, 500" & return to driver usbserial
If you read from several sensors, the USB port will multichannel information, like: a1 = 22 34 a2 = a3 = 18 ... The roads here are many and depends largely on the update time you need. Take all the rows together and then analyze them is the worst, you risk losing a lot of time to figure out if you have received all the data. Usually the best strategy is to read one line at a time, analyze it and read the next one. The same goes for writing. Another typical mistake is to want to immediately want to communicate at very high speeds, experiment and see if you really need it . Usually the base speed of the door (9600) is more than sufficient for almost all applications. Increase speed increases synchronization problems between reading and writing, the time decreases in provisions and you can not figure out if you have received all the data correctly.