how to write C code to use RS232 (com port)


short ULR http://wp.me/ph3BR-iE

 

how to write the code to use RS232 (com port)

1) setup MinGW, free C++ compiler for windows, 下載 MinGW 從這裡 http://sourceforge.net/projects/mingw/files/

2) open dosbox.bat
3) under this dosbox (like terminal), open set_path (one time only, until close this dosbox)
4) open compile.bat

result-
rs232.exe will be generated.

main.c


//--------------------------- main.c
//JUL-30-2011, xiaolaba

#include <stdio.h>; //wordpress 自動去除 < > 及內容, 解決辦法, 前後加空格
#include <stdlib.h>;

#include "rs232.h" //wordpress 自動轉換雙引號為 半角符號, 無法解決
//原因參考以下
//http://farlee.info/archives/wordpress-format-quote-mark.html

//測試用, 打開 com port, 發送3byte資料, 關閉 com port

int main()
{
OpenComport(4, 9600); //win32/ win64 底下有問題, 需要改library
SendByte(3, 'S');
char i;

for(i=0x30;i&lt;0x40;i++){
SendByte(3, i);
}

CloseComport(4);
}

//--------------------------- main.c

rs232.h


/*
***************************************************************************
* rs232.h
* Author: Teunis van Beelen
*
* Copyright (C) 2005, 2006, 2007, 2008, 2009 Teunis van Beelen
*
* teuniz@gmail.com
*
***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
***************************************************************************
*
* This version of GPL is at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*
***************************************************************************
*/

#ifndef rs232_INCLUDED
#define rs232_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <string.h>

#ifdef __linux__

#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>

#else

#include <windows.h>

#endif

int OpenComport(int, int);
int PollComport(int, unsigned char *, int);
int SendByte(int, unsigned char);
int SendBuf(int, unsigned char *, int);
void CloseComport(int);
void cprintf(int, const char *);
int IsCTSEnabled(int);

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif

dosbox.bat

rem //-------------
rem dosbox.bat
rem //-------------

cmd

set_path.bat

rem //-----------------
rem // set_path.bat
rem //-----------------

set path=c:\MinGW\bin;%path%

compile.bat

rem //-----------------
rem compile.bat
rem //-----------------
rem 如果不用 -static 選項, Windows 環境, 必須提供
rem path = c:\MinGW\bin 或
rem 複製 libgcc_s_dw2-1.dll 到本資料夾
rem JUL/31/2011, xiaolaba

rem 用Gcc, 沒有GDB 除錯資料的編譯
gcc  -static -o RS232.exe main.c RS232.c

rem 用G++, 包含GDB 除錯資料的編譯
g++ -g -static -o RS232.exe main.c RS232.c

ref:
open source for RS232, com port control library
http://www.teuniz.net/RS-232/

how to use gcc and without uses dynamic linked library
http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

how to setup compiler for gcc in Windows enviroment
http://shunyuan-chou.blogspot.com/2010/02/2-mingw-windows-gnu.html

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s