Solved

Reading & Writing HEX to serial port ( DOS based )

Posted on 2004-09-28
11
2,704 Views
Last Modified: 2013-11-15
Hello. I want to write a dos-based ( command line ) program that can send & receive Hex data using serial port. For information I use borland C in Windows 98 environment.  I am quite novice in C programming & really hope to get assistance in completing this task.
0
Comment
Question by:eazrul
[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
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 6

Expert Comment

by:Amritpal Singh
ID: 12179769
plz have a look at the following links
good tutorials
www.funducode.com/freec/ Hardware/new_hi_c03/Article03.htm

www.funducode.com/freec/ Hardware/new_hi_c02/Article02.htm

www.funducode.com/freec/ Hardware/new_hi_c04/Article04.htm

i hope they'll be helpful
BOL
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12180006
Do you mean hex data or binary data, both are different thing first needs the twice space than the second.
Here you will find some snippets for DOS serial programming in C (in .zip format):
http://c.snippets.org/
0
 

Author Comment

by:eazrul
ID: 12187848
Actually, the application purpose is to establish full-duplex communication between PC & external device ( DVD Recorder ). The PC send a command in Hex format through serial port to this external device & then the external device will receive the command & reply with return value ( also in Hex format ). Both command & return value will be displayed. Is anybody out there can help?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12189170
Do you know the protocol, have you any code portion to help you inserting proper routines?
0
 
LVL 6

Expert Comment

by:Amritpal Singh
ID: 12189594
0
 

Accepted Solution

by:
samwaters earned 250 total points
ID: 12195802
try this:
/*
Com1 = 0x3F8
Com2 = 0x3E8
Com3 = 0x2F8
Com4 = 0x2E8
*/

#include <stdio.h>
#include <conio.h>

#define serial 0x3F8

void main()
{
  int invalue=0, outvalue=0;
  outvalue = 0x10; /*HEX value to send*/
  outportb(serial, outvalue); /*Output to the serial port*/
  printf("Sent %x to the serial port\n", outvalue);
  delay(20); /*Give the device time to respond*/
  invalue = inportb(serial); /*Read from serial port*/
  printf("Received %x from the serial port\n", invalue);
}

Of course, this will just send one byte (0x10 in that example) to the serial port and read the reply.
To communicate with the device, you will need to know the protocol it uses, as jaime said.
0
 

Expert Comment

by:samwaters
ID: 12195827
That example also uses Com1. If your device is on another port, change the #define serial 0x3F8 line accordingly (I gave you the 4 Com port addresses).
0
 

Author Comment

by:eazrul
ID: 12196051
Maybe I am wrong in my previous comment, sorry. Actually for communication protocol it will use COM1 at 38400,N,8,1 ( Non-parity, 1 stop bit ) & the data that need to be sent is in ASCII. For example :

SEND : "C 2"/ODELIM:0x0D,0x0A ( i.e. send "C 2" - C-space-2 and followed by "ENTER"
and the external device will reply with :
RECEIVE :  "[OK]" ( i.e character OK ).

Hopefully you guys can help me.
0
 

Expert Comment

by:samwaters
ID: 12236782
Looks like you want to pass a string the port to me.
However, outportb can't deal with strings on it's own - it takes a byte, so you have to break it up.
I'd use a function to do this:

void SendData(char data[]) /*Takes a string parameter*/
{
    char dat='A'; /*Random character to initialise - do NOT use 0 here*/
    int i=0;
    while(dat != '\0') /*Look for string termination character*/
    {
        dat=data[i]; /*Get character from string array*/
        outportb(serial, dat); /*Send to port*/
       i++;
    }
    outportb(serial, '\n'); /*Enter*/
}

Then you can say from any of your functions:
SendData("C 2");
And it'll send that to the serial port, followed by enter.
0
 

Expert Comment

by:samwaters
ID: 12236892
To set the parity, speed of the port, stop bits etc use this:
Call this function FIRST - before you send ANY data at all.

#define serial 0x3F8 /*This should be at the very top of the program*/

void SetupPort()
{
    outportb(serial + 1, 0); /*Turn off interrupts for the port so Windows and DOS won't interfere*/
    outportb(serial + 3, 0x80); /*Turn on DLAB (Divisor Latch Access Bit)*/
    outportb(serial + 0, 0x03); /*Set baud rate to 38400*/
    outportb(serial + 1, 0x00); /*Set baud rate again*/
    outportb(serial + 3, 0x03); /*No parity, 8 Bits, 1 stop bit*/
    outportb(serial + 2, 0xC7); /*FIFO Control Register*/
    outportb(serial + 4, 0x0B); /*DTR, RTS, OUT2*/
}

You don't need to worry about what most of this does, just copy it and it should work fine.
Call SetupPort() from main() before you do anything else.
Only thing you might want to change is the baud rate - different speeds are:

0x01 = 115,200 BPS
0x02 =  57,600 BPS
0x03 =  38,400 BPS
0x06 =  19,200 BPS
0x0C =   9,600 BPS
0x18 =   4,800 BPS
0x30 =   2,400 BPS

So just change the 0x03 in this line to the desired HEX value.
outportb(serial + 0, 0x03); /*Set baud rate to 38400*/
0
 

Expert Comment

by:samwaters
ID: 12236940
You can check to see if anything's been received (new) to the port like this:

char c,d;
c = inportb(serial + 5);
if(c & 1) /*Yes it has*/
{
    d = inportb(serial); /*get what's been received*/
    /*Do whatever else we need with it*/
}
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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