當前位置:首頁 » 硬碟大全 » 串口環形緩存代碼下載
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

串口環形緩存代碼下載

發布時間: 2022-07-04 08:30:26

A. 通過串口下載文件到指定的內存後如何運行這個文件

盡量關閉消耗資源的程序運行.如WORD裡面老跳的話,就把拼寫檢查之類的東西都關了,應該會好點,128M的內存,在電腦屬性里查,只有96M可用,把這些關了後,WORD編輯裡面內存不足就不會再跳出來了.也可將虛擬內存稍微設置大一點,運行個後台內存整理的軟體,可能會好一點.
不知道你是內存太小了還是其他別的原因,如果僅僅只是內存太小了,那沒辦法了,只能想辦法將就著用了.一般來說現在128M的內存處理文字玩小游戲都還行,部分數碼圖片處理,跳出內存不足是很正常的,WORD老跳就要檢查下了.

B. 請問:希捷硬碟500G 32MB緩存,32MB緩存的代碼是多少

希捷7200.11
500GB串口盒包硬碟的編號為ST3500320AS,ST代表希捷,3代表是3.5英寸的桌面硬碟,500代表硬碟容量是500GB,320代表32MB緩存及兩碟片設計,AS代表這塊硬碟是SATA介面

C. linux 內核 uart driver 只有fifo滿才向用戶buf傳遞數據嗎

對於串口驅動的移植准備自己分析一下源代碼的,但是發現自己好多地方都只知道一 些皮毛,不明白其中的道理,所以我上網搜的時候發現有好多人寫了很多很好的文章了,下面我轉載的這篇就非常不錯,一個困惱我好久的問題是驅動代碼中只是注 冊了platform驅動,而platform設備注冊在哪裡?這個問題困惱我好久,源代碼中一直沒找到,下面文章就解決了這個問題。當然文章中詳細了講 述了很多細節的知識。
原文地址 http://chxxxyg.blog.163.com/blog/static/150281193201082044140894/

(1)串口移植
S3C2440共有3個串口,在SMDK2440平台上串口0和串口1都作為普通串口使用,串口2工作在紅外收發模式。TQ2440開發板將它們都作為普通串口,目前所需要的只有串口0,作為控制終端,所以此處不作修改。
在文件 linux/arch/arm/plat-s3c24xx/devs.c中定義了三個串口的硬體資源。
static struct resource s3c2410_uart0_resource[] = {
………………………………
};
static struct resource s3c2410_uart1_resource[] = {
………………………………
};
static struct resource s3c2410_uart2_resource[] = {
………………………………
};
在文件linux/arch/arm/plat-samsung/dev-uart.c中定義了每個串口對應的平台設備。
static struct platform_device s3c24xx_uart_device0 = {
.id = 0,
};
static struct platform_device s3c24xx_uart_device1 = {
.id = 1,
};
static struct platform_device s3c24xx_uart_device2 = {
.id = 2,
};
在文件linux/arch/arm/mach-s3c2440/mach-smdk2440.c中有串口一些寄存器的初始化配置。
static struct s3c2410_uartcfg smdk2440_uartcfgs[] __initdata = {
[0] = {
…………………………
},
[1] = {
…………………………
},
/* IR port */
[2] = {
…………………………
}
};
在文件linux/arch/arm/mach-s3c2440/mach-smdk2440.c中將調用函數
s3c24xx_init_uarts()最終將上面的硬體資源,初始化配置,平台設備整合到一起。
在文件 linux/arch/arm/plat-s3c/init.c中有
static int __init s3c_arch_init(void)
{
………………………………
ret = platform_add_devices(s3c24xx_uart_devs, nr_uarts);
return ret;
}
這個函數將串口所對應的平台設備添加到了內核。
(2)串口設備驅動原理淺析
我認為任何設備在linux中的實現就「兩條線」。一是設備模型的建立,二是讀寫數據流。串口驅動也是這樣。
串口設備模型建立:
串口設備驅動的核心結構體在文件linux/drivers/serial/samsuing.c中如下
static struct uart_driver s3c24xx_uart_drv = {
.owner = THIS_MODULE,
.dev_name = "s3c2410_serial",
.nr = CONFIG_SERIAL_SAMSUNG_UARTS,
.cons = S3C24XX_SERIAL_CONSOLE,
.driver_name = S3C24XX_SERIAL_NAME,
.major = S3C24XX_SERIAL_MAJOR,
.minor = S3C24XX_SERIAL_MINOR,
};
串口驅動的注冊
static int __init s3c24xx_serial_modinit(void)
{
………………………………
ret = uart_register_driver(&s3c24xx_uart_drv);
………………………………
}
串口驅動其實是一個典型的tty驅動
int uart_register_driver(struct uart_driver *drv)
{
………………………………
//每一個埠對應一個state
drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
………………………………
normal = alloc_tty_driver(drv->nr); //分配該串口驅動對應的tty_driver
………………………………
drv->tty_driver = normal; //讓drv->tty_driver欄位指向這個tty_driver
………………………………
normal->driver_name = drv->driver_name;
normal->name = drv->dev_name;
normal->major = drv->major;
normal->minor_start = drv->minor;
………………………………
//設置該tty驅動對應的操作函數集tty_operations (linux/drivers/char/core.c)
tty_set_operations(normal, &uart_ops);
………………………………
retval = tty_register_driver(normal); //將tty驅動注冊到內核
………………………………
}
其實tty驅動的本質是一個字元設備,在文件 linux/drivers/char/tty_io.c中
int tty_register_driver(struct tty_driver *driver)
{
………………………………
cdev_init(&driver->cdev, &tty_fops);
driver->cdev.owner = driver->owner;
error = cdev_add(&driver->cdev, dev, driver->num);
………………………………
}
它所關聯的操作函數集tty_fops在文件linux/drivers/char/tty_io.c中實現
static const struct file_operations tty_fops = {
.llseek = no_llseek,
.read = tty_read,
.write = tty_write,
………………………………
.open = tty_open,
………………………………
};
到此串口的驅動作為tty_driver被注冊到了內核。前面提到串口的每一個埠都是作為平台設備被添加到內核的。那麼這些平台設備就對應著有它們的平台設備驅動。在文件linux/drivers/serial/s3c2440.c中有:
static struct platform_driver s3c2440_serial_driver = {
.probe = s3c2440_serial_probe,
.remove = __devexit_p(s3c24xx_serial_remove),
.driver = {
.name = "s3c2440-uart",
.owner = THIS_MODULE,
},
};
當其驅動與設備匹配時就會調用他的探測函數
static int s3c2440_serial_probe(struct platform_device *dev)
{
return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
}
每一個埠都有一個描述它的結構體s3c24xx_uart_port 在 文件linux/drivers/serial/samsuing.c
static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
[0] = {
.port = {
.lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
.iotype = UPIO_MEM,
.irq = IRQ_S3CUART_RX0, //該埠的中斷號
.uartclk = 0,
.fifosize = 16,
.ops = &s3c24xx_serial_ops, //該埠的操作函數集
.flags = UPF_BOOT_AUTOCONF,
.line = 0, //埠編號
}
},
………………………………

上面探測函數的具體工作是函數s3c24xx_serial_probe()來完成的
int s3c24xx_serial_probe(struct platform_device *dev,
struct s3c24xx_uart_info *info)
{
………………………………
//根據平台設備提供的硬體資源等信息初始化埠描述結構體中的一些欄位
ret = s3c24xx_serial_init_port(ourport, info, dev);
//前面注冊了串口驅動,這里便要注冊串口設備
uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
………………………………
}
int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
{
………………………………
//前面說串口驅動是tty_driver,這里可以看到串口設備其實是tty_dev
tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
………………………………
}
串口數據流分析:
在串口設備模型建立中提到了三個操作函數集,uart_ops ,tty_fops,s3c24xx_serial_ops數據的流動便是這些操作函數間的調用,這些調用關系如下:

在對一個設備進行其他操作之前必須先打開它,linux/drivers/char/tty_io.c
static const struct file_operations tty_fops = {
………………………………
.open = tty_open,
………………………………
};
static int tty_open(struct inode *inode, struct file *filp)
{
………………………………
dev_t device = inode->i_rdev;
………………………………
driver = get_tty_driver(device, &index); //根據埠設備號獲取它的索引號
………………………………
if (tty) {
………………………………
} else
tty = tty_init_dev(driver, index, 0); //創建一個tty_struct 並初始化
………………………………
}
struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,int first_ok)
{
………………………………
tty = alloc_tty_struct(); //分配一個tty_struct結構
//一些欄位的初始化,
initialize_tty_struct(tty, driver, idx);
//完成的主要工作是driver->ttys[idx] = tty;
retval = tty_driver_install_tty(driver, tty);
………………………………
/*
下面函數主要做的就是調用線路規程的打開函數ld->ops->open(tty)。
在這個打開函數中分配了一個重要的數據緩存
tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
*/
retval = tty_ldisc_setup(tty, tty->link);
}
void initialize_tty_struct(struct tty_struct *tty,struct tty_driver *driver, int idx)
{
………………………………
//獲取線路規程操作函數集tty_ldisc_N_TTY,並做這樣的工作tty->ldisc = ld;
tty_ldisc_init(tty);
………………………………
/*
下面函數的主要工作是INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc);
初始化一個延時tty->buf.work 並關聯一個處理函數flush_to_ldisc(),這個函數將在
數據讀取的時候用到。
*/
tty_buffer_init(tty);
………………………………
tty->driver = driver;
tty->ops = driver->ops; //這里的ops就是struct tty_operations uart_ops
tty->index = idx; //idx就是該tty_struct對應埠的索引號
tty_line_name(driver, idx, tty->name);
}
埠設備打開之後就可以進行讀寫操作了,這里只討論數據的讀取,在文件 linux/drivers/char/tty_io.c中,
static const struct file_operations tty_fops = {
………………………………
.read = tty_read,
………………………………
};
static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
loff_t *ppos)
{
………………………………
ld = tty_ldisc_ref_wait(tty); //獲取線路規程結構體
if (ld->ops->read) //調用線路規程操作函數集中的n_tty_read()函數
i = (ld->ops->read)(tty, file, buf, count);
else
………………………………
}
在linux/drivers/char/N_tty.c中:
struct tty_ldisc_ops tty_ldisc_N_TTY = {
………………………………
.open = n_tty_open,
………………………………
.read = n_tty_read,
………………………………
};
static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
unsigned char __user *buf, size_t nr)
{
………………………………
while (nr) {
………………………………
if (tty->icanon && !L_EXTPROC(tty)) {
//如果設置了tty->icanon 就從緩存tty->read_buf[]中逐個數據讀取,並判斷讀出的每一個數//據的正確性或是其他數據類型等。
eol = test_and_clear_bit(tty->read_tail,tty->read_flags);
c = tty->read_buf[tty->read_tail];
………………………………
} else {
………………………………
//如果沒有設置tty->icanon就從緩存tty->read_buf[]中批量讀取數據,之所以要進行兩次讀
//取是因為緩存tty->read_buf[]是個環形緩存
uncopied = _from_read_buf(tty, &b, &nr);
uncopied += _from_read_buf(tty, &b, &nr);
………………………………
}
}
………………………………
}
用戶空間是從緩存tty->read_buf[]中讀取數據讀的,那麼緩存tty->read_buf[]中的數據有是從那裡來的呢?分析如下:
回到文件 linux/drivers/serial/samsuing.c中,串口數據接收中斷處理函數實現如下:
這是串口最原始的數據流入的地方
static irqreturn_t s3c24xx_serial_rx_chars(int irq, void *dev_id)
{
………………………………
while (max_count-- > 0) {
………………………………
ch = rd_regb(port, S3C2410_URXH); //從數據接收緩存中讀取一個數據
………………………………
flag = TTY_NORMAL; //普通數據,還可能是其他數據類型在此不做討論
………………………………
/*
下面函數做的最主要工作是這樣
struct tty_buffer *tb = tty->buf.tail;
tb->flag_buf_ptr[tb->used] = flag;
tb->char_buf_ptr[tb->used++] = ch;
將讀取的數據和該數據對應標志插入 tty->buf。
*/
uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN, ch, flag);
}
tty_flip_buffer_push(tty); //將讀取到的max_count個數據向上層傳遞。
out:
return IRQ_HANDLED;
}
void tty_flip_buffer_push(struct tty_struct *tty)
{
………………………………
if (tty->low_latency)
flush_to_ldisc(&tty->buf.work.work);
else
schele_delayed_work(&tty->buf.work, 1);
//這里這個延時work在上面串口設備打開中提到過,該work的處理函數也是flush_to_ldisc。
}
static void flush_to_ldisc(struct work_struct *work)
{
………………………………
while ((head = tty->buf.head) != NULL) {
………………………………
char_buf = head->char_buf_ptr + head->read;
flag_buf = head->flag_buf_ptr + head->read;
………………………………
//剛才在串口接收中斷處理函數中,將接收到的數據和數據標志存到tty->buf中,現在將
//這些數據和標志用char_buf 和flag_buf指向進一步向上傳遞。
disc->ops->receive_buf(tty, char_buf,flag_buf, count);
spin_lock_irqsave(&tty->buf.lock, flags);
}
}
上面調用的函數disc->ops->receive_buf在文件linux/drivers/char/N_tty.c中實現
struct tty_ldisc_ops tty_ldisc_N_TTY = {
………………………………
.receive_buf = n_tty_receive_buf,
………………………………
};
static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
{
………………………………
//現在可以看到緩沖區tty->read_buf 中數據的由來了。
if (tty->real_raw) {
//如果設置了tty->real_raw將上面講到的些傳入數據批量拷貝到tty->read_head中。
//對環形緩存區的數據拷貝需要進行兩次,第一次拷貝從當前位置考到緩存的末尾,如果還//有沒考完的數據而且緩存區開始出處還有剩餘空間,就把沒考完的數據考到開始的剩餘空
//間中。
spin_lock_irqsave(&tty->read_lock, cpuflags);
i = min(N_TTY_BUF_SIZE - tty->read_cnt,N_TTY_BUF_SIZE - tty->read_head);
i = min(count, i);
memcpy(tty->read_buf + tty->read_head, cp, i);
tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
tty->read_cnt += i;
cp += i;
count -= i;
i = min(N_TTY_BUF_SIZE - tty->read_cnt,
N_TTY_BUF_SIZE - tty->read_head);
i = min(count, i);
memcpy(tty->read_buf + tty->read_head, cp, i);
tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
tty->read_cnt += i;
spin_unlock_irqrestore(&tty->read_lock, cpuflags);
} else {
for (i = count, p = cp, f = fp; i; i--, p++) {
//如果沒有設置tty->real_raw,就根據傳入數據標志分類獲取數據。
………………………………
}
………………………………
}
………………………………
}
到此,數據讀取的整個過程就結束了。可以看出數據讀取可以分為兩個階段,一個階段是上層函數從環形緩存區tty->read_buf 讀取數據,第二階段是底層函數將接收的數據考到環形緩存區tty->read_buf 中。

D. 串口向單片機發送指令,接受環形隊列怎麼理解

多的先不說了,就說6個字元的:
開兩個6個數組空間,將採集的數據存在一個數組,判斷起始位,和結束位,是否符合,符合就將4數據送下個數組,不符就不送下個數組.
ea 不用,es用,傳很多數據時必須用,不然會錯

E. c# 如何將串口數據寫入緩存(一個arraylist[])

BytesBuffer.Add("你要寫入的東西.");如:你讀到
一個字元串
string
str="abc";寫入緩存:
BytesBuffer.Add(str);

F. 關於串口的代碼

你的電腦上面沒有串口吧,用GetPortName獲取到一個空數組,數組ports的長度為-1,你所引ports的時候就會報錯,你可以用虛擬串口軟體虛擬串口,或者加一條判斷有沒有串口

G. 想清除串口緩存區,怎麼做

tcflush函數清除串口輸入緩存(終端驅動已接到,但用戶尚未讀取)或串口輸出緩存(用戶已經寫如緩存,但尚未發送)。函數原型:int tcflush(int filedes,int quene)參數解釋filedes: 描述符。quene取值及含義: *TCIFLUSH 清除輸入隊列 *TCOFLUSH 清除輸出隊列 *TCIOFLUSH 清除輸入、輸出隊列舉例:tcflush(fd,TCIOFLUSH);另加的說明:在打開串口後,用戶其實其實已經可以開始從串口讀取數據了,但如果用戶沒有讀取,數據將被將保存在緩沖區里。如果用戶不想要開始的一段數據,或者發現緩沖區數據有誤,可以使用這個函數將緩沖區清空。應用舉例:tcflush(fd, TCIOFLUSH);sleep(2); read_len = read(fd, buff, 10);

H. labview串口接收緩存數據問題

1. 你的程序有些問題,你不能每次都打開串口,在循環中,這樣會導致串口不斷的開啟和關閉。

2. 數據接收時,只有長度大於0,才去讀取信息。不要每次都強行讀取。
3. 當點擊發送命令後,要加個延時,才能去讀取。

I. C#串口通信中COM口的緩存為4096,假設1個2MB的數據通過此COM口,怎麼樣實現全部讀取,而沒有遺漏或錯誤

發送數據大於緩沖區時,一般採用分片多次發送的方法,另外可以另外開辟一塊緩沖區,將緩沖區數據讀完之後立刻刷新等待後續數據,但是此種方法依然可能造成數據丟失。

J. 求上位機串口程序代碼

以下是根據「人民郵電出版社」的「VISUAL BASIC 串口通訊實例導航」一書的第一章代碼修改用於發送和接收十六進制的數據流實用可運行代碼.
標准模塊:
Option Explicit
Public fMainForm As frmMain
Public yibiao_wei(10) As Integer
Public di1 As String * 2
Public main_i As Integer
Public i As Integer
Public j As Integer
Public fasong_sj(10, 5) As String
Public xh As Integer
Public di As Integer
Public sj_bm(10, 5) As Single
Public number As Byte
Public setMingling(10) As String * 16
Public alame(10) As String * 1
Public record_jm(5) As Single
Public a As Double
Public PRINT_Cs(14) As String
Public PRINT_WzCs(12) As String
Public shiYAnH As String
Public shiYAnTime As String
Public shiyan_sj(4) As String
Public print_fg As Byte
Option Explicit
Dim sum_zs
Dim xuhao_zs As String * 2
Dim i As Byte
Dim j As Byte
Dim ccl(2) As String * 1
Dim blL As String * 2
Dim bl As String * 1
Dim cclL(2) As String * 4
Dim bl_dm As String * 4
Dim zt_dm1 As String * 8
Dim jieshou_sj As String * 6
Dim sum As Byte
Dim sum1 As Byte
Dim xuhao As String * 2
Dim fa0 As String * 2
Dim HexStr1 As String * 20
' 基本設置
Private intPort As Integer '串列口號
Private strSet As String '協議設置
Private intTime As Integer '發送時間間隔
'發送與接收標志
Private blnAutoSendFlag As Boolean '發送標志
Private blnAutoSendFlag1 As Boolean '發送標志
Private blnReceiveFlag As Boolean '接收標志
'發送模塊
Private intOutMode As Integer '發送模式
Private strSendText As String '發送文本數據
Private bytSendByte() As Byte '發送二進制數據
'顯示標志
Private intHexChk As Integer '十六進制編碼標志
Private intAsciiChk As Integer 'ASCII碼標志
Private intAddressChk As Integer '地址標志
Private intAdd48Chk As Integer '4/8位地址標志
'接收模塊
Private bytReceiveByte() As Byte '接收到的位元組
Private intReceiveLen As Integer '接收到的位元組數
Private strTestn As String
'顯示模塊
Private strAddress As String '地址信息
Private strHex As String '十六進制編碼
Private strAscii As String 'ASCII碼
Private intHexWidth As Integer '顯示列數
'
Private intOriginX As Long '橫向原點(像素)
Private intOriginY As Integer '縱向原點(行)
Private intLine As Integer '總行數
'
Dim m As Integer
Dim blnChakanFlag As Boolean
'顯示常量
Private Const ChrWidth = 105 '單位寬度
Private Const ChrHeight = 2 * ChrWidth '單位高度
Private Const BorderWidth = 210 '預留邊界
Private Const LineMax = 16 '最大顯示行數
'輸入處理
'處理接收到的位元組流,並保存在全局變數
'bytReceiveRyte()
Private Sub InputManage(bytInput() As Byte, intInputLenth As Integer)
Dim n As Integer '定義變數及初始化
ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)
For n = 1 To intInputLenth Step 1
bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)
Next n
intReceiveLen = intReceiveLen + intInputLenth
End Sub
'為輸出准備文本
'保存在全局變數
'strText
'strHex
'strAddress
'總行數保存在intLine
Private Sub GetDisplayText()
Dim n As Integer
Dim intValue As Integer
Dim intHighHex As Integer
Dim intLowHex As Integer
Dim strSingleChr As String * 1
Dim intAddress As Integer
Dim intAddressArray(8) As Integer
Dim intHighAddress As Integer
Dim HexStr As String
On Error GoTo abc
strAscii = "" '設置初值
strHex = ""
strAddress = ""
'獲得16進制碼和ASCII碼的字元串
For n = 1 To intReceiveLen
intValue = bytReceiveByte(n - 1)
If intValue < 32 Or intValue > 128 Then '處理非法字元
strSingleChr = Chr(46) '對於不能顯示的ASCII碼,
Else '用"."表示
strSingleChr = Chr(intValue)
End If
strAscii = strAscii + strSingleChr
intHighHex = intValue \ 16
intLowHex = intValue - intHighHex * 16
If intHighHex < 10 Then
intHighHex = intHighHex + 48
Else
intHighHex = intHighHex + 55
End If
If intLowHex < 10 Then
intLowHex = intLowHex + 48
Else
intLowHex = intLowHex + 55
End If
HexStr = HexStr & Chr$(intHighHex) & Chr$(intLowHex)
HexStr1 = HexStr '傳遞數據
strHex = strHex + " " + Chr$(intHighHex) + Chr$(intLowHex) + " "
If (n Mod intHexWidth) = 0 Then '設置換行
strAscii = strAscii + Chr$(13) + Chr$(10)
strHex = strHex + Chr$(13) + Chr$(10)
Else
End If
Next n
'獲得地址字元串
intLine = intReceiveLen \ intHexWidth
If (intReceiveLen - intHexWidth * intLine) > 0 Then
intLine = intLine + 1
End If
For n = 1 To intLine
intAddress = (n - 1) * intHexWidth
If intAdd48Chk = 1 Then
intHighAddress = 8
Else
intHighAddress = 4
End If
intAddressArray(0) = intAddress
For m = 1 To intHighAddress
intAddressArray(m) = intAddressArray(m - 1) \ 16
Next m
For m = 1 To intHighAddress
intAddressArray(m - 1) = intAddressArray(m - 1) - intAddressArray(m) * 16
Next m
For m = 1 To intHighAddress
If intAddressArray(intHighAddress - m) < 10 Then
intAddressArray(intHighAddress - m) = intAddressArray(intHighAddress - m) + Asc("0")
Else
intAddressArray(intHighAddress - m) = intAddressArray(intHighAddress - m) + Asc("A") - 10
End If
strAddress = strAddress + Chr$(intAddressArray(intHighAddress - m))
Next m
strAddress = strAddress + Chr$(13) + Chr$(10) '設置換行
Next n
'Text1 = "Ok"
Exit Sub
abc:
'Text1 = "Error"
Resume
End Sub
'顯示輸出
Private Sub display()
Dim intViewWidth As Long '橫向寬度(像素)
Dim intViewLine As Integer '縱向寬度(行)
Dim strDisplayAddress As String
Dim strDisplayHex As String
Dim strDisplayAscii As String
strDisplayAddress = ""
strDisplayHex = ""
strDisplayAscii = ""
Dim intStart As Integer
Dim intLenth As Integer
'調整顯示頁面大小,設置滾動位置寬度
If intAdd48Chk = 1 Then
frmMain.txtHexEditAddress.Width = 8 * ChrWidth + BorderWidth
Else
frmMain.txtHexEditAddress.Width = 4 * ChrWidth + BorderWidth
End If
frmMain.txtHexEditHex.Width = intHexWidth * 4 * ChrWidth + BorderWidth
frmMain.txtHexEditText.Width = intHexWidth * ChrWidth + BorderWidth
frmMain.txtBlank.Width = BorderWidth
intViewWidth = frmMain.txtHexEditAddress.Width * intAddressChk + frmMain.txtHexEditHex.Width * intHexChk + frmMain.txtHexEditText.Width * intAsciiChk
If intViewWidth <= frmMain.fraHexEditBackground.Width And intLine < LineMax Then
frmMain.txtBlank.Width = frmMain.fraHexEditBackground.Width - intViewWidth
frmMain.hsclHexEdit.Visible = False
frmMain.vsclHexEdit.Visible = False
intViewWidth = frmMain.fraHexEditBackground.Width
intViewLine = intLine
intOriginX = 0
intOriginY = 0
ElseIf intViewWidth > frmMain.fraHexEditBackground.Width And intLine < LineMax - 1 Then
frmMain.hsclHexEdit.Visible = True
frmMain.vsclHexEdit.Visible = False
frmMain.hsclHexEdit.Width = frmMain.fraHexEditBackground.Width
intViewLine = intLine
intOriginY = 0
If intOriginX > intViewWidth - frmMain.fraHexEditBackground.Width Then
intOriginX = intViewWidth - frmMain.fraHexEditBackground.Width
End If
ElseIf intViewWidth < (frmMain.fraHexEditBackground.Width - frmMain.vsclHexEdit.Width) And intLine >= LineMax Then
frmMain.vsclHexEdit.Visible = True
frmMain.hsclHexEdit.Visible = False
frmMain.txtBlank.Width = frmMain.fraHexEditBackground.Width - intViewWidth
intViewWidth = frmMain.fraHexEditBackground.Width
intViewLine = LineMax
intOriginX = 0
If intOriginY > intLine - LineMax Then
intOriginY = intLine - LineMax
End If
Else
frmMain.hsclHexEdit.Visible = True
frmMain.vsclHexEdit.Visible = True
frmMain.hsclHexEdit.Width = frmMain.fraHexEditBackground.Width - frmMain.vsclHexEdit.Width
intViewLine = LineMax - 1
If intOriginX > intViewWidth - frmMain.fraHexEditBackground.Width Then
intOriginX = intViewWidth - frmMain.fraHexEditBackground.Width
End If
If intOriginY > intLine - LineMax + 1 Then
intOriginY = intLine - LineMax + 1
End If
End If
frmMain.txtHexEditAddress.Left = intOriginX
frmMain.txtHexEditHex.Left = intOriginX + frmMain.txtHexEditAddress.Width * intAddressChk
frmMain.txtHexEditText.Left = intOriginX + frmMain.txtHexEditAddress.Width * intAddressChk + frmMain.txtHexEditHex.Width * intHexChk
frmMain.txtBlank.Left = intOriginX + frmMain.txtHexEditAddress.Width * intAddressChk + frmMain.txtHexEditHex.Width * intHexChk + frmMain.txtHexEditText.Width * intAsciiChk
intStart = intOriginY * (6 + 4 * intAdd48Chk) + 1
intLenth = intViewLine * (6 + 4 * intAdd48Chk)
strDisplayAddress = Mid(strAddress, intStart, intLenth)
intStart = intOriginY * (intHexWidth * 4 + 2) + 1
intLenth = intViewLine * (intHexWidth * 4 + 2)
strDisplayHex = Mid(strHex, intStart, intLenth)
intStart = intOriginY * (intHexWidth + 2) + 1
intLenth = intViewLine * (intHexWidth + 2)
strDisplayAscii = Mid(strAscii, intStart, intLenth)
'設置滾動條
frmMain.vsclHexEdit.Max = intLine - intViewLine
frmMain.hsclHexEdit.Max = (intViewWidth - frmMain.fraHexEditBackground.Width) \ ChrWidth + 1
'顯示輸出
frmMain.txtHexEditHex.Text = strDisplayHex
frmMain.txtHexEditText.Text = strDisplayAscii
frmMain.txtHexEditAddress.Text = strDisplayAddress
End Sub
'文本無變化的刷新
Private Sub ScrollRedisplay()
Call display
End Sub
'文本發生變化的刷新
Private Sub SlideRedisplay()
Call GetDisplayText
Call display
End Sub
'字元表示的十六進制數轉化為相應的整數,錯誤則返回 -1
Function ConvertHexChr(str As String) As Integer
Dim test As Integer
test = Asc(str)
If test >= Asc("0") And test <= Asc("9") Then
test = test - Asc("0")
ElseIf test >= Asc("a") And test <= Asc("f") Then
test = test - Asc("a") + 10
ElseIf test >= Asc("A") And test <= Asc("F") Then
test = test - Asc("A") + 10
Else
test = -1 '出錯信息
End If
ConvertHexChr = test
End Function
'字元串表示的十六進制數據轉化為相應的位元組串,返回轉化後的位元組數
Function strHexToByteArray(strText As String, bytByte() As Byte) As Integer
Dim HexData As Integer '十六進制(二進制)數據位元組對應值
Dim hstr As String * 1 '高位字元
Dim lstr As String * 1 '低位字元
Dim HighHexData As Integer '高位數值
Dim LowHexData As Integer '低位數值
Dim HexDataLen As Integer '位元組數
Dim StringLen As Integer '字元串長度
Dim Account As Integer
Dim n As Long
'計數
strTestn = "" '設初值
HexDataLen = 0
strHexToByteArray = 0
StringLen = Len(strText)
Account = StringLen \ 2
ReDim bytByte(Account)
For n = 1 To StringLen
Do '清除空格
hstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While hstr = " "
Do
lstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While lstr = " "
n = n - 1
If n > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
HighHexData = ConvertHexChr(hstr)
LowHexData = ConvertHexChr(lstr)
If HighHexData = -1 Or LowHexData = -1 Then '遇到非法字元中斷轉化
HexDataLen = HexDataLen - 1
Exit For
Else
HexData = HighHexData * 16 + LowHexData
bytByte(HexDataLen) = HexData
HexDataLen = HexDataLen + 1
End If
Next n
If HexDataLen > 0 Then '修正最後一次循環改變的數值
HexDataLen = HexDataLen - 1
ReDim Preserve bytByte(HexDataLen)
Else
ReDim Preserve bytByte(0)
End If
If StringLen = 0 Then '如果是空串,則不會進入循環體
strHexToByteArray = 0
Else
strHexToByteArray = HexDataLen + 1
End If
End Function
Public Function Hex_bin()
'輸出口狀態鑒別
For i = 1 To 2
ccl(i) = Mid(blL, i, 1)
If ccl(i) >= Chr(48) And ccl(i) <= Chr(57) Or ccl(i) >= Chr(65) And ccl(i) <= Chr(70) Then
ccl(i) = ccl(i)
Else
Exit Function '退出過程函數
ccl(i) = "0"
End If
Next i
For j = 1 To 2
bl = ccl(j)
If bl = "F" Then
bl_dm = "1111"
ElseIf bl = "E" Then
bl_dm = "1110"
ElseIf bl = "D" Then
bl_dm = "1101"
ElseIf bl = "C" Then
bl_dm = "1100"
ElseIf bl = "B" Then
bl_dm = "1011"
ElseIf bl = "A" Then
bl_dm = "1010"
ElseIf bl = "9" Then
bl_dm = "1001"
ElseIf bl = "8" Then
bl_dm = "1000"
ElseIf bl = "7" Then
bl_dm = "0111"
ElseIf bl = "6" Then
bl_dm = "0110"
ElseIf bl = "5" Then
bl_dm = "0101"
ElseIf bl = "4" Then
bl_dm = "0100"
ElseIf bl = "3" Then
bl_dm = "0011"
ElseIf bl = "2" Then
bl_dm = "0010"
ElseIf bl = "1" Then
bl_dm = "0001"
ElseIf bl = "0" Then
bl_dm = "0000"
Else:
bl_dm = ""
End If
cclL(j) = bl_dm
Next j
zt_dm1 = cclL(1) + cclL(2)
For i = 1 To 8
'zt_dm(i) = Mid$(zt_dm1, i, 1)
Next i
End Function
Private Sub cboHexAscii_Click()
If frmMain.cboHexAscii.Text = "按ASCII碼" Then
intOutMode = 0
Else
intOutMode = 1
End If
End Sub
Private Sub chkAddress_Click()
If chkAddress.Value = 0 Then
intAddressChk = 0
Else
intAddressChk = 1
End If
Call ScrollRedisplay
End Sub
Private Sub chkAddress48_Click()
If chkAddress48.Value = 1 Then
intAdd48Chk = 1
Else
intAdd48Chk = 0
End If
Call SlideRedisplay
End Sub
Private Sub chkAscii_Click()
If chkAscii.Value = 1 Then
intAsciiChk = 1
Else
intAsciiChk = 0
End If
Call ScrollRedisplay
End Sub
Private Sub chkHex_Click()
If chkHex.Value = 0 Then
intHexChk = 0
Else
intHexChk = 1
End If
Call ScrollRedisplay
End Sub
Private Sub cmdAutoSend_Click()
If blnAutoSendFlag Then
frmMain.ctrTimer.Enabled = False
If Not blnReceiveFlag Then
frmMain.ctrMSComm.PortOpen = False
End If
frmMain.cmdAutoSend.Caption = "自動定址"
Else
If Not frmMain.ctrMSComm.PortOpen Then
frmMain.ctrMSComm.CommPort = intPort
frmMain.ctrMSComm.Settings = strSet
frmMain.ctrMSComm.PortOpen = True
End If
frmMain.ctrTimer.Interval = intTime
frmMain.ctrTimer.Enabled = True
frmMain.cmdAutoSend.Caption = "停止定址"
End If
blnAutoSendFlag = Not blnAutoSendFlag
End Sub
Private Sub cmdAutoSend1_Click()
'用於設置參數
If blnAutoSendFlag1 Then
Call cmdAutoSend_Click
frmMain.ctrTimer1.Enabled = False
frmMain.cmdAutoSend1.Caption = "自動設置"
Else
If Not frmMain.ctrMSComm.PortOpen Then
frmMain.ctrMSComm.CommPort = intPort
frmMain.ctrMSComm.Settings = strSet
frmMain.ctrMSComm.PortOpen = True
End If
Call cmdAutoSend_Click
frmMain.cmdAutoSend1.Caption = "停止設置"
frmMain.ctrTimer1.Enabled = True
End If
blnAutoSendFlag1 = Not blnAutoSendFlag1
End Sub
Private Sub cmdChakan_Click()
If blnChakanFlag Then
frmMain.cmdChakan.Caption = "查看"
frmMain.Height = 2800
Else
frmMain.cmdChakan.Caption = "恢復"
frmMain.Height = 6700
End If
blnChakanFlag = Not blnChakanFlag
End Sub
Private Sub cmdClear_Click()
Dim bytTemp(0) As Byte
ReDim bytReceiveByte(0)
intReceiveLen = 0
Call InputManage(bytTemp, 0)
Call GetDisplayText
Call display
End Sub
Private Sub cmdManualSend_Click()
If Not frmMain.ctrMSComm.PortOpen Then
frmMain.ctrMSComm.CommPort = intPort
frmMain.ctrMSComm.Settings = strSet
frmMain.ctrMSComm.PortOpen = True
End If
Call ctrTimer_Timer
If Not blnAutoSendFlag Then
frmMain.ctrMSComm.PortOpen = False
End If
End Sub
Private Sub cmdReceive_Click()
If blnReceiveFlag Then
If Not blnAutoSendFlag And Not blnReceiveFlag Then
frmMain.ctrMSComm.PortOpen = False
End If
frmMain.cmdReceive.Caption = "開始接收"
Else
If Not frmMain.ctrMSComm.PortOpen Then
frmMain.ctrMSComm.CommPort = intPort
frmMain.ctrMSComm.Settings = strSet
frmMain.ctrMSComm.PortOpen = True
End If
frmMain.ctrMSComm.InputLen = 0
frmMain.ctrMSComm.InputMode = 0
frmMain.ctrMSComm.InBufferCount = 0
frmMain.ctrMSComm.RThreshold = 10
frmMain.cmdReceive.Caption = "停止接收"
End If
blnReceiveFlag = Not blnReceiveFlag
End Sub
因長度超10000字,請另行提問給於補充.