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

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.

Related

Problem with ivi.visa reading binary block data

I am writing an application which communicates with a network analyser using ivi.visa from Keysight. I often work remotely where I don't have an instrument, so I decided to write an application that can respond to the SCPI commands I need. This works ok for things like "*IDN?", but when I try to receive a binary block using ReadBinaryBlockOfByte(), the call terminates when it sees a newline character in the data.
If I set TerminationCharacterEnabled to false, all reads timeout. This is mentioned in the Keysight visa.net examples, so it doesn't seem to be an option.
So how can I read binary data?

Watson IBM Speech to Text c# api

I using follow example to recognize text from audio https://gist.github.com/nfriedly/0240e862901474a9447a600e5795d500 but I need also time codes, i added at line 40 "timestamps" : true, and removed "interim_results": true as I need only final results. But it broken, after { "state": "listening" } message it takes some time and raise exception like that
"Text" received message is invalid after the call Websocket.Closeasync. Websockets.In cases closeasync, so you should only use those when you do not expect to receive other data from the remote endpoint. Use "Websockets.CloseOutputAsync" to preserve the possibility of obtaining additional data, but to close the outgoing channel.
And if i set "continuous" : false, It do only the first iteration of speech (few first words before a pause), and then repeat {"state": "listening" } and freezes.
Can you help me, how to update that example to return Timecodes?
continuous: false means "only transcribe until the first pause" - so it isn't "freezing", it's just stopping when you tell it to.
The service then sends the final results followed by the second {"state": "listening"} message to indicate that it's done sending results. The example code closes the connection after that, but it sound like you're still attempting to send audio after closing the connection.
I'm not certain, but I think that timestamps and interim_results will probably work the way you want once you set continuous: false.
Although, if you only need final results, then the HTTP interface might make more sense. It's much simpler than the WebSockets one.
Finally, as I mentioned in email, the official IBM Watson .net SDK has support for Speech to Text in the development branch right now, and should have it included in a release soon.

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!

c# serial port redirection

Is there a way to, in c# write a serial port redirector? I have an app that is coded to use port 1 (com1:) however, my port on the handheld (Intermec CN50) is always 3. I do not have access to modify the mobile software, however I would like to redirect any data going out to 1 to copy it to 3 and any data coming in to copy it from 3 to 1, so the app doesnt know the difference.
I know this is a difficult issue, but I am sure one of you guys out there have an answer.
I sure dont.
Thanks
Chris
No, there isn't. You have a couple alternatives, depending on the device and your exact needs.
The first, and simplest, is to redirect the driver. If your device doesn't already have a COM1 device defined, you can modify the registry in HKLM\drivers\builtin and change the Index for your specific port from 3 to 1. That will cause device.exe to expose the port as COM1 and not COM3. In my experience this isn't likely to work, though, as most serial ports are set to let the OS auto-assign the index, meaning COM1 and COM2 are already in use. You can, however, still work around this. You simply have to explicitly assign an Index to every instance at or below 3 for the device. Move index 1 to 3 and index 3 to 1 and you effectively swap the ports.
Here are some examples - there are a few because it depends on how the OEM set up driver initialization. Here's an example of a driver with no explicit port named (in this case I2C, but it works the same way):
Note there is a Prefix and an Index. This driver is going to enumerate as "I2C0:". If you changes the Index value to something else, say '2', then it would enumerate as "I2C2:".
In some cases the driver can provide a specific port name, like this one:
Note here that the Port value matches Prefix plus Index. No idea what would happen if you manually set them different, so don't do that. If you wanted to move this one to COM3, you'd change Index to 3 and Port to COM3, but you have to make sure there's nothing else in the registry already set to use those values. You don't want two driver instances trying to enumerate at the same Index. If you do, I think the behavior is that the second one just gets incremented, but it's best to explicitly set the values as unique so you know what's happening.
If that doesn't meet your needs, you'd have to write an actual device driver to do the port move/aggregation. It would work a lot like the existing GPS Intermediate Driver (GPSID) under Windows Mobile and would allow you to redirect the serial data to any port you want. This, however, must be written in C because managed code (in the CF) cannot export native symbols so there's no way to get device.exe to load a C# assembly.

How to handle incoming control characters in a custom terminal?

For my project I needed to create a custom terminal-like window and connect to a Cisco router through console port. So I inherited a TextBox and created a custom control in WPF. And I thought it would be enough; just to add some code to implement extra features I wanted. I'm sending one character at a time and wait for responses. But then I saw that my custom terminal does not handle special characters, those basic like \n sure are recognized but all the others not. I have written responses to a text file and I see i got quite a lot of those special ones but I don't know how should I react.
Maybe someone knows or have found some links with info about how to react for a specific control character? I have only found what characters are send here , but nothing else about it. I understand that for a "BS" I will delete one character before carriage but what should I do when I get "ACK" or "DC1" ? Maybe I should just skip them ? Any ideas?
Edit:
Apart from sending one character at a time I'll get response only when I send enter or characters like ? or TAB. Then when I receive a response it's mixed with special chars to format the text like in 80x25 terminal window. And I would like just to display it like it wants, but that means I need to know how to react when I get special chars I don't need to display them, just to format normal text based on the special ones I'm also receiving. When I get BS i need to clear the character before carriage, when I get DEL I need to clear the character after carriage. And I would like to know what I need to do when I'm getting other special chars. I know I can do it the hard way and just send those to a working terminal and see what it will do but it will take a while so I wonder if someone maybe knows the answer.
Ok, I played the hard way with terminal and now I know that the only special character I need to handle is BS (of course I only tried this with Cisco routers but I just need to work on them), the only problem is that you need to create your own code to get it work and move caret on the right place based on the BS count. And better create your own variable to store caret index because TextBox CaretIndex just didn't want to work as I expected. The rest of special characters can be, as far as I know, omitted. And it works great as I wanted.

Categories

Resources