用CC++写一个小程序读取串口接收到的数据

用CC++写一个小程序读取串口接收到的数据,第1张

你太幸运了,刚好我有一个,你在vc++6.0下测试一下。

/* serrecv.c */

/* Receives and saves a file over a serial port */

/* Last modified: Septemeber 21, 2005 */

/* */

#include <windows.h>

#include <stdio.h>

#include <stdlib.h>

/* Function to print out usage information */

void usage(void)

/* Function to set up the serial port settings with the specified baud rate,

no parity, and one stop bit */

void set_up_serial_port(HANDLE h, long baud)

/* Function to receive and save file from serial port */

void get_file_from_serial_port(HANDLE h, char *file_name, unsigned long file_length)

int main(int argc, char **argv)

{

HANDLE serial_port/* Handle to the serial port */

long baud_rate = 9600 /* Baud rate */早指

char port_name[] = "COM1:" /* Name of serial port */

unsigned long file_size /* Size of file to receive in bytes */

unsigned long bytes_received /* Bytes received from serial port */

unsigned long file_name_size /* Size of file name in bytes */

char file_name[256] /* Name of file to receive */

/* Check command line */

if (argc == 3)

{

/* Read in baud rate */

if (argv[1][1] != 'b'睁睁洞 || sscanf(argv[2], "%ld", &baud_rate) != 1)

{

usage()

exit(0)

}

}

else if (argc != 1)

{

usage()

exit(0)

}

/* Open up a handle to the serial port */

serial_port = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)

/悉枯* Make sure port was opened */

if (serial_port == INVALID_HANDLE_VALUE)

{

fprintf(stderr, "Error opening port\n")

CloseHandle(serial_port)

exit(0)

}

/* Set up the serial port */

set_up_serial_port(serial_port, baud_rate)

/* Receive file name size from serial port */

ReadFile(serial_port, (void *)&file_name_size, sizeof(unsigned long), &bytes_received, NULL)

if (bytes_received != sizeof(unsigned long))

{

fprintf(stderr, "Error getting file name size.\n")

CloseHandle(serial_port)

exit(0)

}

/* Receive file name from serial port */

ReadFile(serial_port, (void *)file_name, file_name_size, &bytes_received, NULL)

if (bytes_received != file_name_size)

{

fprintf(stderr, "Error retrieving file name.\n")

CloseHandle(serial_port)

exit(0)

}

/* Append NULL terminator to end of string */

file_name[bytes_received] = '\0'

/* Receive file size from serial port */

ReadFile(serial_port, (void *)&file_size, sizeof(unsigned long), &bytes_received, NULL)

if (bytes_received != sizeof(unsigned long))

{

fprintf(stderr, "Error getting file size.\n")

CloseHandle(serial_port)

exit(0)

}

/* Get the file from the serial port */

get_file_from_serial_port(serial_port, file_name, file_size)

/* Print out success information */

printf("\n%lu bytes successfully received and saved as %s\n", file_size, file_name)

/* Close handle */

CloseHandle(serial_port)

return 0

}

void usage(void)

{

fprintf(stderr, "Usage:\n")

fprintf(stderr, "\tserrecv [-b baud rate]\n")

fprintf(stderr, "\tDefault baud rate is 9600\n")

fprintf(stderr, "tSupported baud rates: 1200, 2400, 4800, 9600, 14400, 19200\n")

return

}

void set_up_serial_port(HANDLE h, long baud)

{

DCB properties /* Properties of serial port */

/* Get the properties */

GetCommState(h, &properties)

/* Set the baud rate */

switch(baud)

{

case 1200:

properties.BaudRate = CBR_1200

break

case 2400:

properties.BaudRate = CBR_2400

break

case 4800:

properties.BaudRate = CBR_4800

break

case 9600:

properties.BaudRate = CBR_9600

break

case 14400:

properties.BaudRate = CBR_14400

break

case 19200:

properties.BaudRate = CBR_19200

break

case 38400:

properties.BaudRate = CBR_38400

break

default:

fprintf(stderr, "Invalid baud rate: %ld\n", baud)

usage()

exit(0)

break

}

/* Set the other properties */

properties.Parity = NOPARITY

properties.ByteSize = 8

properties.StopBits = ONESTOPBIT

SetCommState(h, &properties)

return

}

void get_file_from_serial_port(HANDLE h, char *file_name, unsigned long file_length)

{

FILE *data_file /* File to create */

unsigned long bytes_left = file_length/* Bytes left to receive */

unsigned long bytes_received_total = 0/* Total bytes received */

unsigned long bytes_to_receive /* Number of bytes to receive */

unsigned long bytes_received /* Number of bytes receive */

char buffer[200] /* Buffer to store data */

/* Open the file */

data_file = fopen(file_name, "wb")

/* Quit if file couldn't be opened */

if (data_file == NULL)

{

fprintf(stderr, "Could not create file %s\n", file_name)

CloseHandle(h)

exit(0)

}

while (1)

{

/* Determine how many bytes to read */

if (bytes_left == 0)

{

break

}

else if (bytes_left <200)

{

bytes_to_receive = bytes_left

}

else

{

bytes_to_receive = 200

}

/* Receive data over serial cable */

ReadFile(h, (void *)buffer, bytes_to_receive, &bytes_received, NULL)

if (bytes_received != bytes_to_receive)

{

fprintf(stderr, "Error reading file.\n")

CloseHandle(h)

exit(0)

}

/* Save buffer to file */

fwrite((void *)buffer, 1, bytes_received, data_file)

/* Decrement number of bytes left */

bytes_left -= bytes_received

/* Increment number of bytes received */

bytes_received_total += bytes_received

/* Print out progress */

printf("\r%5lu bytes received.", bytes_received_total)

}

fclose(data_file)

return

}

#include <reg51.h>

#include <intrins.h>

unsigned char key_s, key_v, tmp

char code str[] = "welcome! www.willar.com \n\r"

void send_str()

bit scan_key()

void proc_key()

void delayms(unsigned char ms)

void send_char(unsigned char txd)

sbit K1 = P1^4

main()

{

TMOD = 0x20// 定时器1工作于8位自动重载模式, 用于产生波特率

TH1 = 0xFD// 波特率9600

TL1 = 0xFD

SCON = 0x50// 设定串行口工作方式

PCON &= 0xef// 波特率不倍增

TR1 = 1// 启动定时器1

IE = 0x0// 禁止任何中断

while(1)

{

if(scan_key()) // 扫描按键

{

delayms(10)// 延时去抖动

if(scan_key()) // 再次扫描

{

key_v = key_s// 保存键值

proc_key()// 键处理

}

}

if(RI) // 是否有数据到来

{

RI = 0

tmp = SBUF// 暂存接收到的数据

P0 = tmp// 数据传送到P0口

send_char(tmp)// 回传接收到的数据

}

}

}

bit scan_key()

// 扫描按键

key_s = 0x00

key_s |= K1

return(key_s ^ key_v)

}

void proc_key()

// 键处理

{

if((key_v &0x01) == 0)

{ // K1按下

send_str()// 传送字串"welcome!...

}

}

void send_char(unsigned char txd)

// 传送一个字符

{

SBUF = txd

while(!TI)// 等特数据传送

TI = 0// 清除数据传送标志

}

void send_str()

// 传送字串

{

unsigned char i = 0

while(str[i] != '\0')

{

SBUF = str[i]

while(!TI)// 等特数据传送

TI = 0// 清除数据传送标志

i++// 下一个字符

}

}

void delayms(unsigned char ms)

// 延时子程序

{

unsigned char i

while(ms--)

{

for(i = 0i <120i++)

}

}

拓展资料

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

尽管C语言提供了许多简激则低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。

二十世纪八十年代,为铅宴了避免各开发厂商用的C语言语法产生差异,由美国国家标准局为C语言制定了一套完整的美国国家标准语法,称为ANSI C,作为C语言最初的标准。目前2011年12月8日,国际标准化组织(ISO)和国际电工委员会(IEC)发布的C11标准是C语言的第三个官方标准,也是C语言的最新标准,该标拦棚准更好的支持了汉字函数名和汉字标识符,一定程度上实现了汉字编程。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8236909.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-14
下一篇2023-04-14

发表评论

登录后才能评论

评论列表(0条)

    保存