?
Solved

Sending a string through the com port

Posted on 2003-03-24
20
Medium Priority
?
647 Views
Last Modified: 2011-09-20
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.

Thanks
0
Comment
Question by:Fischjm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
20 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8198165
What version of Windows are you on?
0
 
LVL 6

Accepted Solution

by:
gj62 earned 260 total points
ID: 8198198
0
 

Expert Comment

by:mirko_germany
ID: 8198519
"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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:gj62
ID: 8198587
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...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp


0
 

Expert Comment

by:mirko_germany
ID: 8198640
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
0
 

Expert Comment

by:mirko_germany
ID: 8198645
sorry - posted to wrong forum - simply ignore it - sorry
0
 
LVL 6

Expert Comment

by:gj62
ID: 8198655
Um, what is this Mirko?  Is this for a different question?  This doesn't write a com port...
0
 
LVL 6

Expert Comment

by:gj62
ID: 8198659
Oh, OK - thanks -I thought I was missing something! <grin>
0
 

Expert Comment

by:mirko_germany
ID: 8198696
"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.
0
 
LVL 6

Expert Comment

by:gj62
ID: 8198794
Nope, I stand by my earlier comment.  See this comment from Borland themselves:

http://bdn.borland.com/article/0,1410,21372,00.html

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>
0
 

Expert Comment

by:mirko_germany
ID: 8198951
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";

// Funktionen OPENCOM, CLOSECOM, DTR, RTS, TXD, TIMEINIT und TIMEREAD werden
// 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:
funcDTR(1);
}
0
 

Expert Comment

by:mirko_germany
ID: 8199000
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:

// Funktionen OPENCOM, CLOSECOM, DTR, RTS, TXD, TIMEINIT und TIMEREAD werden
// 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

mirko
0
 

Expert Comment

by:Getch
ID: 8202285
use hardware i/o outportb(); ouport(); inport(); inportb();
and stuff like this...
0
 
LVL 6

Expert Comment

by:gj62
ID: 8204224
"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.
0
 

Expert Comment

by:Terrance
ID: 8208157
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
0
 

Expert Comment

by:mirko_germany
ID: 8219307
"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

mirko
0
 
LVL 6

Expert Comment

by:gj62
ID: 8221279
Mirko,

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).
0
 

Expert Comment

by:mirko_germany
ID: 8226538
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."

mirko
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10190942
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question