Author Topic: Problems with Reading and Writing to Serial Port Using DigiCDC or DigiUSB  (Read 4000 times)


  • Newbie
  • *
  • Posts: 6
Hello there, newbie here

I recently bought a digispark with attiny85 and work pretty well for most of the part. But i've stumbled upon some problems that i'm not being able to fix: How can i transmit data to serial port using but the board itself and it's usb connection. From what i've read it's possible to send strings via DigiUSB to a serial monitor but without sucess. Using DigiCDC creats a virtual port and i can open ot without problems, but i am not able to read/write to it.
On the pc side the code must be written in C, and from what i have until now works fine with Arduino boards like the Nano.
Code: [Select]
#include <sys/types.h>
      #include <sys/stat.h>
      #include <fcntl.h>
      #include <termios.h>
      #include <stdio.h>
      #define BAUDRATE B9600
      #define MODEMDEVICE "/dev/ttyS0"
      #define _POSIX_SOURCE 1 /* POSIX compliant source */
      #define FALSE 0
      #define TRUE 1
      volatile int STOP=FALSE;
        int fd,c, res;
        struct termios oldtio,newtio;
        char buf[255];
        fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
        if (fd <0) {perror(MODEMDEVICE); exit(-1); }
        tcgetattr(fd,&oldtio); /* save current port settings */
        bzero(&newtio, sizeof(newtio));
        newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
        newtio.c_iflag = IGNPAR;
        newtio.c_oflag = 0;
        /* set input mode (non-canonical, no echo,...) */
        newtio.c_lflag = 0;
        newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
        newtio.c_cc[VMIN]     = 2;   /* blocking read until 5 chars received */
        tcflush(fd, TCIFLUSH);
        while (STOP==FALSE) {       /* loop for input */
          res = read(fd,buf,1);   /* returns after 5 chars have been input */
          buf[res]=0;               /* so we can printf... */
          printf("%s", buf);
          if (buf[0]=='z') STOP=TRUE;

The code sets the port for non-canonnical mode and receives a string, stops when detects a 'z' unto the string.
I've already changed the Serial Port and even Baud rates but no data is ever read nor writen.
On the digispark side i derived from DigisparkExamples->DigiCDC->Echo and DigiUSB-> Echo, but only to read or write one experiment at a time.
Is there a way to read/write data to digispark? I was thinking maybe i can use SoftSerial and redirect the Tx and Rx pins to USB+ and USB-, but no sure if it will do the job.
I'm using Ubuntu btw.

Best Regards


  • Hero Member
  • *****
  • Posts: 881
Hi ;)

It's been quite a while since I've played with the CDC/USB comms stuff, but AFAIK, it did work well enough considering it's limitations.

Are you aware of the wiki pages about DigiUSB and DigiCDC? Note that DigiUSB has been deprecated in favour of DigiCDC, as with DigiUSB you needed you to run a specific program in order to communicate with the Digispark... it didn't show up as a serial port ;)

So, is the Echo DigiCDC example working for you? Is it sending and receiving if you use the Arduino IDE serial monitor? Meaning you have the driver that it needs installed (you can download them from here if you haven't already got them). And when writing code using it, you know that you need to swap out standard delays with SerialUSB.delays (so VUSB can do it's thing)? Just some of the standard "haven't overlooked something stupidly simple" questions!  :o ;D


  • Newbie
  • *
  • Posts: 6
Hi, thanks for replying

I have read the wiki pages and installed the drivers. Using the CDC example doesn't  work.
If i use the program for DigiUSB it shows the following output
Code: [Select]
Detecting USB devices...
Detected DigiSpark...
Found 2 interfaces, using interface 0
Read from Digispark: Error -71 reading from USB device

Error -22 releasing Interface 0
Still doesn't read. Made an analysis to the code in receive.cpp but im afraid i'm completly ignorant to using usb.h library.
Any help would be welcome.
Best Regards