Skip to content

Commit

Permalink
serial: make baud rate configurable and return object
Browse files Browse the repository at this point in the history
This makes the baudrate configurable and makes the opened serial port accessible - so different parts of the test can share and use this single object and avoid conflicts accessing the port. Also adds error handling for when the port can't be opened

Change-type: patch
Signed-off-by: Ryan Cooke <[email protected]>
  • Loading branch information
rcooke-warwick authored and acostach committed Aug 22, 2024
1 parent 04497d8 commit d2f83a6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions lib/features/serial/implementations/ftdi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ const SerialPort = require('serialport');

export class Ftdi implements Serial {
public DEV_SERIAL = '/dev/ttyUSB0' || process.env.DEV_SERIAL;
public BAUD_RATE = Number(process.env.BAUD_RATE || 115200);
public serial : any;
constructor(baudRate = 115200){
constructor(){
this.serial = new SerialPort(
this.DEV_SERIAL,
{
baudRate: baudRate,
baudRate: this.BAUD_RATE,
autoOpen: false,
});

Expand All @@ -18,9 +19,20 @@ export class Ftdi implements Serial {

async open(){
if(this.serial.isOpen){
console.log(`Serial already open!`)
console.log(`Serial already open!`);
return this.serial;
} else {
this.serial.open();
try{
console.log(`Opening Serial port ${this.DEV_SERIAL} with baud rate: ${this.BAUD_RATE}`)
this.serial.open(() => {
console.log('DUT serial is opened');
this.serial?.flush();
})
return this.serial;
} catch(e){
console.log(e)
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface Serial extends Base{
serial: any;
write(data: string): Promise<string | void>;
read(): Promise<string>;
open(): Promise<void>;
open(): Promise<Stream.Readable | null>;
close(): Promise<void>;
}

Expand Down

0 comments on commit d2f83a6

Please sign in to comment.