NO

Author Topic: Comm Port  (Read 24759 times)

0gmios

  • Guest
Comm Port
« on: April 22, 2008, 05:43:24 AM »
Greetings,

I have been using Pelles C with Xp for the last 4 years to interface with electronics via the printer port and an interface card (http://www.ultrasmart.org/CTLplus3_mLogicDS-K2805.html). We just upgraded to a new USB interface (http://www.starting-point-systems.com/) and it is controlled by sending text strings to the comm port (USB).

I have done some searching and found that using the comm port has to be done with asm (which I have not used but happy to learn). I was hoping someone could 'point' (;)) to some code to help me make a function. The function will need to be called with the text as the argument, which will then be sent to the comm port.

Another idea I had was to run a batch file from the C exe. The batch file would contain the relevant communications commands. However, I have no idea how to do this.

Regards,

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Comm Port
« Reply #1 on: April 22, 2008, 07:39:12 AM »
The COM port will be handled like an ordinary file on Windows, see the CreateFile API function.
Be sure to suppress the end-of-line characters (CRLF), if the manual for the interface does not say that they are needed.

There is no need for ASM, plain white C will do ;)
---
Stefan

Proud member of the UltraDefrag Development Team

0gmios

  • Guest
Re: Comm Port
« Reply #2 on: April 22, 2008, 07:51:17 AM »
I see API and I am thinking windows API, as in a windows application. I am just going for a simple console application...  ???

Regards,

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Comm Port
« Reply #3 on: April 22, 2008, 08:54:12 AM »
There is no difference, if it is a GUI or console application.
You do not need a GUI to use the API functions.

You should be able to use the build-in file functions, since the COM ports are special files, like the file name COM1 will open a connection to COM1.
---
Stefan

Proud member of the UltraDefrag Development Team

0gmios

  • Guest
Re: Comm Port
« Reply #4 on: April 22, 2008, 10:08:00 AM »
I guess I will have to relearn file handling. I haven't done file handling since the day I was shown how...  ;D

At least I have a direction, just need to make sure I call the file COM3, since that is the comm port it was assigned.

Now were is my K&R...

Regards,

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Comm Port
« Reply #5 on: April 22, 2008, 05:20:36 PM »
Have you considered using VISA?

I have used both Agilent VISA and NI VISA and find that they make IO fairly easy.

I am using VISA now for direct USB communications and also for RS232.  The Agilent Developers Network has several C based examples implementing VISA and you might get going a bit faster than using the Windows API.

I started working on a Windows API wrapper for RS232 in my spare time but havn't been able to get to it in a while.

David M.
No one cares how much you know,
until they know how much you care.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Comm Port
« Reply #6 on: April 22, 2008, 08:00:24 PM »
Open COM port something like this:
Code: [Select]
hCom = OpenComPort("COM1");
Code: [Select]
HANDLE OpenComPort(char *szPort)
{
HWND hWnd;
HANDLE hCom;
DCB dcb;
COMMTIMEOUTS cto;
BOOL bOk;
char szTmp[80];

if (!szPort) return 0;
hWnd = hDlg;

hCom = CreateFile(szPort, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
if ( hCom == (HANDLE)-1 ) {
wsprintf(szTmp, "Error opening port %s", szPort);
MessageBox( hWnd, szTmp, szAppName, MB_OK|MB_ICONERROR );
return 0;
}
bOk = GetCommState(hCom, &dcb);
if ( !bOk ) {
CloseHandle(hCom);
wsprintf(szTmp, "Error reading port %s state", szPort);
MessageBox( hWnd, szTmp, szAppName, MB_OK|MB_ICONERROR );
return 0;
}
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
bOk = SetCommState(hCom, &dcb);
if ( !bOk ) {
CloseHandle(hCom);
wsprintf(szTmp, "Error writing port %s state", szPort);
MessageBox( hWnd, szTmp, szAppName, MB_OK|MB_ICONERROR );
return 0;
}
cto.ReadIntervalTimeout = 100;
cto.ReadTotalTimeoutMultiplier = 10;
cto.ReadTotalTimeoutConstant = 1000;
cto.WriteTotalTimeoutMultiplier = 500;
cto.WriteTotalTimeoutConstant = 1000;
bOk = SetCommTimeouts(hCom, &cto);
return hCom;
}

and use ReadFile and WriteFile functions.

finally close port like this:
Code: [Select]
if (hCom) CloseHandle(hCom);
May the source be with you

0gmios

  • Guest
Re: Comm Port
« Reply #7 on: April 23, 2008, 02:51:10 AM »
Have you considered using VISA?

I have used both Agilent VISA and NI VISA and find that they make IO fairly easy.

I am using VISA now for direct USB communications and also for RS232.  The Agilent Developers Network has several C based examples implementing VISA and you might get going a bit faster than using the Windows API.

Hmm, sounds interesting. More information please...

Thanks,

0gmios

  • Guest
Re: Comm Port
« Reply #8 on: April 23, 2008, 03:09:41 AM »
Open COM port something like this:

At the risk of sounding foolish, I would like to say that dcb and cto appear to be objects... As in C++, not C...  ???

Then again, I didn't say that I want to write a C console program, so my bad.

Not that I am opposed to C++ just found a good book (like K&R) for it.

Regards,

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Comm Port
« Reply #9 on: April 23, 2008, 04:04:43 AM »
Quote
Hmm, sounds interesting. More information please...

Here are some links:

C code samples:
http://adn.tm.agilent.com/index.cgi?CONTENT_ID=2908

A good article: (C#.NET but VISA is C based and so you can see the technique)
http://www.ddj.com/windows/184405902

The download:
http://joule.ni.com/nidu/cds/fn/p/sn/n23:3.1637.1640/lang/en

This includes NI SPY which allows you to view input / output bus activity of devices making use of VISA.

The Manuals:
http://digital.ni.com/manuals.nsf/websearch/266526277DFF74F786256ADC0065C50C
http://digital.ni.com/manuals.nsf/websearch/87E52268CF9ACCEE86256D0F006E860D

Regards,
DMac
No one cares how much you know,
until they know how much you care.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Comm Port
« Reply #10 on: April 23, 2008, 04:18:04 AM »
At the risk of sounding foolish, I would like to say that dcb and cto appear to be objects... As in C++, not C...  ???
They are structs and not objects.
You may want to spend some time on MSDN checking out the definitions of the Windows API functions used in the example.

Since Pelles C is an IDE for writing Windows applications there is no way around MSDN.
---
Stefan

Proud member of the UltraDefrag Development Team

0gmios

  • Guest
Re: Comm Port
« Reply #11 on: April 23, 2008, 05:31:46 AM »
You may want to spend some time on MSDN checking out the definitions of the Windows API functions used in the example.

Since Pelles C is an IDE for writing Windows applications there is no way around MSDN.

 :'(

0gmios

  • Guest
Re: Comm Port
« Reply #12 on: April 23, 2008, 08:21:44 AM »
Okay, windows API and MSDN it is... I just ordered "The Windows Serial Port Programming Handbook" as a reference (which I am reading through google books). That way I can get my hands on some complete example code.

I don't do well with snippits. For example,

Code: [Select]
hCom = OpenComPort("COM1");
what is type is hCom...

Hopefully the book will help.

Regards,

PS The VISA download link doesn't work for me, I get a failure message...

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Comm Port
« Reply #13 on: April 23, 2008, 10:40:48 AM »
Quote
what is type is hCom...
HANDLE
OpenComPort() is example home made function for opening COM port, not Win32 API function.
Example code was below that usage example.
Code: [Select]
HANDLE OpenComPort(char *szPort);
May the source be with you

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Comm Port
« Reply #14 on: April 23, 2008, 05:10:42 PM »
Quote
PS The VISA download link doesn't work for me, I get a failure message...

Try this:
http://ftp.ni.com/support/softlib/visa/NI-VISA/4.3/win32/visa430full.exe
No one cares how much you know,
until they know how much you care.