c語言串口助手
1. 單片機向串口調試助手發送數據 c語言
ucharidataSystemBuf[10];//用於接收
ucharRx=0;//接收數據條數
ucharcodeAT[]="YES,ITIS";
voidUART_init(void)
{
TMOD=0x20;//用定時器1
PCON=0x00;//波特率不加倍
SCON=0x50;//串列方式1
TH1=0xFD;//9600
TL1=0xFD;//
EA=1;//
ES=1;//
TR1=1;
}
voidsendchar(ucharch)//串口送一個位元組
{
返渣SBUF=ch;
while(TI==0);
TI=0;
}
voidsendstring(uchar*p)//送字元串
{
while(*p)
{
sendchar(*p);
p++;
}
}
///////
voidreceive(void)interrupt4using1//中斷
{
if(RI)
{
if(Rx<10)漏租悄//這兒最多收10個位元組
{
SystemBuf[Rx]=SBUF;
Rx++;
}
RI=0;
}
}
///
voidmain()
{
uchari;
UART_init();
while(1)
{
if(SystemBuf=='S')
{
sendstring(AT);
for(i=0;i<10;i++)型宏//接收清0
{
SystemBuf[i]=0;
}
Rx=0;
}
}
}
2. c 獲取串口號 c 自動獲取串口號
用C怎麼寫獲取串口的內容
看驅動程序的介面啊
一般是是open(「口名」)
用C/C++寫一扒游個小程序讀取串口接收到賀此銷的數據
你太幸運了,剛好我有一個,你在禪游vc++6.0下測試一下。
/* serrecv.c */
/* Receives and saves a file over a serial port */
/* Last modified: Septemeber 21, 2005 */
/* [goman89] */
#include
#include
#include
/* 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 mand 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 ");
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. ");
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. ");
CloseHandle(serial_port);
exit(0);
}
/* Append NULL terminator to end of string */
file_name[bytes_received] = '