有没谁有不用socket的串口通讯的源代码,散分!

hd506lg 2002-02-03 03:05:22
...全文
128 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
liubear 2002-09-10
  • 打赏
  • 举报
回复
socket的文档也不少,找一个试试
Behard 2002-09-10
  • 打赏
  • 举报
回复
最好是tc平台下的???
arran 2002-09-09
  • 打赏
  • 举报
回复
兄台,串口通信的例子很多,msdn里面也有一个很完整的,何苦要找socket的~
hd506lg 2002-02-05
  • 打赏
  • 举报
回复
最好是tc平台下的
NowCan 2002-02-04
  • 打赏
  • 举报
回复
串口通信要用socket?为什么???
真是不明白。
hd506lg 2002-02-04
  • 打赏
  • 举报
回复
好高深的代码,这位老大能否解释一下
hd506lg 2002-02-04
  • 打赏
  • 举报
回复
你那代码是什么平台的麻烦说清楚啊
kickmaster 2002-02-04
  • 打赏
  • 举报
回复

enum SERIAL_RESULT_TYPE {
SERIAL_OK = 0, // Everything's OK.
SERIAL_PORT_NOT_INIT, // Can't do this until the port is initialized
SERIAL_PORT_ALREADY_OPENED, // Can't re-open an already-opened serial port
SERIAL_CANNOT_CREATE_THREAD,// Can't create the secondary thread necessary
SERIAL_CANNOT_CREATE_MUTEX, // Can't create the mutex object for the data
SERIAL_CANNOT_INIT_PORT, // Can't open the serial port (bad settings)
SERIAL_UNABLE_TO_LOCK, // Unable to lock NMEA data
SERIAL_UNABLE_TO_UNLOCK, // Unable to unlock NMEA data
SERIAL_WIN32_ERROR // OS-specific error. (Call GetLastError())
};

SERIAL_RESULT_TYPE CNmeaSerial::OpenConnection (uint8 comPort,
uint32 baudRate, uint8 dataBits, SETPARITY parity, STOPBITS stopBits)


{
if (m_isConnected == TRUE) {
return SERIAL_PORT_ALREADY_OPENED;
}

char comString[10]; // Contains enough room for 'ComXxx'.
char settingsString[15]; // Contains enough room for '115200,n,8,1.5'.
char stopBitNum[4]; // Contains enough room for '1', '1.5', or '2'.
char parityChar; // Contains the letter (n)one, (e)ven, or (o)dd.

sprintf (comString, "COM%d", comPort);
m_hPort = CreateFile (
comString,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (m_hPort != INVALID_HANDLE_VALUE) {
// Serial port exists. Set the parity and stop bits.
switch (parity) {
case SETPARITY_NONE: parityChar = 'n'; break;
case SETPARITY_EVEN: parityChar = 'e'; break;
case SETPARITY_ODD : parityChar = 'o'; break;
}
switch (stopBits) {
case STOPBITS_ONE : strcpy (stopBitNum, "1" ); break;
case STOPBITS_ONEFIVE: strcpy (stopBitNum, "1.5"); break;
case STOPBITS_TWO : strcpy (stopBitNum, "2" ); break;
}
sprintf (settingsString, "%d,%c,%d,%s", baudRate, parityChar,
dataBits, stopBitNum);

// Attempt to connect to the serial port.
if (!SetupComm (m_hPort, 4096, 4096)) {
CloseHandle (m_hPort);
return SERIAL_CANNOT_INIT_PORT;
}

// Specify the com port read timeouts.
COMMTIMEOUTS commTimeouts;
commTimeouts.ReadIntervalTimeout = 0;
commTimeouts.ReadTotalTimeoutMultiplier = 0;
commTimeouts.ReadTotalTimeoutConstant = m_timeout;
commTimeouts.WriteTotalTimeoutMultiplier = 0;
commTimeouts.WriteTotalTimeoutConstant = 0;

// Create a DCB (device control block) for the serial port. A DCB
// contains com port settings. Clear the initial junk out of it.
DCB dcb;
ZeroMemory (&dcb, sizeof (DCB));

// Initialize the DCB values with the correct values given the baud
// rate, parity, number of stop bits, and number of data bits.
if (!BuildCommDCB (settingsString, &dcb)) {
CloseHandle (m_hPort);
return SERIAL_CANNOT_INIT_PORT;
}

// Set the com port parameters.
if ((!SetCommState (m_hPort, &dcb))
|| (!SetCommTimeouts (m_hPort, &commTimeouts))) {
CloseHandle (m_hPort);
return SERIAL_CANNOT_INIT_PORT;
}

// Create the thread that will constantly read strings given the
// serial port handle.
SERIAL_RESULT_TYPE status = InitThread ();
if (status != SERIAL_OK) {
CloseHandle (m_hPort);
return status;
}

// Everything's good!
m_comPort = comPort;
m_baudRate = baudRate;
m_dataBits = dataBits;
m_parity = parity;
m_stopBits = stopBits;
m_isConnected = TRUE;
return SERIAL_OK;
} else {
return SERIAL_CANNOT_INIT_PORT;
}
}

SERIAL_RESULT_TYPE CNmeaSerial::InitThread ()
{
unsigned long hThread;
HANDLE hMutex;

if (hThread = _beginthread (&CNmeaSerial::ProcessSentences, 0, this)) {
hMutex = CreateMutex (NULL, FALSE, "NMEA");
if (hMutex != NULL) {
// Everything's ready to go!
m_hThread = hThread;
m_hMutex = hMutex;
m_parser.ResetData ();
TRACE0 ("Serial port thread initialized.\n");
return SERIAL_OK;
} else {
// Cannot create the mutex; terminate the serial thread.
KillThread ();
return SERIAL_CANNOT_CREATE_MUTEX;
}
} else {
return SERIAL_CANNOT_CREATE_THREAD;
}
}



kickmaster 2002-02-04
  • 打赏
  • 举报
回复
老兄,代码的注释很清楚啦。
不懂的函数MSDN拉
hd506lg 2002-02-03
  • 打赏
  • 举报
回复
发给我也行 my email: hd506lg@163.net
hd506lg 2002-02-03
  • 打赏
  • 举报
回复
贴出来啊
大家参考一下
kickmaster 2002-02-03
  • 打赏
  • 举报
回复
我有,要乎?
还是贴出来?
hd506lg 2002-02-03
  • 打赏
  • 举报
回复
我知道
可是他上面的都有用到socket啊
ty3310 2002-02-03
  • 打赏
  • 举报
回复
你在网上找找“龚建伟 串口通讯”
会找到很多资料的

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧