顯示具有 programming 標籤的文章。 顯示所有文章
顯示具有 programming 標籤的文章。 顯示所有文章

2017年2月15日 星期三

使用 C# 判斷有那些 Serial Port

    
    SerialPort ComPort = new SerialPort();
    string[] ports = SerialPort.GetPortNames();
    foreach(string portName in ports)
    {
        ComPort.PortName = portName;
        ComPort.BaudRate = 115200;
        ComPort.Parity = Parity.None;
        ComPort.StopBits = StopBits.One;
        ComPort.ReadTimeout = 500;
        ComPort.WriteTimeout = 500;
        try
        {
            ComPort.Open();
            ...
        }
        catch
        {
            ComPort.Close()
         
        }
    }

2016年7月19日 星期二

在 Batch File 中使用 findstr 比對 log 檔中的訊息

當 Batch File 需要判斷某個程式輸出的 log 檔內容時,  findstr 是一個可用的工具.

目前用過的參數:

參數說明:
findstr /?

比對檔案中的文字
findstr /c:string filename

可用 ERRORLEVEL 判斷是否有符合字串.

ERRORLEVEL 為 0 時, 有符合字串.
ERRORLEVEL 為 1 時, 沒有找到符合字串.

--
ERRORLEVEL 的用法:

IF ERRORRLEVEL N to check if the errorlevel is >= N. 
IF NOT ERRORLEVEL N to check if errorlevel is < N

例如:

findstr /C:FFFF info_1.hex
IF ERRORLEVEL 1 goto NOT_MATCH
echo MATCH
goto END

:NOT_MATCH

echo NOT_MATCH

:END

Batch file 指令太長如何換行

當 Batch File 的一行太長要換行時, 加入 "^" (不含 ") , 緊接著換行就可以了.
例如:

echo off
IF "%1"=="" goto end
echo %1

:end

寫成:


echo off
IF "%1"=="" ^
goto end

echo %1

:end

注意 "^" 一定要緊接著換行碼, 不能有空格!

2016年5月4日 星期三

讀取特定的行數

要讀取文字檔中的特定行數, 除了 for line in file(file) 之外;
使用內建函式 enumerate()

    for i, line in enumerate(fin,1):
        if i == 5:
            #do something
            break
    fin.close()


enumerate(iterable, start=0), 預設 start 是 0; 但是行數從 1 開始, 所以把 start 設為 1

2016年3月23日 星期三

8051 Timer0 Mode0 (13 bits)

8051 Timer0 Mode0 (TMOD = 0x00) 是 13 bit timer, 要注意的是這裡 TL0 是 5 bits !!

TH0 (8 bits): 0~255
TL0 (5 bits): 0~31

所以在計算 overflow 時, 把計算值除 32 才是要給 TH0, 餘數給 TL0.
例如要在 1000 count overflow :

TH0 = (8192-1000)/32;
TL0 = (8192-1000)%32;

2016年3月2日 星期三

TI CCStudio Bit-Field Definitions

Bit-Field Definitions
- Bit field members are stored from right to left in memory. (LSB 開始放置)

例如:
struct SCICCR_BITS {        // bit description
    Uint16 SCICHAR:3;       // 2:0 Character length control (LSB)
    Uint16 ADDRIDLE_MODE:1; // 3 ADDR/IDLE Mode control
    Uint16 LOOPBKENA:1;     // 4 Loop Back enable
    Uint16 PARITYENA:1;     // 5 Parity enable
    Uint16 PARITY:1;        // 6 Even or Odd Parity
    Uint16 STOPBITS:1;      // 7 Number of Stop Bits
    Uint16 rsvd1:8;         // 15:8 reserved
};


- The C28x compiler limits the maximum number of bits within a bit field to the size of an integer; no bit field can be greater than 16 bits in length.
 - If the total number of bits defined by bit fields within a structure grows above 16 bits, then the next bit field is stored consecutively in the next word of memory.

要注意的是, 例如寫入單一個成員變數, CPU 是執行 read-modify-write; 也就是說在有些 register 寫入 1 是清除的指令, 當成員變數在同一個 structure 中時, 寫入 A 成員, 可能會造成 B 成員也被清除. 這時候可能要用整個 structure 寫入的方式.
 以下的 register 在執行 read-modify-write 動作時, 要注意:
1) Registers with bits that hardware can change after the read, but before the write
2) Registers with write 1-to-clear bits
3) Registers that have bits that must be written with a value different from what the bits read back

ref: TI SPRAA85D, Programming TMS320x28xx and 28xxx Peripherals in C/C++ (Rev. D)
p.16

2016年1月7日 星期四

關於計算一年之中的第幾週

The ISO 8601 definition for week 01 is the week with the year's first Thursday in it.

一年裡的第一週就是第一個星期四出現的那一週.
 2016/1/1 是星期五, 所以是2015的第 53 週


ref:
https://en.wikipedia.org/wiki/ISO_week_date#Calculating_the_week_number_of_a_given_date

2015年11月24日 星期二

Borlndmm.dll missing 的問題

XE10 可能會出現編譯後在另一台電腦上執行出現少了 Borlndmm.dll 的問題.

在 project 中設定:
Project\ Options\ C++ Compiler\ Classic Compiler\ Use 'classic' Borland compiler = false


2015年10月12日 星期一

python 3.0 print() 如何不換行

在 print() 加入 end = "" 參數即可

example:

b = [1,2,3,4,6]
for item in b:
    print(item,end="")

2015年9月29日 星期二

Energia : using hardware UART

Using LaunchPad MSP-EXP430G2
MCU: M430G2553

problem:
Serial Monitor can't get the data from LaunchPad

Solution:
  Connecting to the USB2.0 hub port instead of connecting to USB3.0 port. If it still not works, trying another USB2.0 hub port.


使用 LaunchPad 時, Serial Monitor 一直無法看到 UART 傳來的資料.
後來使用 USB2.0 hub 連接 LaunchPad , Serial Monitor 就可收到 UART 的資料.

2015年9月18日 星期五

ATMEGA328 port 的操作

原因: 使用 arduino 的 digitalWrite() 及 digitalRead() 的速度不夠快, 直接對 register 控制.
用到的 register:
   PORTD: set data output
   DDRD  : set input/output mode
   PIND    : read data input . if write 1 , the port will toggle.

方法:

-- 找出對應的 pin 腳 --
input pin:
  digital pin 6, PORTD6

output pin:
  digital pin 2, PORTD2

-- 設定 --
 output mode:
    DDRD |= (0x04);    // 1: output, 0:input
  
 input mode:
   DDRD &= (~0x40)
   PORTD |= (0x40);  // input pull-high

-- 讀值 --
   Data = PIND;     // read PORTD input data

ex: // 反向輸出
..
noInterrupts();
while(1){
    
    if(PIND & 0x04){
        PORTD &= (~0x40);
    }
    else{
        PORTD |= (0x40);
    }
}