Author Topic: using COM port in WinXP  (Read 30476 times)

El Rakone

  • Member
  • ***
  • Posts: 9
using COM port in WinXP
« on: September 15, 2010, 10:40:24 am »
Hello,

I am trying to communicate with a device which is connected thru a serial port (RS-232). I am doing this by writing a C program in which I used a  "bioscom" command for this purpose, but as it is a command which addresses hardware directly, it is not applicable in the WinXP environment.

Can anyone give my some advice/hint/pointers on how to solve this?

I also ran across an application "UserPort" which is supposed to emulate DOS or smth like that in XP, still haven't tried it out, I was hoping there is a way to do this directly in XP with some other command.

Thanks in advance,

El Rakone

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: using COM port in WinXP
« Reply #1 on: September 17, 2010, 07:05:11 am »
Umm are trying for a Console/DOS based program or a Windows one, what C compiler are you using.  I sounds like bioscom was written using Borland Turbo C++ rather than an a newer (more recent) compiler that uses the "proper" method.  I have used Microsoft C#'s Serial Port Class that does things "properly".
Got go for a lunch meeting

El Rakone

  • Member
  • ***
  • Posts: 9
Re: using COM port in WinXP
« Reply #2 on: September 17, 2010, 07:52:37 am »


Well, I just got the most ordinary compiler I could find  - Lcc, but I have Turbo C also.
It makes no difference, but I'm gonna go for the console solution
I'm not familiar with c++ and c# so I'm sticking to C :)

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: using COM port in WinXP
« Reply #3 on: September 17, 2010, 08:45:13 am »
Hi,

Sorry about that but had to go to Lunch (Client, Boss paying!). Umm just for the sake of being up to date you really ought to think about using a more up to date complier Borland C++ Builder 5 was free but with out an IDE.  The problem I think you are finding is the Borland command "pokeb" & "peek" use the direct memory access that is prevented by the NT frame work to make life "easier" for Motherboad designers and others.  Windows operates in a memory bubble to make things stable (prevention of memory errors, crashes and the like). The problem is to use a piece of software "to poke a hole" in the HAL (not "open the Pod bay doors" but Hardware Abstration Layer) and allow comms.
Post Windows 98 I have used VB, VB.NET, C#, Borland Builder 6 and others all Windows based and either the .NET serial port class or an ActiveX (MSComm 16 & MSComm32.ocx & MHComm32.ocx) so if you are using Windows XP I would download Microsofts C# Express edition and use Jans example code or Serial Port Complete 2nd Edition to try interfacing to real world!

Glenn
(C# is affectively C/Java you can as an ANSI C programmer make sense of it with a few exceptions)  
The DOS based or Console project will allow you to to do the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
//serial port demo  17/9/2010 14:20
namespace ConsoleApplication2
{
    class Program
    {

        static void Main(string[] args)
        {
            SerialPort myComPort = new SerialPort();
            Console.WriteLine("DOS Serial Port With C#");
            Console.ReadLine();  //hit return
            myComPort.PortName = "COM1";
            myComPort.BaudRate = 9600;
            myComPort.Parity = Parity.None;
            myComPort.DataBits = 8;
            myComPort.StopBits = StopBits.One;
            myComPort.Handshake = Handshake.None;
            // to send data set up calls functions
            // and use myComPort.Write("hello"); or myComPort.WriteLine("world!");
            //for reading either use a timer or an interrupt handler and use
            //String recievedText; and recievedText = myComPort.ReadLine();

        }
    }
}
« Last Edit: September 17, 2010, 09:26:05 am by GlennP »

El Rakone

  • Member
  • ***
  • Posts: 9
Re: using COM port in WinXP
« Reply #4 on: September 17, 2010, 09:06:16 am »
Does changing the compiler make such a difference?
To be frank, I didn't quite understand the part with .NET and ActiveX, but can you give me your opinion on this site which I came across http://msdn.microsoft.com/en-us/library/ms810467. I've also been browsing thru some forums, and people often mention this "CreateFile" function for accessing serial ports under Windows..

Thanks,

Rakone



GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: using COM port in WinXP
« Reply #5 on: September 17, 2010, 09:39:25 am »
Oops, I appear to have been editing my reply while you were editing it. Have another look.  Okay I take it you are a hardware guy (me too!) Windows allows pre-written tools to be used in Visual Programming (such as VB, Delphi etc) These are effectively extra's like headers in C (stdlib.h etc) the Active X approach is easier to implement as more people have used it and so on. About the method you have found I have tried something similar but found it over complicated (I think one or two packet managed to get reversed) it may well cause the data to be delayed slightly.

Glenn 

El Rakone

  • Member
  • ***
  • Posts: 9
Re: using COM port in WinXP
« Reply #6 on: September 17, 2010, 09:49:07 am »
Uh.. Is there a simpler way? :/
I just need to be able to send a command like "#3 P1000 T1000" to a robotic hand which is connected thru COM1, so I'm writing a C program which will convert keyboard inputs to those commands..
Are there any C specialized functions for connecting, or it has to be in C#

Thanks,

Rakone

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: using COM port in WinXP
« Reply #7 on: September 17, 2010, 10:18:19 am »
Umm well no.  I just suggested C# as it has all the things you need built in, its C compliant. Also being that it is windows based it is easier for users to use.  If you are using it for control which by the looks of it you are there are C ways and means of doing it, I'm afraid I've not really used non-visual programming means post Win95/98.  I'm assuming you have some sort of driver to defeat the HAL.  Sadly Microsoft in there infinite wisdom took away direct port access (unless you can afford time to study Windows Driver Reference) in NT. There must be a way of doing it I just never had the time and went the Visual Basic 6 route. I started to use C# because of all the Micro controlller programming that I have to do in C. The example code I wrote above can have a fuction added
Move_Arm()
{
     myComPort.WriteLine("#3 P1000 T1000");
}
(this will work if the robot arm is expecting a string if it individual bits, let me think...)

once you get over the using instead of a # it's pretty C like!

Glenn








-    

    
« Last Edit: September 17, 2010, 10:27:19 am by GlennP »

El Rakone

  • Member
  • ***
  • Posts: 9
Re: using COM port in WinXP
« Reply #8 on: September 17, 2010, 10:25:53 am »
Ok, well, I'll go over the C method I've shown you in the link during the weekend , and if there's no use, I'll switch to these C# commands.
Thanks a lot Glenn, I'll probably bore you again from Monday :D

have a nice weekend! :)

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: using COM port in WinXP
« Reply #9 on: September 17, 2010, 10:33:46 am »
Good luck to you! if you have any luck let me know. Can I ask is it a uni Assignment or a Job thing.  I only ask as Uni they don't make such a fuss of using Freeware/Express editions where as if its for sale and an Express edition is used you might have some questions to answer.

Glenn
(still waiting for my R2 unit!)

El Rakone

  • Member
  • ***
  • Posts: 9
Re: using COM port in WinXP
« Reply #10 on: September 17, 2010, 10:41:44 am »
Well, I'm a student, and it's like a student practice at an Institute. This is supposed to be simple, because afterward it's gonna be controlled with a PC-104
 ;)

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: using COM port in WinXP
« Reply #11 on: September 17, 2010, 11:50:41 am »
Okay, Whose PC104 if it's a big manufacturer you might be lucky and they could supply the tools and away (fire up the Web browser).  Also if its a PC104 some of the newer one it might well support the .NET frame work which could make your life a whole lot eaiser. The version of controller you are using is kind of important hopefully some version of WinCE or WindowsXP embedded Serial support is poor but it works (kinda).  Also is this going to run from a PC interfacing to the card or just the card in standalone (I was thinking you were after a PC interface which led me to recommend C#).  There should be available on the manufactures websites enough to get a system up and running in a quick and dirty method

Glenn    
« Last Edit: September 17, 2010, 12:19:04 pm by GlennP »

El Rakone

  • Member
  • ***
  • Posts: 9
Re: using COM port in WinXP
« Reply #12 on: October 06, 2010, 05:34:02 am »
Well, in the end, the C code that did the job.. for future reference :)

Code: [Select]
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

HANDLE hSerial;
LPCWSTR Port = L"COM1";
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};

DWORD myBytesWrite = 0;


char Home[]= "#0P1500T1000#1P1300T1500#2P1400T1000#3P1700T1000#4P1400T1000#5P1400T1000\r";
int P0 = 1500, P1 = 1300, P2 = 1400, P3 = 1700, P4 = 1400, P5 = 1400;

char a ;
int i;

//int WriteRS232 (char *szBuff);                        // you can also write to the port using a separate function


///////////////////////////////////////////////////// 
//MAIN
int main( void )
{


    hSerial = CreateFile( Port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 
    if (hSerial == INVALID_HANDLE_VALUE)
        {
            //  Reports an error if HANDLE was unsuccessful
           printf ("CreateFile failed with error %d.\n", GetLastError());
           return (1);
        }

   
    // Setting DCB

        ZeroMemory( &dcbSerialParams, sizeof(dcbSerialParams) );

        dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
        dcbSerialParams.BaudRate = CBR_115200;
        dcbSerialParams.ByteSize = 8;
        dcbSerialParams.StopBits = ONESTOPBIT;
        dcbSerialParams.Parity = NOPARITY;

        if(!SetCommState(hSerial, &dcbSerialParams))
            {
            //  Reports an error if DCB parameters weren't set successfully
                printf ("SetCommState failed with error %d.\n", GetLastError());
                return (1);
            }

    //Setting Timeouts

        timeouts.ReadIntervalTimeout = 100;
        timeouts.ReadTotalTimeoutConstant = 100;
        timeouts.ReadTotalTimeoutMultiplier = 20;

        timeouts.WriteTotalTimeoutConstant = 100;
        timeouts.WriteTotalTimeoutMultiplier = 20;

        if(!SetCommTimeouts(hSerial, &timeouts))
            {
            //  Reports an error if Timeout parameters weren't set successfully
               printf ("SetCommTimeouts failed with error %d.\n", GetLastError());
               return (1);
            }

/// some other code 
       
        // writing to the port


// if not using a separate funtion WriteRS232(), you can write the home position as following

       strcpy(szBuff, Home);

        i = strlen(szBuff);

        WriteFile(hSerial, szBuff, i, &myBytesWrite, NULL);

       CloseHandle(hSerial);



Well, this is basically it. I deleted a portion of code which was used for determining which key was pressed etc. But this is the important part. An explanation for each function used can be found on the MSDN site

Cheers!

El Rakone

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: using COM port in WinXP
« Reply #13 on: October 19, 2010, 07:12:45 am »
Well, thats good plain run O' the mill C does it again!  I would be interested to know if you have found any data reversal, mine I think was due to a stupidly high speed I was forced to use!
Glenn

El Rakone

  • Member
  • ***
  • Posts: 9
Re: using COM port in WinXP
« Reply #14 on: October 19, 2010, 05:34:16 pm »
What do you mean by data reversal?