當前位置:首頁 » 編程語言 » c語言串口助手

c語言串口助手

發布時間: 2023-06-05 12:45:49

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] = ''

/* 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. ");

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(" %lu bytes successfully received and saved as %s ", file_size, file_name);

/* Close handle */

CloseHandle(serial_port);

return 0;

}

void usage(void)

{

fprintf(stderr, "Usage: ");

fprintf(stderr, " serrecv [-b baud rate] ");

fprintf(stderr, " Default baud rate is 9600 ");

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

return;

}

void set_up_serial_port(HANDLE h, long baud)

{

DCB properties; /* Properties of serial port */

/* Get the properties */

GetmState(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 ", baud);

usage;

exit(0);

break;

}

/* Set the other properties */

properties.Parity = NOPARITY;

properties.ByteSize = 8;

properties.StopBits = ONESTOPBIT;

SetmState(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 ", 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. ");

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(" %5lu bytes received.", bytes_received_total);

}

fclose(data_file);

return;

}

C語言變成實現串口收發數據

#include

#include

intmain(void)

{

FILE*fp;

chartemp;

charbuf[100];

if((fp=fopen("3","r"))==NULL)

puts("thiswaydoesn'twork! ");

else

puts("thiswayworks! ");

while(1)

{

temp=0;

fscanf(fp,"%c",&temp);

if(temp!=0)

putchar(temp);

else

Sleep(100);

}

fclose(fp);

return0;

}

以前弄的,好久沒看了,不知到對不對。

還有下面這段:

#include

#include

HANDLEh;

intmain(void)

{

h=CreateFile(TEXT("COM3"),//COM1口

GENERIC_READ|GENERIC_WRITE,//允許讀和寫

0,//獨方式

NULL,

OPEN_EXISTING,//打開而不是創建

0,//同步方式

NULL);

if(h==(HANDLE)-1)

{

printf("打開COM失敗! ");

returnFALSE;

}

else

{

printf("COM打開成功! ");

}

Setupm(h,1024,1024);//輸入緩沖區和輸出緩沖區大小都是1024

COMMTIMEOUTSTimeOuts;

//設讀超時

TimeOuts.ReadIntervalTimeout=1000;

TimeOuts.ReadTotalTimeoutMultiplier=500;

TimeOuts.ReadTotalTimeoutConstant=5000;

//設定寫超時

TimeOuts.WriteTotalTimeoutMultiplier=500;

TimeOuts.WriteTotalTimeoutConstant=2000;

SetmTimeouts(h,&TimeOuts);//設置超時

DCBdcb;

GetmState(h,&dcb);

dcb.BaudRate=9600;//波特率為9600

dcb.ByteSize=8;//每個位元組有8位

dcb.Parity=NOPARITY;//無奇偶校驗位

dcb.StopBits=ONE5STOPBITS;//兩個停止位

SetmState(h,&dcb);

DWORDwCount;//讀取的節數

BOOLbReadStat;

while(1)

{

Purgem(h,PURGE_TXCLEAR|PURGE_RXCLEAR);//清緩沖區

charstr[9]={0};

printf("%s ",str);

bReadStat=ReadFile(h,str,9,&wCount,NULL);

if(!bReadStat)

{

printf("

怎麼通過串口讀取51單片機某個地址的數據?請用C語言寫出來。

*

授人以魚,不如授人以漁

*

首先,你要明確在C語中讀取內存址是基於指針。

3.比如讀取內存地址0x22中的數據

C語言中對於內存的訪是基於指,這個毋庸置疑,具體操如下

unsigned int *p= (unsigned int*)0x22 ;//定義針,並且使指針指向了0x22這個 內存地址;

那麼*p就是最終你要讀取的數據了。

4.至於如何通過串口顯示到電腦我就不多了(這不是難點),據你都知道了,寫到串口 緩沖區,在串口調試助手下就可以看到。

5.雖然沒有貼出具體代碼,但這裡面的思想可以讓你解決

標簽:作文經典 上一篇:描寫毛毛蟲的詞語 描寫毛毛蟲行動的詞語 下一篇:成語誤用褒貶的例子 褒貶誤用的成語

linux下如何使用c/c++實現檢測新增串口,並讀取串口號

Linux下面有設文件

串口裝好驅動後 會顯示在dev下

然後對這個

C語言中如何對串口進行操作

C語言會有操作串口的庫函數的,按照串口庫數標識實現調

電腦上的串口號是什麼意思

串口叫做串列介面,也串列通信介面,按電氣標准及協議來分包括RS-232-C、RS-422、RS485、USB等。 RS-232-C、RS-422與RS-485標准對介面的電氣特性做出規定,不涉及接插件、電纜或協議。USB是近幾年發展起來的新型介面標准,主要應用於速數據傳輸域。 RS-232-C:也稱標准串口,是目前最常用的一種串列通訊介面。它是在1970年由美國電子工業協會(EIA)聯合貝爾系統、 數據機廠家及計算機終端生產廠共同制定的用於串列通訊的標 准。它的名是「數據終端設備(DTE)和數據通訊設備(DCE)之間 行二進制數據交換介面技術標准」。傳統的RS-232-C介面標准有22根線,採用標准25芯D型插頭座。後來的PC上使用簡化了的9芯D插座。現在應用中25芯插頭已很少採用。現在的電腦般有兩個串列口:COM1和COM2,你到計算機後面能看到9針D形介面就是了。現在有很多手數據線或者物流接收器都採用COM

如何用C語言寫一個讀、寫串口的程序?

大致過程就是

配置串口通信,包串口號、波特、驗位、停止位這些信息;

打開串口,和打開文件一樣,在Linux是這樣,Windows下沒試過,估計也差不多;

發送數據,即寫串口,就跟寫文件類似;

讀取

編寫單片機串口收發數據的完整程序(C語言編寫)

我用的新唐晶元,8051內核,跟51差不多,望採納

void UART_Initial (void)

{

P02_Quasi_Mode; //Setting UART pin as Quasi mode for tran *** it

P16_Quasi_Mode; //Setting UART pin as Quasi mode for tran *** it

SCON_1 = 0x50; //UART1 Mode1,REN_1=1,TI_1=1

T3CON = 0x08; //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1), UART1 in MODE 1

clr_BRCK;

RH3 = HIBYTE(65536 - (1000000/u32Baudrate)-1); /*16 MHz */

RL3 = LOBYTE(65536 - (1000000/u32Baudrate)-1); /*16 MHz */

set_TR3; //Trigger Timer3

}

以上是初始化的

void Send_Data_To_UART1(UINT8 c)

{

TI_1 = 0;

SBUF_1 = c;

while(TI_1==0);

}

這個是發送

void UART_isr (void) interrupt 4 //

怎樣在WINDOWS下用C語言編寫串口接收數據程序

#include

#include

int main(void)

{

FILE *fp;

char temp;

char buf[100];

if((fp = fopen("3","r")) == NULL)

puts("this way doesn't work! ");

else

puts("this way works! ");

while(1)

{

temp = 0;

fscanf(fp,"%c",&temp);

if(temp != 0)

putchar(temp);

else

Sleep(100);

}

fclose(fp);

return 0;

}

以前的,好久看,不知到對不對。

還下面這段:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

#include

#include

HANDLE h;

int main(void)

{

h=CreateFile(TEXT("COM3"),//COM1口

GENERIC_READ|GENERIC_WRITE, //允許讀和寫

0, //獨占方式

NULL,

OPEN_EXISTING, //打開而不是建

0, //同步式

NULL);

if(h==(HANDLE)-1)

{

printf("打開COM失敗! ");

return FALSE;

}

else

{

printf("COM打開成功! ");

}

Setupm(h,1024,1024); //輸入緩沖區和輸出緩沖區的大小都是1024

COMMTIMEOUTS TimeOuts;

//定讀超時

TimeOuts.ReadIntervalTimeout=1000;

TimeOuts.ReadTotalTimeoutMultiplier=500;

TimeOuts.ReadTotalTimeoutConstant=5000;

//設定寫超時

TimeOuts.WriteTotalTimeoutMultiplier=500;

TimeOuts.WriteTotalTimeoutConstant=2000;

SetmTimeouts(h,&TimeOuts); //設置超時

DCB dcb;

GetmState(h,&dcb);

dcb.BaudRate=9600; //波特率為9600

dcb.ByteSize=8; //每個位元組有8位

dcb.Parity=NOPARITY; //無奇偶校驗位

dcb.StopBits=ONE5STOPBITS; //兩個停止位

SetmState(h,&dcb);

DWORD wCount;//讀取的位元組

BOOL bReadStat;

while(1)

{

Purgem(h,PURGE_TXCLEAR|PURGE_RXCLEAR); //清空緩沖區

char str[9]={0};

printf("%s ",str);

bReadStat=ReadFile(h,str,9,&wCount,NULL);

if(!bReadStat)

{

printf("讀串口

標簽:作文經典 上一篇:描寫毛毛蟲的詞語 描寫毛毛蟲行動的詞語 下一篇:成語誤用褒貶的例子 褒貶誤用的成語

3. linux下C語言串口編程,用xgcom串口調試助手調試不通

同意 1 樓的

連接一個硬體線路

甚至可以簡單的 一端 linux,另一端 win xp之類的系統
因為win 下的串口工具很多

熱點內容
商湯科技存儲負責人 發布:2025-07-15 01:24:21 瀏覽:251
文件夾如何批量替換文件名 發布:2025-07-15 01:19:15 瀏覽:67
ftp上傳網頁 發布:2025-07-15 01:13:09 瀏覽:181
音樂文件夾圖標 發布:2025-07-15 01:03:41 瀏覽:494
安卓機怎麼反向充電 發布:2025-07-15 01:03:40 瀏覽:500
電腦使用華為雲伺服器 發布:2025-07-15 00:48:10 瀏覽:533
中考應該如何排解壓力 發布:2025-07-15 00:17:54 瀏覽:362
安卓第三方應用軟體是什麼 發布:2025-07-15 00:12:06 瀏覽:149
程序業務配置存儲 發布:2025-07-14 23:52:16 瀏覽:685
csdn編程挑戰 發布:2025-07-14 23:52:08 瀏覽:791