Loopback Test Serial Port

Scenario: I have two machines which should communicate with each other through a serial port (RS232). However, it doesn’t work. Loopback Tests. Now, I need to ensure that each component is working.

  • Basically, this test will verify that your PC's serial port is working properly, and that the RS-232 software that you are using is operating correctly, and that you are connected to the proper port. It is a common mistake to connect a serial device to the printer port, since many believe this to be a 25-pin RS-232 port.
  • Abstract away your serial port comms behind an interface so that you can code your app against the interface and then test with a 'fake' implementation. When you've got the hardware for the real thing, you can code up the 'real' implementation of the interface and swap out the fake one.
  • Loopback Test. A loopback test can be used to troubleshoot serial communications. It can show problems in the serial port, the cable, or the software generating the messages without having to connect to third party hardware.
Active6 years, 7 months ago

So I have a standard RS232 serial port that is looped back to itself by simply running a wire from Tx to Rx. I'm testing loopback by running echo and cat in two separate terminals:

Adobe Flash Professional CC 2015 Full Version adalah sebuah software terbaik yang dapat anda gunakan untuk membuat animasi flash professional dengan tampilan. 201 rows  Adobe Flash Professional Cc Serial Numbers. Convert Adobe Flash Professional Cc trail version to full software. Adobe flash professional cc crack for mac. What Is Flash Professional for? Over the years, the technology of Adobe Flash is easily installed on most web pages. From animations to programs running in the browser or Adobe Air, all have been created using Adobe Flash Professional.

My issue is with the output. I would expect to see one 'hi' come back on the terminal running cat but instead I get this:

..and so on until I ctrl+ccat.

After interrupting cat, if I run it again it will not output 'hi's until I run echo a second time.

Is this normal? Any idea why I'm seeing this behavior?

Edit: By newline, I mean ASCII 0x0A. There are no carriage returns in this output.

Martín Canaval
1,0861 gold badge8 silver badges21 bronze badges
RyanRyan
4162 gold badges5 silver badges12 bronze badges

2 Answers

Thanks to the second comment by Bruce, I was able to figure out the problem on my own.

After running stty -a -F /dev/ttyS1, there were 3 options I found to contribute to the problem: 'echo', 'onlcr', and 'icrnl'.

Since this serial port is looped back to itself, here is what happened after running echo 'hi' > /dev/ttyS1:

Loopback Test Serial Port
  1. The echo command appends a newline to the end of the message by default, so 'hi' + LF is sent out to /dev/ttyS1
  2. Because 'onlcr' was set, the serial device converted the LF to CRLF so the physical message sent out the Tx line was 'hi' + CRLF
  3. Because 'icrnl' was set, the physical messaged received on the Rx line converted the CR to LF. So the message outputted by 'cat' was 'hi' + LFLF.
  4. Because 'echo' was set, the message received on the Rx ('hi' + LFLF), was then sent back out on the Tx line.
  5. Because of onlcr, 'hi' + LFLF became 'hi' + CRLFCRLF.
  6. Because of icrnl, 'hi' + CRLFCRLF became 'hi' + LFLFLFLF
  7. Because of echo, 'hi' + LFLFLFLF was then sent out the Tx

And so on..

In order to fix this problem, I ran the following command:

Disabling 'echo' prevents an infinite loop of messages and disabling 'onlcr' prevents the serial device from converting LF to CRLF on output. Now cat receives one 'hi' (with a single newline!) for each time I run echo.

CR = carriage return (ASCII 0x0D); LF = line feed or newline (ASCII 0x0A)

RyanRyan
4162 gold badges5 silver badges12 bronze badges

I had a similar issue as well with concatenating files into a serial tty for testing. In addition to the accepted answer:

If you're testing serial output by doing something like: cat somefile.txt > /dev/ttyS0, it will have a good amount of unexpected byte data if you're testing for exact byte values.

With stty doing a simple stty raw -F /dev/ttyS0 will stop the terminal from inserting/replacing characters (e.g. [..] 0x0A [..] ->[..] 0x0D 0x0A [..]). The raw flag changes the modes of the terminal so no input and output processing is performed.

ineedtosleepineedtosleep

Not the answer you're looking for? Browse other questions tagged catserial-portecho or ask your own question.

Active1 year, 2 months ago

I'm about to start developing a small app (C#) that communicates with a PLC and a testing unit via Serial Ports - this is my first venture into this area.

In essence, I am going to send the PLC a signal to start an operation, and then I am going to wait for the result of that operation from the test unit (which will be independently communicating with the PLC) to return a ASCII string.

Depending on the content of that string, I may want to listen to a signal from the PLC..

It's all new to me, so at the moment, I'm just researching System.IO.Ports.SerialPort; digression: are there third part products out there than simplify interaction with the Serial Port, or are the built-in classes as good as you will get? I'm thinking of ease of use as opposed to better features.

However, it will be a few weeks before the hardware is available for development and testing, so I was wondering how I could simulate communication to/from the serial port so that I can start developing my app?

[I don't yet know how the PLC and the PC are due to communicate - I understand it will be binary rather than text, but at the moment, that is all I know.]

Anthony Mastrean
14.7k19 gold badges84 silver badges150 bronze badges
CJMCJM
9,40920 gold badges69 silver badges112 bronze badges

6 Answers

Abstract away your serial port comms behind an interface so that you can code your app against the interface and then test with a 'fake' implementation. When you've got the hardware for the real thing, you can code up the 'real' implementation of the interface and swap out the fake one.

So for example, you'd have an interface

and you'd code your app against that interface using a fake implementation:

Hope that helps!

DavidGougeDavidGouge
3,3835 gold badges25 silver badges42 bronze badges
fryguybobfryguybob
3,7372 gold badges25 silver badges35 bronze badges

There are two pieces of software that I have found invaluable while doing serial port work.

Free Serial Port Monitor

Despite the cheesy name, it is actually quite useful. Note that you should have it stop listening to your port if you go to unplug a USB-to-Serial converter. Otherwise it can crash (well.. wait indefinitely on exit, which is annoying). It doesn't have to put itself in the middle of a serial connection to sniff data. It monitors the IO using the Win32 API.

Franson Serial Port Tools

Or. any loopback software really. There are lots out there. This allows you to send data and receive it within software. If you end up doing any GPS work, Franson also has a nice GPS simulator so you don't have to sit outside the whole time to debug code.

Finally, if you have had enough with the built-in serial class and its horrendous shortcomings, then you need a replacement, and going straight to the Win32 API will take forever.

CommStudio

I have found CommStudio to be absolutely rock solid. Quite frankly, after spending 5 months researching and buying other options, it is the only one that works perfectly with removable USB adapters. All of the other solutions have issues when the device is plugged back in. You can download their free 'Express' version here: http://www.componentsource.com/products/commstudio/downloads.html?rv=42917

BradBrad
122k30 gold badges252 silver badges413 bronze badges

There is another resource out there that emulates serial ports for windows if anyone else is still looking for decent serial debugging tools.

The 32-bit version is free and seems pretty decent. It's called Virtual Serial Ports Emulator.

jlafayLoopback Test Serial Portjlafay
9,20312 gold badges63 silver badges90 bronze badges

How To Test Serial Port

I like David's answer above but if your looking to do integration tests and actually test your serial port communication I have used and application called ViN soft virtual serial cable in the past to basically create 2 serial ports on your machine that are connected by a virtual cable.

Also if you have a serial port on your development machine you could use it to connect to another machine that has a serial port and write an application that will basically simulate the communication of the PLC.

I would prefer to use a combination of both David's method and this method to ensure proper testing.

Cole WCole W
12k6 gold badges39 silver badges78 bronze badges

I have wrote an article on this topic using Virtual Serial Port Driver 9.0 standard using Microsoft SerialPort Class (Sytem.IO.Ports), it is of course possible to use any other comm port tool.

In the software I create 2 virtual ports COM1 and COM2.

I use COM1 to emulate as data sender.

I use COM2 to receive what ever being send from COM1.

Loop Back Test Serial

This is helpful if you are developing Embedded or IoT solution.

Emulator (in this example as random accelerometer)

And my data receiver is almost similar

Disclaimer: the link of this guideline refer to my personal web site.

Loopback Test Serial Port Cisco

maytham-ɯɐɥʇʎɐɯ

Serial Port Loopback Test Windows 7

maytham-ɯɐɥʇʎɐɯ

Serial Port Loopback Test Software

13.1k7 gold badges52 silver badges75 bronze badges

Loopback Test Serial Port Putty

Not the answer you're looking for? Browse other questions tagged c#serial-portsimulation or ask your own question.