Continual Serial Port Reads - c#

I'm trying to build a C# application that has one textbox that puts the data received on a selected serial port onto new lines as they come in.
I realise this isn't a specific question, but I have done a couple of hours search and can't even find out what to ask exactly from a coding point of view.
Can someone please point me in the right direction of how to read serial data continually?

look here, has examples of how to read and configure the baud rate, handshaking etc:
http://msmvps.com/blogs/coad/archive/2005/03/23/SerialPort-_2800_RS_2D00_232-Serial-COM-Port_2900_-in-C_2300_-.NET.aspx

Related

msp430g2553 How to share datas from board to VS for C#

I wanna make a project with msp430g2553 like use heat sensor and want to read the datas from the sensor on the program that I write on C#. I know the basics of C# I can build form applications and also can use msp430 boards but i don't know how to link them each other. I ask that how can I link each other?
But I want to write the all codes C# and msp430. I don't want to prepared GUI.
I searched a little bit there is something like GIOP to do that but there is information little about that. Can you help me please.
And thanks for helps.
I agree with jdweng that you should use the UART feature from the Launchpad development board. Just find the guide for your board and look for the basic demo that outputs the "hello world" message to the serial port with connection to your PC using a terminal program such as Putty in serial mode. Then write a simple program in C# to replace the terminal program and establish that your program can read the "hello world" from the serial port. From there, you then need to change the demo to reading the sensor. I suggest you first emulate reading data from the sensor (i.e., must make up some data) and output that to the serial port, then finish your C# program with that data. Then find a suitable sensor and read the datasheet for the part. You may need to use another interface (such as SPI or I2C) to read form the sensor. This will depend on the sensor you choose. Best of luck!

Transferring binary file over through RS232 using C#

I am looking to transfer a binary file via RS232. I need to do this to 5000 different devices (no joke). I could do them one-by-one through a terminal program but that will take a lot of time.
So, I am writing a C# program that will be able to automate the process. I am looking at using XMODEM protocol and command line parameters to start the process. I have been looking for this for a few hours now and far my results have turned up little. I tried using uCON but that takes some sort of scripting language.
I was wondering if anyone in the community here might know of a solution to transfer a file over RS232 and program it in C#. Whether it is from a protocol (XMODEM) or a program that accepts command line or some other custom solution, doesn't really matter to me.
Ok so I was able to confirm that the code found on the website( ghielectronics.com/community/codeshare/entry/825) was able to work. The issue was was that I did not know how long it would take the computer to transfer the file. I thought that it would be quick. However, after further testing, it actually takes about 30 sec to 1 min depending on the file size.
This C# code snippet allows anyone to transfer a binary file over the Serial Port using the XMODEM protocol. This is done in code and a terminal is not needed which fits the requirements that I needed for the project.

C# periodic new line reading from serial port

Checked many pages but I still find it unanswered.
My problem is as follows. I have a device, connected over RS232 to the computer. The device is sending TWICE a second a line like this:
"*X;0;bbb;cc;d;eee;f\r\n"
The fields are fixed width numeric fields. Once every minute the 0 flag raises to 1 and the "cc" changes. This number I need then.
In the C# application, I cannot get it working. Tried to use the recieve event to get the data, but the refreshed result needs 10+ seconds to be processed. This means that 10 seconds after the flag was risen i get the result. Sometimes it takes even longer.
I tried first with RecieveEvent. I even tried to fill the buffer untill it would fill the line, but again, same result. Then I tried with Timer that invokes every 300 ms and reads a line. Still the same problem.
So my question is, how to read and process up to date data from serial port?
When reading, the serial port will buffer data. This will introduce a small delay (but we're talking milliseconds, not tens of seconds). Most serial device drivers will allow you to configure the buffering, too, if it's really critical.
If you call ReadLine, you could introduce more delay, as it will wait until it receives a newline before it returns any data. Are you sure that your device is sending good newlines? Use a terminal emulator program to see exactly what the device is actually sending, as device documentation is often terrible!
I would Read raw data from the port, and then parse it myself. You may have to be more careful about receiving partial packets, but you remove all the middle-men from the picture, and you can be more tolerant (e.g. Not caring whether or not the data has newlines in it). Start a read and then write out the data you received to the debug console and you'll soon see what your program is receiving and how often, etc. This will also show you if the delay is in the serial port or in your processing of the messages
You may try serialPort1.readExisting(); then try parsing the data.
Good luck!

get list of printer ports

I'm looking for a way to list all printer ports on the current PC. I want to list them and put them into a combo box. I was going to create a list and pull that in every time the cbo must load, however I then thought about if people add their own ports and some printers install themselves to new ports. Because of this just stating a generic list of ports might not be the correct way.
I am using DOS print process to the printer port directly and in the settings need the user to select which port to print to.
I'm sure someone has a quick way, or a way they can point me to. I saw a couple ways online however It completely boggled my mind and I don't like using code I can't understand, and don't have the time to understand it right now unfortunately. I am sure there must be a class of somesort in C# already created to do this. Most of the other ways I saw to me appeared to be importing several dlls and or running through a few registeries.
And assistance is greatly appreciated
UPDATE just to be clear I want a way that will simply return to me LPT1 LPT2 COM1....

Receiving Invalid Data from Serial Port using SerialPort.ReadExisting()

I have written a program in .NET that listens to a particular Serial Port and processes the data that is being received. I wrote a test program using com0com (the Null-modem emulator) and my program was working fine. I even tested it with HyperTerminal and that seemed to work fine too.
However when I interfaced the software with the original device (an output received from a control system), the data received was garbled. It contained special characters. The same device when connected with Hyper Terminal produced the desired output. I changed the baud rates, parity etc but the data received was the same set of garbage characters.
I have used the DataReceived event of the SerialPort component and used the following line of code to capture data:
string data = portRecieve.ReadExisting();
Can somebody tell me where am i missing out? In the current environment, the output from the device is directly connected with a dot matrix printer which prints whatever is received on the port. The printer seems to catch what is being sent but my code couldn't.
If you ever encountered a similar scenario, Please share your findings.
Thanks
How did you set
SerialPort.DiscardNull
SerialPort.Encoding
And maybe show us an example of the special chars you are receiving.
I can think of the following reasons why the data might apperar garbled:
If there is a bad physical connection, you can sometimes just get garbage (rather than nothing at all). Try unplugging and replugging the leads - and check that you have the correct lead (e.g. do you need a nullmodem?). It looks as though you have this covered by checking in HyperTerminal.
If the baud rate, stop bits, parity are not correct - sounds like you have this one covered
You are trying to receive the data as a string. If it is not sent as plain text, or if your encoding is wrong, then it could easily appear garbage-like. Try using a binary receive and examine the raw data that you are receiving. This will tell you whether the data is just wrong or the .net conversion is screwing it up - eliminate the middle man!
It sounds to me like the device is putting the printer into some special graphics mode. If so, there is likely to be escape sequences in the data being sent to the printer, ie. character sequences starying with an escape (27, 0x1B) character.
In this case, you'll have to look at the printer manual to see what the commands do. Alternatively, you might be able to tell the device to use a simple ASCII only printer, rather than a intellifent one.

Categories

Resources