Serial Port Data Logger C
Your data logger may record great data, but if you can't retrieve it, the data isn’t going to do you much good. To ensure your successful data retrieval, it’s essential that you can troubleshoot possible problems that may arise with PC-to-data-logger communication over a serial connection.
I have a device connected to my computer that sends serial data to the computer every 5 mins. I want to write a basic program to capture this serial data every 5 mins and put it into a database. I was hoping to use C# because I have used C# with databases before and found it quite easy.
Data Logger Temperature
Can anybody offer me any advice on how I might do this, I really have no idea where to start and I know in theory it sounds easy but when I started it I actually found it really hard.
dsolimano2 Answers
Using C#, you can use the System.IO.Ports namespace to communicate over the serial ports - there's a nice article here.
Alternatively, you can use Python and the pySerial module. I've written an app to communicate over the serial port using pySerial - it's quite easy to use, and can run on many different operating systems including OSX and Windows (I'm assuming you're using Windows). Python also has built-in support for SQLite.
MikeVMikeVGiren no yabou axis no kyoui iso file. The problem with capturing data on a serial port is that serial ports aren't thread-safe, so if there is more than one listener, data will be corrupted.
If you are absolutely sure that you're the only one listening for data on this port, .NET has a built-in wrapper, System.IO.Ports.SerialPort, which you can use to connect to COM1, COM2, etc. You'll need to know the rate in bits/sec at which this device sends data (its baud rate), its error-checking (parity) protocol, and the format of the data it is sending (you'll get it as a byte array, which you must convert byte-by-byte into data you can work with). Then, your program should be able to open the port and listen for DataReceived events with a handler that will read and digest the data. Again, it's VERY important that you never have two threads trying to read at once; the easiest way is to set a volatile boolean indicating that a handler is reading data; if another handler is ever spawned while a previous one is still running, the first thing the new one should do is read that value, and since it's set, exit the new handler immediately.
KeithSKeithSNot the answer you're looking for? Browse other questions tagged c#loggingserial-port or ask your own question.
I am new to serial programming in Linux using C. I have found a small piece of code to write data on serial port which I am sharing here. After running this code I may assume that data has written on a specific port. Now I would like to open another terminal and using separate code want to read the data written on that specific port - how do I do that?
The code above will write the data on a specific port.
Jonathan Leffler2 Answers
In theory, all you have to do is open the relevant port for reading, and use read()
to get the data.
There are differences; notably, the read needs a buffer to put the data in. The code shown discards the first message read. Note that a short read simply indicates that there was less data available than requested at the time when the read completed. It does not automatically indicate an error. Think of a command line; some commands might be one or two characters (ls
) where others might be quite complex (find /some/where -name '*.pdf' -mtime -3 -print
). The fact that the same buffer is used to read both isn't a problem; one read
gives 3 characters (newline is included), the other 47 or so.
The program posted makes a lot of assumptions about the state of the port. In a real world application you should do all the important setup explicitly. I think the best source for learning serial port programming under POSIX is the
Serial Programming Guide for POSIX Operating Systems
I'm mirroring it here: https://www.cmrr.umn.edu/~strupp/serial.html
rkta