C Serial Port Sample Application

  

Active7 years, 8 months ago

Can someone please tell me how to communicate with serial port to read and write data? I need a simple program just to learn the basics of serial port communications in C#.

marc_s

This is a basic sample of serial port (COM port) listening in C#. This application is connected to a GPS sending ASCII text for test, but the serial port listening part is all byte-oriented. CodeProject is missing a simple serial port application. Jun 07, 2015  The software is written using C language and communicates with the Serial Port using Win32 API. In Windows,Serial ports are named as COM1, COM2,COM3. COM1 and COM2 usually refer to the hardware serial ports present in the PC while COM numbers in double digits like COM32, COM54,COM24. Etc are given to USB to Serial Converters or PCI. CSerial::Close - While the d'tor will automatically close the serial port for you, this function has been added just in case there is a reason that you need to explicit close the port. CSerial::SendData(const char., int) - This function writes data from a buffer to the serial port. The first argument it takes is a const char. to a buffer.

I was looking for a working sample code of serial port or COM port code written in C for Linux operating system. I came across this very nice serial port library, which was very simple to use and well documented. Serial Port programming in Linux using C - Working code Ravi Pujar 07 June 2015. I was looking for a working sample code of serial port or COM port code written in C for Linux operating system. I came across this very nice serial port library, which was very simple to use and well documented. For my application, i wanted to communicate.

606k137 gold badges1159 silver badges1292 bronze badges
Kaleem SajidKaleem Sajid

closed as not a real question by casperOneJan 30 '12 at 15:25

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

From the MSDN documentation of System.IO.Ports.SerialPort:

M.BabcockM.Babcock
16.7k4 gold badges44 silver badges78 bronze badges

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

Active2 months ago

I am a little bit confused about reading and writing to a serial port. I have a USB device in Linux that uses the FTDI USB serial device converter driver. When I plug it in, it creates: /dev/ttyUSB1.

I thought itd be simple to open and read/write from it in C. I know the baud rate and parity information, but it seems like there is no standard for this?

Am I missing something, or can someone point me in the right direction?

jww
58.1k45 gold badges261 silver badges559 bronze badges
gnychisgnychis
2,39513 gold badges60 silver badges93 bronze badges

2 Answers

I wrote this a long time ago (from years 1985-1992, with just a few tweaks since then), and just copy and paste the bits needed into each project.

C Serial Port Sample Application Status

The values for speed are B115200, B230400, B9600, B19200, B38400, B57600, B1200, B2400, B4800, etc. The values for parity are 0 (meaning no parity), PARENB|PARODD (enable parity and use odd), PARENB (enable parity and use even), PARENB|PARODD|CMSPAR (mark parity), and PARENB|CMSPAR (space parity).

'Blocking' sets whether a read() on the port waits for the specified number of characters to arrive. Setting no blocking means that a read() returns however many characters are available without waiting for more, up to the buffer limit.

Addendum:

CMSPAR is needed only for choosing mark and space parity, which is uncommon. For most applications, it can be omitted. My header file /usr/include/bits/termios.h enables definition of CMSPAR only if the preprocessor symbol __USE_MISC is defined. That definition occurs (in features.h) with

The introductory comments of <features.h> says:

Gabriel Staples
3,3273 gold badges21 silver badges49 bronze badges
wallykwallyk
50k10 gold badges68 silver badges125 bronze badges

For demo code that conforms to POSIX standard as described in Setting Terminal Modes Properlyand Serial Programming Guide for POSIX Operating Systems, the following is offered.
It's essentially derived from the other answer, but inaccurate and misleading comments have been corrected.

Tutorial

To make the program treat the received data as ASCII codes, compile the program with the symbol DISPLAY_STRING, e.g.

If the received data is ASCII text (rather than binary data) and you want to read it as lines terminated by the newline character, then see this answer for a sample program.

sawdustsawdustSample
11.3k1 gold badge27 silver badges40 bronze badges

protected by user405725 Jan 24 '13 at 20:53

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?

C++ Read Serial Port

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