⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serial.c

📁 OBD软件给Palm平台系统所使用范例程式.
💻 C
字号:
// MobyPalm
// Copyright (C) 2005 Olivier Burgard

// 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; either version 2
// the License, or (at your option) any later version.

// 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#include <PalmOS.h>
#include <SerialMgr.h>
#include "Serial.h"
#include "Common.h"

UInt16 portId; 
Char szError[128];
Boolean serPortOpened = false; 

BOOL SerialOpen(int baud) {
	Err err;
	Int16 paramSize;
	UInt32 flags;
		
	MemSet(szError,sizeof(szError),0);

	err = SrmOpen(serPortCradleRS232Port /* port */, 9600, /* baud */ &portId);
	 
 	flags = srmSettingsFlagBitsPerChar8; 
 	paramSize = sizeof(flags); 
	err = SrmControl(portId, srmCtlSetFlags, &flags, &paramSize);
	SrmReceiveFlush(portId, 0);
	 
	if (err) { 
	   // display error message here. 
	   FrmCustomAlert(5000, GetSerialErrorText(err, szError), "", "");
	   serPortOpened = false;
	} else
		//record our open status in global. 
		serPortOpened = true; 

	return serPortOpened;
}

void SerialClose(void) {
	if (serPortOpened) {
		SrmClose(portId);
		serPortOpened=false;
	}
}

long SerialCheck(void) {
  long n = -1;
  if (serPortOpened)
    if (SrmReceiveCheck(portId, &n) != errNone)
        SrmClearErr(portId);

  return n;
}

BOOL SerialRead(UInt8 *buf, int buflen, int timeout) {
  Err err;
  MemSet(szError,sizeof(szError),0);

  if (serPortOpened) {
  	SrmReceive(portId, buf, buflen, timeout, &err);
  	
  	if (err != errNone){
		//FrmCustomAlert(5000, GetSerialErrorText(err, szError), "", "");  
		SrmClearErr(portId);
    }
    else
      return true;
  }
  return false;
}

void SerialWrite(UInt8* buf, int buflen) {
  Err err;
  MemSet(szError,sizeof(szError),0);
  
  if (serPortOpened) {
    SrmSend(portId, buf, buflen, &err);
    if (err != errNone) {
	  //FrmCustomAlert(5000, GetSerialErrorText(err, szError), "", "");
      SrmClearErr(portId);
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -