• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 655
  • Last Modified:

Sending a string through the com port

I need a simple program that will send a ascii string through com 1 and read a string the device sends back. I have written something useing the bioscom function but it is not working with my device. I am really tring to find another function to try.

1 Solution
What version of Windows are you on?
"a simple program that will send a ascii string through com 1"

very nice sentence, but far away from reality ;-)

ofcourse you can send a string through your com-port, and i also beleave that your device gets this string. but it can't read it, because your baudrate is wrong, it needs a parity bit, but you don't send one, you send the wrong number of stopbits, your wordlenght is wrong, etc.

so if you want to communicate with com-devices, at first tell me the baudrate, the wordlength, paritybits and stopbits of your device. next i need to know the com-ic of your computer. if it is the ns16450 (and you won't need your program on other computers), you can directly do it per outportb. but if it should be compatible with other com-ics, you e. g. could use a .dll-file. i could send you a file called port.dll. but then you'll have to program in c++ and not in c anymore, because you can't include dlls in c.

so your "easy program" becomes very difficult, because of problems you never knew about, before sending your first bit to the com-port.
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

Umm, just a few things...

"i could send you a file called port.dll. but then you'll have to program in c++ and not in c anymore, because you can't include dlls in c."

Huh?  Since when?  I have no problems using DLLs in C...

You also need to know their version of Windows.

outportb() is a PROTECTED MODE instruction, and won't be available in Win2K or above - better to use CreateFile(0 etc.

Here's a tutorial on Win32 Serial Communications...


try this:
#include <stdlib.h>
#include <stdio.h>

int main(void)
   int number = 12345;
   char string[25];

   itoa(number, string, 10);
   printf("integer = %d string = %s\n", number, string);
   return 0;

Borland C++ 5.0 Programmierhandbuch
sorry - posted to wrong forum - simply ignore it - sorry
Um, what is this Mirko?  Is this for a different question?  This doesn't write a com port...
Oh, OK - thanks -I thought I was missing something! <grin>
"outportb() [...] won't be available in Win2K or above":

simply wrong! i programmed an interface with outportb. at my pc at home it worked with win2k and winxp, but it didn't work on a win98 and a dos 6.2.2 pc. but this wasn't the problem of the platform, but of the com-ic.

"I have no problems using DLLs in C"

sorry, if i told wrong, but perhaps could you tell me how? it was hard enough to me to find just a possiblity to include dlls and so i was so happy, that i didn't serch for other way after it and said to me: then it isn't possible. but thx for the information that it works and i'd be pleased, if you could tell me how.
Nope, I stand by my earlier comment.  See this comment from Borland themselves:


That said, it is possible to use outportb in NT. It requires a pseudo-device driver, which simply changes the privilege level of the program to ring 0, which makes it able to use the PROTECTED MODE instruction. How it does this? There is no shortcut way in NT unlike in Win9x. You ahve to create a device driver, then your program interacts with this device driver, effectively passing it your process information. The device driver being ring 0, will then change this process to a ring 0 privilege.

However, you will be going around the OS - never something that is recommended, or guaranteed to work and not have very negative affects on other software.

To use a DLL, include the .LIB file in your project and away you go...  If you need more help, post with some points <grin>
outportb: it simply works! i can't tell you why, but i works.

dlls in c: i don't have a .lib-file. i've got a the port.dll where commands like OPENCOM, CLOSECOM, DTR, RTS, TXD, TIMEINIT, TIMEREAD (and many others, but at the moment i only need them) are implemented. i did it that way:

// Variable pfad erhält den Namen der Dynamic Link Library:
const char *pfad = "port.dll";

// eingebunden:
int (__stdcall *funcOPENCOM)(const char *attr);
void (__stdcall *funcCLOSECOM)(void);
void (__stdcall *funcDTR)(int bit);
void (__stdcall *funcRTS)(int bit);
void (__stdcall *funcTXD)(int bit);
void (__stdcall *funcTIMEINIT)();
long (__stdcall *funcTIMEREAD)();

void main()
   // port.dll wird geladen:
     HINSTANCE hLib = LoadLibrary(pfad);
   // Funktionen OPENCOM... aus port.dll nach funcOPENCOM... im Programm verschieben:
   funcOPENCOM = (int(__stdcall*)(const char*)) GetProcAddress(hLib, "OPENCOM");
   funcCLOSECOM = (void(__stdcall*)(void)) GetProcAddress(hLib, "CLOSECOM");
   funcDTR = (void(__stdcall*)(int)) GetProcAddress(hLib, "DTR");
   funcRTS = (void(__stdcall*)(int)) GetProcAddress(hLib, "RTS");
   funcTXD = (void(__stdcall*)(int)) GetProcAddress(hLib, "TXD");

// using function DTR:
oh, sorry! perhaps i should translate the comments:

// Variable pfad erhdlt den Namen der Dynamic Link Library: = giving the variable "pfad" the name of the dll:

// eingebunden: = including functions OPENCOM...:

// port.dll wird geladen: = loading port.dll

 // Funktionen OPENCOM... aus port.dll nach funcOPENCOM... im Programm verschieben: = functions OPENCOM... from port.dll are pushed to funcOPENCOM... in my program:

now you can better understand it - sorry.

much thanks before!

but now i go to bed - it's 0.30 am in germany and tomorrow i've got to go to school and i'm so tired that i don't like translating anymore! oh dear!!!

ok - i'll look here again tomorrow, thx for help, cu@all

use hardware i/o outportb(); ouport(); inport(); inportb();
and stuff like this...
"outportb: it simply works! i can't tell you why, but i works."

I believe I've provided sufficient evidence, both by MSFT and by Borland, that indicates that these functions cannot reliably be used in the WinNT/2000/XP environment.

If you use them, you do so at your own risk, and I would certianly NOT recommend that someone developing new code use your approach.

Instead, implement such functionality in the manner recommended and supported by the manufacturers.
You check the follwing item
1. Your hardware is only serial port or it is having UART engine
2. The Firmware you use can define
   2.1 Baud rate
   2.2 Parity
   2.3 Start bit an dstop bit
3. Then in PC side just open the "hyper terminal" , and set the 2.1,2.2,2.3 as same as your hardware type the character , hyper teminal can only send the ascii character.

4. If your hardware have Then you can take serial port input (RX) as row data otehr wise you must chop start abd stop bit an dtake the balance

5. If you can able to send character but cannot send string then your hardware dosebut have enough buffer in you UART,

6. If such a case You must change your hardware in such a way you hard FIFO with UART which can send the data without taking MCU time.

Please free to talk to me if you need any more help
"You must change your hardware"

if you are too silly to program it - just change it and you'll be happy! what senseful comment!

@gj62: what risks do you mean? could my computer take fire, if i use the command??? if it was really so dangerous, it wouldn't be possible! you can't kill your system using such an easy command! that's unpossible! i think, that the worst thing would could happen is, that your program won't work. but i don't know why not trying it.

and by the way: i also gave another possibility with much more elegance using the port.dll.

could you please tell me how i can translate my program above to ansi c? would be thankful


With most programming tasks in Windows, the worst risk is that your software will have a bug that could cause Windows to hang.  Now, that may not be a big deal for some, but for others it could be a severe problem, resulting in data loss, etc.  Programming bugs have caused blackouts, stock market glitches, telephone outages costing millions of dollars, etc.

For that reason, and just general prudence, it is silly to recommend using a method that the manufacturers tell you is unsafe.

You'd have to post the code to determine what language extensions you may be using that are not in the ansi standard (Borland has *alot* of them).
are we trying to help a commercial software-firm or a private person? if he needs it for commercial reasons, he simply is at the wrong place here, because i don't give him my information for that he can earn money with it. so i think that he doesn't loose millions of dollars when his computer hangs.

"the worst risk is that your software will have a bug that could cause Windows to hang": there are so many bugs in windows itself and also in the programs for windows, so one more or less doesn't matter. if your computer mustn't hang, why do you choose windows??? and i garantue you that my problem doesn't make windows hanging!!!

and when my program wanted to do that, it would be too late, because, when it tried, windows will have already hanged itself.

i don't like repeating what i said one day ago, but "i also gave another possibility with much more elegance using the port.dll."

Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by gj62.


EE Cleanup Volunteer

Featured Post

Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now