Solved

Reading & Writing HEX to serial port ( DOS based )

Posted on 2004-09-28
11
2,625 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

This article shows how to convert a multi-page PDF file into multiple image files, with one image file created for each page of the PDF. It does this by utilizing an excellent, free software package called GraphicsMagick. The solution is amazingly s…
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now