943,865 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 13660
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 23rd, 2007
1

Re: Serial communication using DEV CPP IDE 4.9.9.2

If you must write a program for the PC side under XP. You can use the SerialPort class in .NET. There's lots of information on the net about using VB for uCs. You don't have to use win32.
http://users.tpg.com.au/gramo/Site/rs232.htm
Last edited by Colin Mac; Dec 23rd, 2007 at 9:10 am.
Reputation Points: 78
Solved Threads: 22
Posting Whiz
Colin Mac is offline Offline
327 posts
since Sep 2006
Dec 23rd, 2007
0

Re: Serial communication using DEV CPP IDE 4.9.9.2

In case of VB, try searching for MSComm controls on google.
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Dec 27th, 2007
0

Re: Serial communication using DEV CPP IDE 4.9.9.2

First of all, the mistake you have been doing is using Turbo C compiler and getting confused between code written for MS-DOS and Windows OS. STOP using your Turbo C compile and just focus on things which are more standard.

>I want to know why the above code i mentioned is getting compiled in TC but not DEV CPP IDE >and how to make it run in DEV CPP IDE 4.9.9.2
You can already see from the error message thrown by the compile on Dev-C++. You have been using some non standard library which was available under TC. But, Dev-C++ doest support those libraries what so ever.

And now explain us, why do you need this code. What is the main purpose of writing this code? What I understand from your post is that, you trying to use this program send and receive data between your microcontroller and your PC. If that was your main intention, there is already inbuilt terminal emulator "HyperTerminal" which does that for you. Where you can transfer your binary to your MController.

If that wasn't your intention, Can you explain you what are you trying to do?

And on the other hand, have a look at the links which I posted in my previous post, there is an article on how to program a serial port for Windows. Its a good Article which was taken from MSDN. If you follow that you will be able to achieve something on Dev-C++.

And start thinking STANDARDS.

ssharish
I dont want to use hypertermiaml..I want to have my own standard window where I can send and receive.I want to add more functionality to this and hyperterminal window will not serve that.I am using Dev C++ 4.9.9.2 .The above code was working only in Turbo C compiler.
Reputation Points: 23
Solved Threads: 0
Light Poster
danibootstrap is offline Offline
46 posts
since Dec 2007
Dec 27th, 2007
0

Re: Serial communication using DEV CPP IDE 4.9.9.2

Quote ...
I am using Dev C++ 4.9.9.2 .The above code was working only in Turbo C compiler.
It is obvious from the earlier posts. Try AD's suggestions..
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Jan 14th, 2008
0

Re: Serial communication using DEV CPP IDE 4.9.9.2

The following code (.cpp for DevCpp, Windows) is for sending characters out from the serial port (com1 ,9600,no parity ,one stop bit).
But I am not getting any output from the serial port.As of now I am sending the char 'a' out but I am not observing it.Can anyone help me rectify the code.

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <windows.h>
  6.  
  7. #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
  8. #include <bios.h>
  9. //#include <afxwin.h> // serial.cpp : Defines the entry point for the console application.
  10.  
  11. //#include "stdafx.h"
  12. #include <string.h>
  13. #include "serial.h"
  14.  
  15. // Flow control flags
  16.  
  17. #define FC_DTRDSR 0x01
  18. #define FC_RTSCTS 0x02
  19. #define FC_XONXOFF 0x04
  20.  
  21. // ascii definitions
  22.  
  23. #define ASCII_BEL 0x07
  24. #define ASCII_BS 0x08
  25. #define ASCII_LF 0x0A
  26. #define ASCII_CR 0x0D
  27. #define ASCII_XON 0x11
  28. #define ASCII_XOFF 0x13
  29. using namespace std;
  30. // variables used with the com port
  31. BOOL bPortReady;
  32. DCB dcb;
  33. COMMTIMEOUTS CommTimeouts;
  34. BOOL bWriteRC;
  35. BOOL bReadRC;
  36. DWORD iBytesWritten;
  37. DWORD iBytesRead;
  38.  
  39. HANDLE SerialInit(char *ComPortName, int BaudRate)
  40. {
  41. HANDLE hComm;
  42.  
  43. hComm = CreateFile(ComPortName,
  44. GENERIC_READ | GENERIC_WRITE,
  45. 0, // exclusive access
  46. NULL, // no security
  47. OPEN_EXISTING,
  48. 0, // no overlapped I/O
  49. NULL); // null template
  50.  
  51. bPortReady = SetupComm(hComm, 1, 128); // set buffer sizes
  52.  
  53.  
  54. bPortReady = GetCommState(hComm, &dcb);
  55. dcb.BaudRate = BaudRate;
  56. dcb.ByteSize = 8;
  57. dcb.Parity = NOPARITY;
  58. //dcb.Parity = EVENPARITY;
  59. dcb.StopBits = ONESTOPBIT;
  60. dcb.fAbortOnError = TRUE;
  61.  
  62. // set XON/XOFF
  63. dcb.fOutX = FALSE; // XON/XOFF off for transmit
  64. dcb.fInX = FALSE; // XON/XOFF off for receive
  65. // set RTSCTS
  66. dcb.fOutxCtsFlow = TRUE; // turn on CTS flow control
  67. dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; //
  68. // set DSRDTR
  69. dcb.fOutxDsrFlow = FALSE; // turn on DSR flow control
  70. dcb.fDtrControl = DTR_CONTROL_ENABLE; //
  71. dcb.fDtrControl = DTR_CONTROL_DISABLE; //
  72. dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; //
  73.  
  74. bPortReady = SetCommState(hComm, &dcb);
  75.  
  76. // Communication timeouts are optional
  77.  
  78. bPortReady = GetCommTimeouts (hComm, &CommTimeouts);
  79.  
  80. CommTimeouts.ReadIntervalTimeout = 5;
  81. CommTimeouts.ReadTotalTimeoutConstant = 5;
  82. CommTimeouts.ReadTotalTimeoutMultiplier = 1;
  83. CommTimeouts.WriteTotalTimeoutConstant = 5;
  84. CommTimeouts.WriteTotalTimeoutMultiplier = 1;
  85.  
  86. bPortReady = SetCommTimeouts (hComm, &CommTimeouts);
  87.  
  88. return hComm;
  89. }
  90.  
  91. char SerialGetc(HANDLE *hComm)
  92. {
  93. char rxchar;
  94. BOOL bReadRC;
  95. static DWORD iBytesRead;
  96.  
  97. bReadRC = ReadFile(*hComm, &rxchar, 1, &iBytesRead, NULL);
  98.  
  99. return rxchar;
  100. }
  101.  
  102. void SerialPutc(HANDLE *hComm, char txchar)
  103. {
  104. BOOL bWriteRC;
  105. static DWORD iBytesWritten;
  106.  
  107. bWriteRC = WriteFile(*hComm, &txchar, 1, &iBytesWritten,NULL);
  108.  
  109. return;
  110. }
  111.  
  112. int main()
  113. {
  114. HANDLE my=SerialInit("com1",9600);
  115. char letter;
  116. printf("ranjan\n");
  117.  
  118. HANDLE *ptr;
  119. *ptr=my;
  120. for( ; ; )
  121. {
  122.  
  123. SerialPutc(ptr,'a');
  124.  
  125. }
  126. getch();
  127.  
  128. return 0;
  129.  
  130. }
Last edited by WolfPack; Jan 14th, 2008 at 6:11 am. Reason: ADDED [CODE][/CODE] TAGS.
Reputation Points: 23
Solved Threads: 0
Light Poster
danibootstrap is offline Offline
46 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Hiding names in static libraries
Next Thread in C Forum Timeline: About /proc/stat ???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC