C Serial Port Communication

  1. C Programming Serial Port Communication
  2. C# Serial Port Example
  3. Windows C++ Serial Port
Active6 months ago

Jul 11, 2019  Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. I have done serial port RS-232 connection in C using 16-bit compiler (I was using Turbo C IDE). It included header file bios.h which contain all the required functions for reading values from the.

I'm trying to send/receive data over an USB Port using FTDI, so I need to handle serial communication using C/C++. I'm working on Linux (Ubuntu).

Basically, I am connected to a device which is listening for incoming commands. I need to send those commands and read device's response. Both commands and response are ASCII characters.

Everything works fine using GtkTerm but, when I switch to C programming, I encounter problems.

Here's my code:

What happens is that read() returns 0 (no bytes read at all) or block until timeout (VTIME). I'm assuming this happens because write() does not send anything. In that case, device wouldn't receive command and I cannot receive response. In fact, turning off the device while my program is blocked on reading actually succeded in getting a response (device sends something while shutting down).

Strange thing is that adding this

right after write() call, I receive:

which is exactly what I expect. Only my program doesn't work as it should, like my device cannot receive what I'm actually writing on port.

I've tried different things and solution, also regarding data types (I've tried using std::string, such as cmd = 'INIT r' or const char) but nothing really worked.

Can someone tell me where I'm wrong?

Thank you in advance.

EDIT:Previously version of this code used

unsigned char cmd[] = 'INIT n'

and also cmd[] = 'INIT rn'. I changed it because command sintax for my device is reported as

<command><SPACE><CR>.

I've also tried avoiding the O_NONBLOCK flag on reading, but then I only block until forever. I've tried using select() but nothing happens. Just for a try, I've created a waiting loop until data is avaliable, but my code never exit the loop. Btw, waiting or usleep() is something I need to avoid. Reported one is only an excerpt of my code. Complete code needs to work in a real-time environment (specifically OROCOS) so I don't really want sleep-like function.

jww
58.2k45 gold badges261 silver badges560 bronze badges
Lunatic999Lunatic999
8951 gold badge10 silver badges16 bronze badges

3 Answers

I've solved my problems, so I post here the correct code in case someone needs similar stuff.

Open Port

Set parameters

Write

It was definitely not necessary to write byte per byte, also int n_written = write( USB, cmd, sizeof(cmd) -1) worked fine.

At last, read:

This one worked for me. Thank you all!

Steven Lu
20.1k42 gold badges153 silver badges294 bronze badges
Lunatic999Lunatic999
8951 gold badge10 silver badges16 bronze badges
Programming

Some receivers expect EOL sequence, which is typically two characters rn, so try in your code replace the line

with

BTW, the above way is probably more efficient. There is no need to quote every character. Microsoft scripting runtime dictionary.

radarheadradarhead

1) I'd add a /n after init. i.e. write( USB, 'initn', 5);

2) Double check the serial port configuration. Odds are something is incorrect in there. Just because you don't use ^Q/^S or hardware flow control doesn't mean the other side isn't expecting it.

3) Most likely: Add a 'usleep(100000); after the write(). The file-descriptor is set not to block or wait, right? How long does it take to get a response back before you can call read? (It has to be received and buffered by the kernel, through system hardware interrupts, before you can read() it.) Have you considered using select() to wait for something to read()? Perhaps with a timeout?

Edited to Add:

Do you need the DTR/RTS lines? Hardware flow control that tells the other side to send the computer data? e.g.

guestguest

protected by CommunityApr 2 '16 at 12:37

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

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

8 Apr 2012CPOL
Serial Communication with the .NET Framework in Winforms

Introduction

Serial port data can be read in many ways and many devices communicate via the serial port. In my project I had the need to implement serial port communication for a RFID reader. This is a sample example to read data from the serial port and display on the Windows Forms TextBox control.

Using the code

To begin, let's create a C# Windows Forms application in Visual Studio 2008 (hope it will work in VS 2005/2010 also, though not tested by myself). Add a textbox, a button control and a SerialPort control on to the form.

Declare a string variable as private:

In the form Load event , write the code as shown below:

Although the code is self explanatory, I will explain little.

When the application starts, the serial port is opened to read the received data,you need to set up an event.

It is always better to set up the event before the serial port is opened.

C Programming Serial Port Communication

Now lets declare an event handler to handle the event. The code to manipulate the read data can be specified here.

In my example the RFID tag had 12 letters value and only one card need to be read till a particular operation is completed. So, I have set a condition to close the port if string variable has length of 12. Actually RFID readers can read more than 100 tags per minute. Then you can store the values in some array and according to your need you can manipulate them. But if your tags have the same count like 878BGFFJJJHG, 78766HGGBVVV and you have to make sure that each tag is read then you can specify some condition like the above code.

You can use threading also but if you don't like to use threading then you can use a condition.

I insist that you should use condition as the above code for a tag [78766HGGBVVV] may return values of one letter at a time and it will be very fast. So to make sure that a tag has been read completely find the length and use the condition to do some operation or read another tag.

Now, you can't assign the value read directly to a textbox in the same event handler as it will throw some exception. You have to use the Invoke method to call your method, which can do the appending work in case of my program or manipulation work in your problem etc.

The below DisplayText method is called by the invoke method.

After the read operation is completed make sure you close the serial port.

C# Serial Port Example

Points of Interest

There are many ways to read data from serial port.

Windows C++ Serial Port